summaryrefslogtreecommitdiffabout
path: root/pwmanager
authorzautrix <zautrix>2004-10-19 20:16:14 (UTC)
committer zautrix <zautrix>2004-10-19 20:16:14 (UTC)
commiteca49bb06a71980ef61d078904573f25890fc7f2 (patch) (unidiff)
treec5338e3b12430248979a9ac2c1c7e6646ea9ecdf /pwmanager
parent53cc32b6e7b1f672bf91b2baf2df6c1e8baf3e0a (diff)
downloadkdepimpi-eca49bb06a71980ef61d078904573f25890fc7f2.zip
kdepimpi-eca49bb06a71980ef61d078904573f25890fc7f2.tar.gz
kdepimpi-eca49bb06a71980ef61d078904573f25890fc7f2.tar.bz2
Initial revision
Diffstat (limited to 'pwmanager') (more/less context) (ignore whitespace changes)
-rw-r--r--pwmanager/libcrypt/cipher/Manifest73
-rw-r--r--pwmanager/libcrypt/cipher/ac.c1390
-rw-r--r--pwmanager/libcrypt/cipher/arcfour.c156
-rw-r--r--pwmanager/libcrypt/cipher/bithelp.h54
-rw-r--r--pwmanager/libcrypt/cipher/blowfish.c605
-rw-r--r--pwmanager/libcrypt/cipher/cast5.c620
-rw-r--r--pwmanager/libcrypt/cipher/cipher.c1391
-rw-r--r--pwmanager/libcrypt/cipher/cipher.pro70
-rw-r--r--pwmanager/libcrypt/cipher/crc.c295
-rw-r--r--pwmanager/libcrypt/cipher/des.c1111
-rw-r--r--pwmanager/libcrypt/cipher/dsa.c486
-rw-r--r--pwmanager/libcrypt/cipher/elgamal.c682
-rw-r--r--pwmanager/libcrypt/cipher/md.c1253
-rw-r--r--pwmanager/libcrypt/cipher/md4.c327
-rw-r--r--pwmanager/libcrypt/cipher/md5.c354
-rw-r--r--pwmanager/libcrypt/cipher/primegen.c1028
-rw-r--r--pwmanager/libcrypt/cipher/pubkey.c2352
-rw-r--r--pwmanager/libcrypt/cipher/rand-internal.h41
-rw-r--r--pwmanager/libcrypt/cipher/random.c1134
-rw-r--r--pwmanager/libcrypt/cipher/random.h42
-rw-r--r--pwmanager/libcrypt/cipher/rfc2268.c345
-rw-r--r--pwmanager/libcrypt/cipher/rijndael.c2228
-rw-r--r--pwmanager/libcrypt/cipher/rmd.h36
-rw-r--r--pwmanager/libcrypt/cipher/rmd160.c569
-rw-r--r--pwmanager/libcrypt/cipher/rndegd.c256
-rw-r--r--pwmanager/libcrypt/cipher/rndlinux.c141
-rw-r--r--pwmanager/libcrypt/cipher/rndunix.c848
-rw-r--r--pwmanager/libcrypt/cipher/rndw32.c682
-rw-r--r--pwmanager/libcrypt/cipher/rsa.c630
-rw-r--r--pwmanager/libcrypt/cipher/serpent.c979
-rw-r--r--pwmanager/libcrypt/cipher/sha1.c368
-rw-r--r--pwmanager/libcrypt/cipher/sha256.c310
-rw-r--r--pwmanager/libcrypt/cipher/sha512.c400
-rw-r--r--pwmanager/libcrypt/cipher/tiger.c850
-rw-r--r--pwmanager/libcrypt/cipher/twofish.c1040
-rw-r--r--pwmanager/libcrypt/config.h314
-rw-r--r--pwmanager/libcrypt/crypt/Manifest58
-rw-r--r--pwmanager/libcrypt/crypt/ath.c287
-rw-r--r--pwmanager/libcrypt/crypt/ath.h116
-rw-r--r--pwmanager/libcrypt/crypt/cipher.h80
-rw-r--r--pwmanager/libcrypt/crypt/g10lib.h250
-rw-r--r--pwmanager/libcrypt/crypt/gcrypt-module.h225
-rw-r--r--pwmanager/libcrypt/crypt/gcrypt.h1497
-rw-r--r--pwmanager/libcrypt/crypt/global.c672
-rw-r--r--pwmanager/libcrypt/crypt/misc.c241
-rw-r--r--pwmanager/libcrypt/crypt/missing-string.c151
-rw-r--r--pwmanager/libcrypt/crypt/module.c200
-rw-r--r--pwmanager/libcrypt/crypt/mpi.h199
-rw-r--r--pwmanager/libcrypt/crypt/secmem.c653
-rw-r--r--pwmanager/libcrypt/crypt/secmem.h38
-rw-r--r--pwmanager/libcrypt/crypt/sexp.c1804
-rw-r--r--pwmanager/libcrypt/crypt/stdmem.c195
-rw-r--r--pwmanager/libcrypt/crypt/stdmem.h32
-rw-r--r--pwmanager/libcrypt/crypt/types.h124
-rw-r--r--pwmanager/libcrypt/error/code-from-errno.c44
-rw-r--r--pwmanager/libcrypt/error/code-from-errno.h158
-rw-r--r--pwmanager/libcrypt/error/code-to-errno.c42
-rw-r--r--pwmanager/libcrypt/error/code-to-errno.h733
-rw-r--r--pwmanager/libcrypt/error/err-codes-sym.h446
-rw-r--r--pwmanager/libcrypt/error/err-codes.h446
-rw-r--r--pwmanager/libcrypt/error/err-sources-sym.h70
-rw-r--r--pwmanager/libcrypt/error/err-sources.h70
-rw-r--r--pwmanager/libcrypt/error/errnos-sym.h319
-rw-r--r--pwmanager/libcrypt/error/error.pro32
-rw-r--r--pwmanager/libcrypt/error/gettext.h69
-rw-r--r--pwmanager/libcrypt/error/strerror-sym.c56
-rw-r--r--pwmanager/libcrypt/error/strerror.c169
-rw-r--r--pwmanager/libcrypt/error/strsource-sym.c43
-rw-r--r--pwmanager/libcrypt/error/strsource.c37
-rw-r--r--pwmanager/libcrypt/mpi/ChangeLog572
-rw-r--r--pwmanager/libcrypt/mpi/Manifest41
-rw-r--r--pwmanager/libcrypt/mpi/asm-syntax.h71
-rw-r--r--pwmanager/libcrypt/mpi/generic/Manifest29
-rw-r--r--pwmanager/libcrypt/mpi/generic/distfiles11
-rw-r--r--pwmanager/libcrypt/mpi/generic/mpi-asm-defs.h10
-rw-r--r--pwmanager/libcrypt/mpi/generic/mpih-add1.c65
-rw-r--r--pwmanager/libcrypt/mpi/generic/mpih-lshift.c68
-rw-r--r--pwmanager/libcrypt/mpi/generic/mpih-mul1.c62
-rw-r--r--pwmanager/libcrypt/mpi/generic/mpih-mul2.c68
-rw-r--r--pwmanager/libcrypt/mpi/generic/mpih-mul3.c68
-rw-r--r--pwmanager/libcrypt/mpi/generic/mpih-rshift.c67
-rw-r--r--pwmanager/libcrypt/mpi/generic/mpih-sub1.c66
-rw-r--r--pwmanager/libcrypt/mpi/generic/udiv-w-sdiv.c133
-rw-r--r--pwmanager/libcrypt/mpi/longlong.h1561
-rw-r--r--pwmanager/libcrypt/mpi/mpi-add.c236
-rw-r--r--pwmanager/libcrypt/mpi/mpi-bit.c257
-rw-r--r--pwmanager/libcrypt/mpi/mpi-cmp.c74
-rw-r--r--pwmanager/libcrypt/mpi/mpi-div.c364
-rw-r--r--pwmanager/libcrypt/mpi/mpi-gcd.c54
-rw-r--r--pwmanager/libcrypt/mpi/mpi-inline.c36
-rw-r--r--pwmanager/libcrypt/mpi/mpi-inline.h154
-rw-r--r--pwmanager/libcrypt/mpi/mpi-internal.h275
-rw-r--r--pwmanager/libcrypt/mpi/mpi-inv.c275
-rw-r--r--pwmanager/libcrypt/mpi/mpi-mpow.c224
-rw-r--r--pwmanager/libcrypt/mpi/mpi-mul.c220
-rw-r--r--pwmanager/libcrypt/mpi/mpi-pow.c302
-rw-r--r--pwmanager/libcrypt/mpi/mpi-scan.c132
-rw-r--r--pwmanager/libcrypt/mpi/mpi.pro42
-rw-r--r--pwmanager/libcrypt/mpi/mpicoder.c643
-rw-r--r--pwmanager/libcrypt/mpi/mpih-div.c535
-rw-r--r--pwmanager/libcrypt/mpi/mpih-mul.c530
-rw-r--r--pwmanager/libcrypt/mpi/mpiutil.c431
-rw-r--r--pwmanager/libcrypt/mpi/sysdep.h2
-rw-r--r--pwmanager/libcrypt/zlib/ChangeLog505
-rw-r--r--pwmanager/libcrypt/zlib/README148
-rw-r--r--pwmanager/libcrypt/zlib/adler32.c48
-rw-r--r--pwmanager/libcrypt/zlib/algorithm.doc105
-rw-r--r--pwmanager/libcrypt/zlib/compress.c68
-rw-r--r--pwmanager/libcrypt/zlib/crc32.c163
-rw-r--r--pwmanager/libcrypt/zlib/deflate.c1350
-rw-r--r--pwmanager/libcrypt/zlib/deflate.h318
-rw-r--r--pwmanager/libcrypt/zlib/infblock.c403
-rw-r--r--pwmanager/libcrypt/zlib/infblock.h39
-rw-r--r--pwmanager/libcrypt/zlib/infcodes.c251
-rw-r--r--pwmanager/libcrypt/zlib/infcodes.h27
-rw-r--r--pwmanager/libcrypt/zlib/inffast.c183
-rw-r--r--pwmanager/libcrypt/zlib/inffast.h17
-rw-r--r--pwmanager/libcrypt/zlib/inffixed.h151
-rw-r--r--pwmanager/libcrypt/zlib/inflate.c366
-rw-r--r--pwmanager/libcrypt/zlib/inftrees.c454
-rw-r--r--pwmanager/libcrypt/zlib/inftrees.h58
-rw-r--r--pwmanager/libcrypt/zlib/infutil.c87
-rw-r--r--pwmanager/libcrypt/zlib/infutil.h98
-rw-r--r--pwmanager/libcrypt/zlib/trees.c1214
-rw-r--r--pwmanager/libcrypt/zlib/trees.h128
-rw-r--r--pwmanager/libcrypt/zlib/uncompr.c58
-rw-r--r--pwmanager/libcrypt/zlib/zconf.h279
-rw-r--r--pwmanager/libcrypt/zlib/zlib.h893
-rw-r--r--pwmanager/libcrypt/zlib/zlib.pro36
-rw-r--r--pwmanager/libcrypt/zlib/zutil.c225
-rw-r--r--pwmanager/libcrypt/zlib/zutil.h220
131 files changed, 48586 insertions, 0 deletions
diff --git a/pwmanager/libcrypt/cipher/Manifest b/pwmanager/libcrypt/cipher/Manifest
new file mode 100644
index 0000000..0cd64f7
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/Manifest
@@ -0,0 +1,73 @@
1# Manifest - checksums of the cipher directory
2# Copyright 2003 Free Software Foundation, Inc.
3#
4# This file is part of Libgcrypt.
5#
6# Libgcrypt is free software; you can redistribute it and/or modify
7# it under the terms of the GNU Lesser general Public License as
8# published by the Free Software Foundation; either version 2.1 of
9# the License, or (at your option) any later version.
10#
11# Libgcrypt is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public
17# License along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19
20# Checksums for all source files in this directory. Format is
21# filename, blanks, base-64 part of an OpenPGP detached signature
22# without the header lines. Blank lines and lines beginning with a
23# hash mark are ignored. A tool to process this file is available by
24# cvs -d :pserver:anoncvs@cvs.gnupg.org:/cvs/wk co misc-scripts/manifest-tool
25#
26# The special entry "$names$" holds a signature over all sorted
27# filenames excluding itself.
28
29
30# Algorithm API
31cipher.c iQCVAwUAQDzrVjEAnp832S/7AQIPDgP+OVJ/YNWY5m7c09EBbPAzL/WsGoj6wrBNMmkRlMOqTHeh+OOtjuFHt1f9uhfM2Nzl7sJ5+h4ryZKLEZmQPRMTZTnAqkvGdsrJWJnigUA9QwYdV0ONqC9C63gpuG465gO9TZVOqlQu/FTxSRuTQYUulkaBNG71n8nZEOusBVwV2YA==58xH
32pubkey.c iQCVAwUAP9XQ3jEAnp832S/7AQJ5UgQAyHfEBvPVJ8wTRg8c7ixS2GiVmIgwIo5tvQaiQJTPWASevvYrB+2Z2qa9cATyu50ACjLzbaquGBgPzjJV3dU/qttT1gCqRuN/LCNvXFe5qnIZezejc3RAadFNTw/pOTHq0wxD1Keg66ruei9R36Nba59pEQIWIBXTfubRft2hMYk==E09t
33ac.c iQCVAwUAQDzsOzEAnp832S/7AQJCBQP/WI6EV/dsR4rmha6RVhvkjZo17kQ8z6pIl5J3cXOvqEkIFeD2HYu3HHrWST5l7yXlffhpDkVHkfMih4ruK76q6Fm0dxZ98pO4C/dVtgimlvvcy/wOQjpzsE0fYAe1BYdg81LJ09X33vW5x6C29lunfKROO2tPlV5i8ffeoFvmMF8==j26g
34md.c iQCVAwUAP+NFGjEAnp832S/7AQJs8wP/Qdk0EAKsyr3O1/pmOSN8AG4rPKbd6KDTzvoBPAN4upFwKYY4hWwvy12Q3YU9DmECrzZkRCXHR7mljVQKs6B7CRZJKjFKmOELpcJDtKvu40vTs1bOH4k9iJYZpGgRA83nkQ+ELAcphAbCA+KIpVr2K4mCJAB0FhpC2uOQ50JHAko==BeF6
35primegen.c iQCVAwUAQDzsoDEAnp832S/7AQKYRwP/TqAQBm1rHTnF0HYE05PqXfWlOqa6EosqVpaOcs/OIW6PaqX0xH1UlrukK7jNOjK3xC4o1qNQ1UKzz2dvQaq1bMvNNizeavxAh10SJZc0hIc/ofc83IbjLh8SZVWQ67JxjsUd3DOXmSmhPZ+Pqd7cUIiw8fDoF+I9EZqy3COu1wY==1ebT
36
37# Algorithm implementations
38arcfour.c iQCVAwUAP9XR/TEAnp832S/7AQJcRwP6AlvYEx++fpT4mIYo0xRDqKEQeqMQvbaRhIg2eV74JxItpHa3q5YsYIl+n1yUz5g35JRWWXSWmAZBwO5wLKsHii4kRUhgrKWnSoQZoPpl49L5+N3R58ON3S0ru5lsBiEJEze3xplf2vqwrH9v1QHVD+gU7UTlfNqrIJoOUXN+1O4==Tq+x
39blowfish.c iQCVAwUAP9XTETEAnp832S/7AQJaEgQAgiqqfuO+zQtscgTB0rvOzVymIKjRKjYhFuLjVuc79G4z1RCAffvIn/YM2d7kt+Z/QF7zjcTAOgETCQL1XokpX2zz9HPAMi2tlDY5zsDufTNqj0n4WBL9nM7w6XAvsiwP1B3bqCTv9SjJV4KbxJ58vw1yQE+sqW74R/QIHFvC7mU==wZnX
40cast5.c iQCVAwUAP9XT6DEAnp832S/7AQJ3xgP/ehLjEN3GELGudbqeo91Xd+PqitHrkuBbtRIYX7Udd/fyXLN+h8rMJVyIQX2m+mpxbBxudVU3x8/DNT8B0ZHAwK6qqJmEBLLhEYPgIuF76i9LMrP1KqUPhAwRZ2OppjIIugBQ+rP74aD4eLyd/aKQHNuXML8QGWR6KwQShohXM5I==/BRh
41crc.c iQCVAwUAP7ouejEAnp832S/7AQIgwQQApg5Nm63tH5DQkbN+zPzMO9Ygoj3ukxfFTyTBPYSXYKMiTjEbESegaU40uN8jnz2vprcIQWcgZfzO4+opEJMcI35aPwzEk0vKOp0S/PrBLUY2rJfnDVkX5XgJFZa2Q7LLe826UEBzTVYW924utiCCe8oOaOEWVNpg1mqdknu3M9o==kz5D
42des.c iQCVAwUAQCN2oDEAnp832S/7AQL/jwP6Auoq6nZCDBjpgc9tDzuIRwa9DqyuM3gX94uvgEpUwdHszb2bG43dz03kVmcYxtj1MzXbyCeCZOwox0b2SKmLgxIbrNP6yGbzVdTj6592gDYuf/ZXmc1ZNJ1DDldcPQ0n9fXUipUPwyPaNWo3mSZaNcMKSWWzdK0J6ciG6nk7SWI==9k/t
43dsa.c iQCVAwUAP9XZHDEAnp832S/7AQLBRgP/XrBzTEYx5ccMj1MMb6sg37liEHdIyyy49zjvt6jUqxj4RuwVEN8S6v3u4q/QyJkHAi1E0EkREgENlyHW6PKWhYbcrd0vPIAN15yjnl2yqtrCrJImexUCoqJJewK0E4JOicGbabTil8MZjk+mbhEPnjJBqOkyP1w0i31pEDgE/8M==pC8s
44elgamal.c iQCVAwUAP9XbYzEAnp832S/7AQLXagQA3HrvspZfbTGgmUH0IqLQTJ0exUPxJv5DET2TvoIy62trDmMN6lTAj5P+a7jQ8udcu0w+mR2vXUHcxUpNA2PxLaMwGzNSY4zRDNe9r3SFTDrFm6m4y9Ko2e8XtEA+WF6P/XLpck4Jn7vMEDmVGPwkNd22kXFFE8dBGwG6i5Hk1Mk==oBUs
45md4.c iQCVAwUAP9h50DEAnp832S/7AQJhHgQAzNA/B6MWFDlCtPkIVaW8RpP1Eg0ZNMsy0s7SJkopOCBlu6CwXUOKe+8ppcSxhjYKh4i4uQr/QtfipYlBjzKJGnrafoF/NugXNCOHSTGT11TvK7mCiBuUMVgvZGAlOJImk6eTTfUjRrMfaXM/SWl8bdJ4ZpzdjEyVh89r7I5JrGk==x2UD
46md5.c iQCVAwUAP9h7LzEAnp832S/7AQJUGQP/c0cbf6WZXCzmjufHxiE9FAQBzTsA0WtaNqdFcHl7fhmikGtknlaED8n5a7eYd/C481UQW6Wgq/oZdsvgoPWPhG3fOCy2CFP9cZVXITuMSf0ucyZTFUJNO15fnZ+nDfsUv+JPdv1aSeRinAUtfAcSKfkSyR9BCPZvkx+tgU6cphU==Zv+h
47rijndael.c iQCVAwUAP9h9cTEAnp832S/7AQKF1AP+P2L/tPqDJRDg+/fwbOk8Ts0MNxnvvYEm3gE73TKuLt1S+B2+jkrZcKNvM5VGPnVMJbnS0lmIK04nmedHCOftGTOwhGulZAHHIaKGystT3Jql4iPws/JMgAjE7Fyxh5WZMtB9yEljKBpJ5XNqhrMvvxcHpnyP3+YzIXNwzk34V+c==dJ5k
48rmd160.c iQCVAwUAP9h+bTEAnp832S/7AQK1OgP+PNKF6Nzi6X93easVlksdLqKEsArCAw2QjGWDGyxTnbiJM55qAl9JxR1mn3V+oOL7izLLwTt6EYK9evhzfcxY5N5Mni85RAcsLPsuAfQDEzjI6GUWHtQUKPbM+BaorzfhQjYFSZyvum/dZYJ/WfiwwwhqqIKyVU2ZFSqA38YGC/c==9jdA
49rsa.c iQCVAwUAP9iHIzEAnp832S/7AQKAYwQAuWtnMte54QHN+Hij9t4sGuypXogajOb1vQQwGgS0fKsaBZsuSP2amze4o5diIvsQTsFQ4CzjvqoCVuBDoHM3xkSD8wGDizgvtCamAxkdbF7wmzldKFn8SpJqlVwWQMP6kk1IjXHEuYb4IDWGTbVMhfEu+eOlU8+PSK4IhZqNvt4==/3hp
50serpent.c iQCVAwUAP9h/VzEAnp832S/7AQLyCwP/d1zbmb7l/PriZNa9/Z7mo01XFe5MnAqCfIwhl9GjeaMszcoS37jECNq5nLvrTTFIIJpm3rvBePwiCG4Wwx1I18HCxaP198pcSaR+BLOJ3Aj52EZPrxtqlDKuFr38ZOP5giyUqUYVYGVdrz4kRMNWAZQK53GeJnGhXCnhxojLEgA==ck46
51sha1.c iQCVAwUAP9iATTEAnp832S/7AQKcSwQAwAs/HnNqho3lU1ZUgCPNt5P2/Brm6W21+wWWGKJkSrra/c4NYVKJGDDwlsFE0b9ln1uZt7bHReFkKXK3JnrKTmNVcx/Cy64iCMRNMhaM72Mqy7wWx5yHBAmMBxzFGnNQKbmeY52zeGih5HsNLSibc2pPuOViWo2JPJ5Ci/wIwl8==/wtO
52sha256.c iQCVAwUAP9iAtzEAnp832S/7AQJD2QP/UqvL0hhjG1wEFbGrdkV9tba1sMDXdnnK6X7HdLuRpVAgNiQiFf8JDmntd/dZ2Q71p4Uae2ctqve4WoEijPUZPjACnpuZfx0SEQL0lQBkwxzJp7lz9ujVtwQ2cM/aYexJkXcWgGcloJNLM3JbWPGIJnuYbr/IwJ6RQF9vgj0357o==UWO1
53sha512.c iQCVAwUAP9iBTDEAnp832S/7AQIPBAQA28CJSUQLiW0s2x9u8/OH2eKnxPjA4sZmb50WP7920Lem66P31C3BrOqwfBot4RLhjL+zh/+Uc4s3HPwApZuj9E4BxNMlqLv+Tqk++DAbdaOeYT4jeUt+mlhQQ6mH/RDsy32rZsNsGQ2bUGxazZmfG++PL3JyhawqCy00SUDr/o0==H+0X
54tiger.c iQCVAwUAP9iCfjEAnp832S/7AQKufwP/fryv3MqSOYY+90325DH7X3/CtekxeooN0scGsHX0fxBakWSMecTNrj33KPddLS46gU/S89zIc2N/Bw/7EVIAXVFA3/3Ip+OrFOuIMO4Py1sCdB8o2Y+5ygv8iXLcsXIq1O0av79i9g774V3uaXa2qN9ZnXe0AEhcy8FHJ2i/wro==5XVB
55twofish.c iQCVAwUAP9iD6TEAnp832S/7AQKUnQP/Rq8FaYeHTG7HbZuqAs9pbPitzjDbkdZddmInWR7NmevBkKvhsJALjVooc0KGQfo2lAAmy3Xi/4QQN8VPn51DVjDIgf7x+DQh/9TFJHMccxI9asUgi4+TNnmMqLU1k3N8S2PjyZ1sjeC8B79fKPpwCzj72WkqPkzZw3l2jArr+dU==NdJT
56rfc2268.c iQCVAwUAQCN+3jEAnp832S/7AQLv1gQA1hJh29hAjKi4uLSGxXvJ6cyYmPdmevdKrbLnuHZWtHe4xvCgy/nTdEojEpxgLp/hL/ogasuWRC1W16Wiz9ryxf7YR0uhZWayO/bQNagpfU5MIkJTLuKqqgpwYumCSQfOugXVAqcgEzj+13eeyJaFVrzwrNa67sh84nmbjOjNjvE==0zBq
57
58# Random number related
59random.c iQCVAwUAP7nsITEAnp832S/7AQK4SAQAtvfUgrtGOQ2PlxGMla0qJLPHjJacMwgq0ecusiI79elPdDsFfCCk6dK1Ug2kFbNm22nCGHNcUquqbX7noi7ZVQnmPBQXzyLNZd7GmrawRZfdlRerTUDBpSnR8V8ui/5+YYp627E7kKGC0hPSgqXFql6oBMIfno0LZwFJTjIevRY==L419
60random.h iQCVAwUAP7ovKDEAnp832S/7AQJ3bQQAjnPebnyTC7sphAv2I7uIz+yPgw1ZfbVhLv+OiWDlO9ish+fRyyMpy+HELBOgZjJdgRegqhlZC6qyns5arM/VglYi+PzvdLO3hIqHE/YFfpIFPz8wBrcmlqrYyd3CsGqcYsfjocXNttCBLeSWmoJ09ltKQH8yzJf3oAgN6X1yuc4==eNoU
61rand-internal.h iQCVAwUAP7ouvDEAnp832S/7AQLYnAQAhdI7ERoJVCkV8GiV7MjaUxv1WIL7iZ+jIOvVhv4fNyhCGCGoEtTjkyput/lj7Nsh3FXEqRhypGGrCLf47x/gua5n+BwffogxVyUDqiOyyGhNTPpe3fQcNBvbPCtco8yMK4GJO5G3BqzlPyN+BMeogLymyV6Sm1mvh5LZDyAFbfQ==tZSE
62rndlinux.c iQCVAwUAP9iPYTEAnp832S/7AQL6/AP/ZDrbOkVuB9qJ7sKeX1MImZEsz3mi0xPovJzaBtBU7a0idcUKrWYOvQFWRlLUeq0iCT6+h2l5bniP7q7hepzlKa+VPY9VWaQthqeJm2l5LN6QQ5PyMfBq04QuBncw9BJnCGmEyTLt3RxIXBAPdxmiVxtcRIFUqCBtQvoUXGLvemw==t37k
63rndegd.c iQCVAwUAP9iPRDEAnp832S/7AQImBQP/WHKg+hKXcm1pQvilzML0jZpwK5PAMM4uBnnPJNIXWOYBO6I/Xg9d/tPLg8NlmmtyQCo2Eu0ybDSt+8mu+dWveAys+0LTi0MIqeP9BMzCKz8dnWH6+S8huLXwTF3m0IrqM0JLb6b71GK9SOq6sWQ22yW5vf61hXP8kH9dhIaoMZs==FaHV
64rndunix.c iQCVAwUAP9iQlzEAnp832S/7AQL/KgQA29GnvcD4Xb5qjDMBgW9THEE4+4lfex/6k+Fh0IT61OLJsWVLJ7bJpRntburw4uQm4Tf7CO8vaiDFDYhKKrzXeOF1fmdpcL8hA+fNp9I/MUOc4e9kN9+YJ9wikVa0SZj1OBfhzgcFLd1xOtulkr3ii52HLF9vhrxzkgVwvD10Bi8==2cML
65rndw32.c iQCVAwUAP9iRKDEAnp832S/7AQIuaAQA3AJr3WqnxNDsWCIdvehf8Suotthj+laX8nJsvDfFhXPKcXDpsg0wTTXSnnKgyED53+uYiMDnVRsxeWAyhKwvx1MjjlaSMMjzbH6isWTH8FaWpLgrxEkXoPeNqYf5FXpdUkcUxGX2RkQeuX/cIfiHLNE9CV0usaF2jysjBX2iERY==EEnO
66
67# Helper
68bithelp.h iQCVAwUAP7ouPTEAnp832S/7AQKXggQAqjcgvihIF3WclOgw1JV2rbARw4ISIDRMFqdaNCqBRx6BwEz3UGsEIlz6+iR1sS/reqN61WvtjLb+D0+tujAkGrgQJhFLG85WtG2tB5UVoI3am1fpkwiRm+bR4rv0rGk0BYk81bC7+l4KrK9o5lVp4lCsrorlUKsd48lNmBHyAXM==mDDN
69rmd.h iQCVAwUAP7oumjEAnp832S/7AQJiJQP/V4bJwjZaYndJzV+KRnIDbl1koHuw+ZK5heMYVu8Qk4ylqv//BGyeRa3jZCcfPHI35q6HilCs2VBm8hiBMjHSqY/VPn2ZQ0yg/lt6qEvl7YjsLmyMICvjG+ncszHoq9pRvnF3vTnM18sPIioXLk8fskuM0XOCNBs0ARBAQjY9UGI==olUN
70
71# Configuration
72Makefile.am iQCVAwUAQCN33TEAnp832S/7AQKFJAQAz7BDkC814q+QiuE/jnutJHR5qlgbrm3ikGbQwdRzYUscst4bCCWy3uKL/sIPGLg+JQXtF5FnsQy3s4D9BOYhp72cA9ktYK65hhi4pNm/JQ0lXkZMNfk8Go5lNzKezlWwHvkMwRXR0Fep0wPdyeaKW5BfaW2ABvgep6Bp+hHEbyg==zSyi
73$names$ iQCVAwUAQCN3EDEAnp832S/7AQJXLAP8DvHTpm5DkTF35EmzeKpi9ie59AZcZanD19ir/e/7+PaQxr2riuLHDGwFKTju+dcvvBsqrygXOC378GXVWzIF2OZwS4EdDcJ+pgojo9UpsqpKsJHouY4Ugx5cQialxba462kUn8hcihSBnMyc4LzbJ5WQ4puQuqy544d2x94+2ms==G4Ls
diff --git a/pwmanager/libcrypt/cipher/ac.c b/pwmanager/libcrypt/cipher/ac.c
new file mode 100644
index 0000000..c18f243
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/ac.c
@@ -0,0 +1,1390 @@
1/* ac.c - Alternative interface for asymmetric cryptography.
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3
4 This file is part of Libgcrypt.
5
6 Libgcrypt is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser general Public License as
8 published by the Free Software Foundation; either version 2.1 of
9 the License, or (at your option) any later version.
10
11 Libgcrypt is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <config.h>
22#include <assert.h>
23#include <errno.h>
24#include <stdlib.h>
25#include <string.h>
26#include <stdio.h>
27#include <stddef.h>
28
29#include "g10lib.h"
30#include "cipher.h"
31
32
33
34/* At the moment the ac interface is a wrapper around the pk
35 interface, but this might change somewhen in the future, depending
36 on how much people prefer the ac interface. */
37
38/* Mapping of flag numbers to the according strings as it is expected
39 for S-expressions. */
40struct number_string
41{
42 int number;
43 const char *string;
44} gcry_ac_flags[] =
45 {
46 { GCRY_AC_FLAG_NO_BLINDING, "no-blinding" },
47 { 0, NULL },
48 };
49
50/* The positions in this list correspond to the values contained in
51 the gcry_ac_key_type_t enumeration list. */
52static const char *ac_key_identifiers[] =
53 {
54 "private-key",
55 "public-key",
56 };
57
58/* These specifications are needed for key-pair generation; the caller
59 is allowed to pass additional, algorithm-specific `specs' to
60 gcry_ac_key_pair_generate. This list is used for decoding the
61 provided values according to the selected algorithm. */
62struct gcry_ac_key_generate_spec
63{
64 int algorithm; /* Algorithm for which this flag is
65 relevant. */
66 const char *name; /* Name of this flag. */
67 size_t offset; /* Offset in the cipher-specific spec
68 structure at which the MPI value
69 associated with this flag is to be
70 found. */
71} gcry_ac_key_generate_specs[] =
72 {
73 { GCRY_AC_RSA, "rsa-use-e", offsetof (gcry_ac_key_spec_rsa_t, e) },
74 { 0 },
75 };
76
77/* Handle structure. */
78struct gcry_ac_handle
79{
80 int algorithm; /* Algorithm ID associated with this
81 handle. */
82 const char *algorithm_name;/* Name of the algorithm. */
83 unsigned int flags; /* Flags, not used yet. */
84 gcry_module_t module; /* Reference to the algorithm
85 module. */
86};
87
88/* A named MPI value. */
89typedef struct gcry_ac_mpi
90{
91 const char *name; /* Name of MPI value. */
92 gcry_mpi_t mpi; /* MPI value. */
93 unsigned int flags; /* Flags. */
94} gcry_ac_mpi_t;
95
96/* A data set, that is simply a list of named MPI values. */
97struct gcry_ac_data
98{
99 gcry_ac_mpi_t *data; /* List of named values. */
100 unsigned int data_n; /* Number of values in DATA. */
101};
102
103/* The key in `native' ac form and as an S-expression. */
104struct gcry_ac_key
105{
106 gcry_ac_data_t data; /* Data in native ac structure. */
107 gcry_sexp_t data_sexp;/* Data as an S-expression. */
108 gcry_ac_key_type_t type;/* Type of the key. */
109};
110
111/* Two keys. */
112struct gcry_ac_key_pair
113{
114 gcry_ac_key_t public;
115 gcry_ac_key_t secret;
116};
117
118
119
120/*
121 * Primitive functions for the manipulation of `data sets'.
122 */
123
124/* Create a copy of the data set DATA and store it in DATA_CP. */
125static gcry_err_code_t
126gcry_ac_data_copy_internal (gcry_ac_data_t *data_cp, gcry_ac_data_t data)
127{
128 gcry_err_code_t err = GPG_ERR_NO_ERROR;
129 gcry_ac_data_t data_new;
130 void *p = NULL;
131 int i = 0;
132
133 /* Allocate data set. */
134 err = _gcry_malloc (sizeof (struct gcry_ac_data), 0, &p);
135 data_new = p;
136 if (! err)
137 data_new->data_n = data->data_n;
138
139 if (! err)
140 /* Allocate space for named MPIs. */
141 err = _gcry_malloc (sizeof (gcry_ac_mpi_t) * data->data_n, 0,
142 (void **) &data_new->data);
143
144 if (! err)
145 {
146 /* Copy named MPIs. */
147
148 for (i = 0; i < data_new->data_n && (! err); i++)
149 {
150 data_new->data[i].name = NULL;
151 data_new->data[i].mpi = NULL;
152
153 /* Name. */
154 data_new->data[i].name = strdup (data->data[i].name);
155 if (! data_new->data[i].name)
156 err = gpg_err_code_from_errno (errno);
157
158 if (! err)
159 {
160 /* MPI. */
161 data_new->data[i].mpi = gcry_mpi_copy (data->data[i].mpi);
162 if (! data_new->data[i].mpi)
163 err = gpg_err_code_from_errno (errno);
164 }
165 }
166 }
167
168 if (! err)
169 {
170 /* Copy out. */
171 *data_cp = data_new;
172 }
173 else
174 {
175 /* Deallocate resources. */
176 if (data_new)
177 {
178 if (data_new->data)
179 {
180 for (; i >= 0; i--)
181 {
182 if (data_new->data[i].name)
183 free ((void *) data_new->data[i].name);
184 if (data_new->data[i].mpi)
185 gcry_mpi_release (data_new->data[i].mpi);
186 }
187 gcry_free (data_new->data);
188 }
189 gcry_free (data_new);
190 }
191 }
192
193 return err;
194}
195
196
197
198/*
199 * Functions for converting data between the native ac and the
200 * S-expression structure.
201 */
202
203/* Extract the S-Expression DATA_SEXP into DATA under the control of
204 TYPE and NAME. This function assumes that S-Expressions are of the
205 following structure:
206
207 (IDENTIFIER <data to be ignored>
208 (ALGORITHM <list of named MPI values>))
209
210 IDENTIFIER is one of `private-key', `public-key', `enc-val',
211 `sig-val'; ALGORITHM is the name of the algorithm used. */
212static gcry_err_code_t
213gcry_ac_data_extract (const char *identifier, const char *algorithm,
214 gcry_sexp_t data_sexp, gcry_ac_data_t *data)
215{
216 gcry_err_code_t err = GPG_ERR_NO_ERROR;
217 gcry_sexp_t data_element_sexp = NULL;
218 gcry_sexp_t inner_data_sexp = NULL;
219 size_t inner_data_n;
220
221 const char *name;
222 size_t name_n;
223
224 gcry_mpi_t data_elem_mpi = NULL;
225 char *data_elem_name = NULL;
226
227 gcry_ac_data_t data_new = NULL;
228
229 int i = 0;
230
231 /* Verify that the S-expression contains the correct identifier. */
232 name = gcry_sexp_nth_data (data_sexp, 0, &name_n);
233 if (! name)
234 err = GPG_ERR_INTERNAL;
235 else if (strncmp (identifier, name, name_n))
236 err = GPG_ERR_INTERNAL;
237
238 if (! err)
239 {
240 /* Extract inner S-expression. */
241 inner_data_sexp = gcry_sexp_find_token (data_sexp, algorithm, 0);
242 if (! inner_data_sexp)
243 err = GPG_ERR_INTERNAL;
244 else
245 /* Count data elements, this includes the name of the
246 algorithm. */
247 inner_data_n = gcry_sexp_length (inner_data_sexp);
248 }
249
250 if (! err)
251 {
252 /* Allocate new data set. */
253 data_new = gcry_malloc (sizeof (struct gcry_ac_data));
254 if (! data_new)
255 err = gpg_err_code_from_errno (errno);
256 else
257 {
258 data_new->data = gcry_malloc (sizeof (gcry_ac_mpi_t)
259 * (inner_data_n - 1));
260 if (! data_new->data)
261 err = gpg_err_code_from_errno (errno);
262 }
263 }
264
265 if (! err)
266 {
267 /* Iterate through list of data elements and add them to the
268 data set. */
269
270 for (i = 1; i < inner_data_n; i++)
271 {
272 data_new->data[i - 1].name = NULL;
273 data_new->data[i - 1].mpi = NULL;
274
275 /* Get the S-expression of the named MPI, that contains the
276 name and the MPI value. */
277 data_element_sexp = gcry_sexp_nth (inner_data_sexp, i);
278 if (! data_element_sexp)
279 err = GPG_ERR_INTERNAL;
280
281 if (! err)
282 {
283 /* Extract the name. */
284 name = gcry_sexp_nth_data (data_element_sexp, 0, &name_n);
285 if (! name)
286 err = GPG_ERR_INTERNAL;
287 }
288
289 if (! err)
290 {
291 /* Extract the MPI value. */
292 data_elem_mpi = gcry_sexp_nth_mpi (data_element_sexp, 1,
293 GCRYMPI_FMT_USG);
294 if (! data_elem_mpi)
295 err = GPG_ERR_INTERNAL;
296 }
297
298 if (! err)
299 {
300 /* Duplicate the name. */
301 data_elem_name = gcry_malloc (name_n + 1);
302 if (! data_elem_name)
303
304 err = gpg_err_code_from_errno (errno);
305 else
306 {
307 strncpy (data_elem_name, name, name_n);
308 data_elem_name[name_n] = 0;
309 }
310 }
311
312 /* Done. */
313
314 if (data_element_sexp)
315 gcry_sexp_release (data_element_sexp);
316
317 if (! err)
318 {
319 data_new->data[i - 1].name = data_elem_name;
320 data_new->data[i - 1].mpi = data_elem_mpi;
321 }
322 else
323 break;
324 }
325 }
326
327 if (! err)
328 {
329 /* Copy out. */
330 data_new->data_n = inner_data_n - 1;
331 *data = data_new;
332 }
333 else
334 {
335 /* Deallocate resources. */
336
337 if (data_new)
338 {
339 if (data_new->data)
340 {
341 int j;
342
343 for (j = 0; j < i - 1; j++)
344 {
345 if (data_new->data[j].name)
346 gcry_free ((void *) data_new->data[j].name);
347 if (data_new->data[j].mpi)
348 gcry_mpi_release (data_new->data[j].mpi);
349 }
350
351 gcry_free (data_new->data);
352 }
353 gcry_free (data_new);
354 }
355 }
356
357 return err;
358}
359
360/* Construct an S-expression from the DATA and store it in
361 DATA_SEXP. The S-expression will be of the following structure:
362
363 (IDENTIFIER [(flags [...])]
364 (ALGORITHM <list of named MPI values>)) */
365static gcry_err_code_t
366gcry_ac_data_construct (const char *identifier, int include_flags,
367 unsigned int flags, const char *algorithm,
368 gcry_ac_data_t data, gcry_sexp_t *data_sexp)
369{
370 gcry_err_code_t err = GPG_ERR_NO_ERROR;
371 void **arg_list = NULL;
372
373 gcry_sexp_t data_sexp_new = NULL;
374
375 size_t data_format_n = 0;
376 char *data_format = NULL;
377
378 int i;
379
380 /* We build a list of arguments to pass to
381 gcry_sexp_build_array(). */
382 arg_list = gcry_malloc (sizeof (void *) * data->data_n);
383 if (! arg_list)
384 err = gpg_err_code_from_errno (errno);
385 else
386 /* Fill list with MPIs. */
387 for (i = 0; i < data->data_n; i++)
388 arg_list[i] = (void *) &data->data[i].mpi;
389
390 if (! err)
391 {
392 /* Calculate size of format string. */
393
394 data_format_n = (5 + (include_flags ? 7 : 0)
395 + strlen (identifier) + strlen (algorithm));
396
397 for (i = 0; i < data->data_n; i++)
398 {
399 /* Per-element sizes. */
400 data_format_n += 4 + strlen (data->data[i].name);
401 }
402
403 if (include_flags)
404 {
405 /* Add flags. */
406 for (i = 0; gcry_ac_flags[i].number; i++)
407 if (flags & gcry_ac_flags[i].number)
408 data_format_n += strlen (gcry_ac_flags[i].string) + 1;
409 }
410
411 /* Done. */
412 data_format = gcry_malloc (data_format_n);
413 if (! data_format)
414 err = gpg_err_code_from_errno (errno);
415 }
416
417 if (! err)
418 {
419 /* Construct the format string. */
420
421 *data_format = 0;
422 strcat (data_format, "(");
423 strcat (data_format, identifier);
424 if (include_flags)
425 {
426 strcat (data_format, "(flags");
427 for (i = 0; gcry_ac_flags[i].number; i++)
428 if (flags & gcry_ac_flags[i].number)
429 {
430 strcat (data_format, " ");
431 strcat (data_format, gcry_ac_flags[i].string);
432 }
433 strcat (data_format, ")");
434 }
435 strcat (data_format, "(");
436 strcat (data_format, algorithm);
437 for (i = 0; i < data->data_n; i++)
438 {
439 strcat (data_format, "(");
440 strcat (data_format, data->data[i].name);
441 strcat (data_format, "%m)");
442 }
443 strcat (data_format, "))");
444
445 /* Create final S-expression. */
446 err = gcry_sexp_build_array (&data_sexp_new, NULL,
447 data_format, arg_list);
448 }
449
450 if (err)
451 {
452 /* Deallocate resources. */
453
454 if (arg_list)
455 gcry_free (arg_list);
456 if (data_format)
457 gcry_free (data_format);
458 if (data_sexp_new)
459 gcry_sexp_release (data_sexp_new);
460 }
461
462 else
463 /* Copy-out. */
464 *data_sexp = data_sexp_new;
465
466 return err;
467}
468
469
470
471/*
472 * Functions for working with data sets.
473 */
474
475/* Creates a new, empty data set and stores it in DATA. */
476gcry_error_t
477gcry_ac_data_new (gcry_ac_data_t *data)
478{
479 gcry_err_code_t err = GPG_ERR_NO_ERROR;
480 gcry_ac_data_t data_new = NULL;
481
482 data_new = gcry_malloc (sizeof (struct gcry_ac_data));
483 if (! data_new)
484 err = gpg_err_code_from_errno (errno);
485
486 if (! err)
487 {
488 data_new->data = NULL;
489 data_new->data_n = 0;
490 *data = data_new;
491 }
492
493 return gcry_error (err);
494}
495
496/* Destroys the data set DATA. */
497void
498gcry_ac_data_destroy (gcry_ac_data_t data)
499{
500 int i;
501
502 for (i = 0; i < data->data_n; i++)
503 {
504 gcry_free ((void *) data->data[i].name);
505 gcry_mpi_release (data->data[i].mpi);
506 }
507 gcry_free (data->data);
508 gcry_free (data);
509}
510
511/* Add the value MPI to DATA with the label NAME. If FLAGS contains
512 GCRY_AC_FLAG_DATA_COPY, the data set will contain copies of NAME
513 and MPI. If FLAGS contains GCRY_AC_FLAG_DATA_DEALLOC or
514 GCRY_AC_FLAG_DATA_COPY, the values contained in the data set will
515 be deallocated when they are to be removed from the data set. */
516gcry_error_t
517gcry_ac_data_set (gcry_ac_data_t data, unsigned int flags,
518 const char *name, gcry_mpi_t mpi)
519{
520 gcry_err_code_t err = GPG_ERR_NO_ERROR;
521 gcry_ac_mpi_t *ac_mpi = NULL;
522 gcry_mpi_t mpi_add = NULL;
523 char *name_add = NULL;
524 unsigned int i = 0;
525
526 if (flags & ~(GCRY_AC_FLAG_DEALLOC | GCRY_AC_FLAG_COPY))
527 err = GPG_ERR_INV_ARG;
528 else
529 {
530 if (flags & GCRY_AC_FLAG_COPY)
531 {
532 /* Create copies. */
533
534 name_add = strdup (name);
535 if (! name_add)
536 err = GPG_ERR_ENOMEM;
537 if (! err)
538 {
539 mpi_add = gcry_mpi_copy (mpi);
540 if (! mpi_add)
541 err = GPG_ERR_ENOMEM;
542 }
543 }
544 else
545 {
546 name_add = (char *) name;
547 mpi_add = mpi;
548 }
549
550 /* Search for existing entry. */
551 for (i = 0; (i < data->data_n) && (! ac_mpi); i++)
552 if (! strcmp (name, data->data[i].name))
553 ac_mpi = data->data + i;
554
555 if (ac_mpi)
556 {
557 /* An entry for NAME does already exist. */
558 if (ac_mpi->flags & GCRY_AC_FLAG_DEALLOC)
559 {
560 /* Deallocate old values. */
561 gcry_free ((char *) ac_mpi->name);
562 gcry_mpi_release (ac_mpi->mpi);
563 }
564 }
565 else
566 {
567 /* Create a new entry. */
568
569 gcry_ac_mpi_t *ac_mpis = NULL;
570
571 ac_mpis = realloc (data->data, sizeof (*data->data) * (data->data_n + 1));
572 if (! ac_mpis)
573 err = gpg_err_code_from_errno (errno);
574
575 if (data->data != ac_mpis)
576 data->data = ac_mpis;
577 ac_mpi = data->data + data->data_n;
578 data->data_n++;
579 }
580
581 ac_mpi->flags = flags;
582 ac_mpi->name = name_add;
583 ac_mpi->mpi = mpi_add;
584 }
585
586 return gcry_error (err);
587}
588
589/* Create a copy of the data set DATA and store it in DATA_CP. */
590gcry_error_t
591gcry_ac_data_copy (gcry_ac_data_t *data_cp, gcry_ac_data_t data)
592{
593 gcry_err_code_t err = GPG_ERR_NO_ERROR;
594
595 err = gcry_ac_data_copy_internal (data_cp, data);
596
597 return gcry_error (err);
598}
599
600/* Returns the number of named MPI values inside of the data set
601 DATA. */
602unsigned int
603gcry_ac_data_length (gcry_ac_data_t data)
604{
605 return data->data_n;
606}
607
608/* Store the value labelled with NAME found in DATA in MPI. If FLAGS
609 contains GCRY_AC_FLAG_COPY, store a copy of the MPI value contained
610 in the data set. MPI may be NULL. */
611gcry_error_t
612gcry_ac_data_get_name (gcry_ac_data_t data, unsigned int flags,
613 const char *name, gcry_mpi_t *mpi)
614{
615 gcry_err_code_t err = GPG_ERR_NO_DATA;
616 gcry_mpi_t mpi_found = NULL;
617 unsigned int i = 0;
618
619 if (flags & ~(GCRY_AC_FLAG_COPY))
620 err = GPG_ERR_INV_ARG;
621 else
622 {
623 for (i = 0; i < data->data_n && (! mpi_found); i++)
624 if (! strcmp (data->data[i].name, name))
625 {
626 if (flags & GCRY_AC_FLAG_COPY)
627 {
628 mpi_found = gcry_mpi_copy (data->data[i].mpi);
629 if (! mpi_found)
630 err = GPG_ERR_ENOMEM;
631 }
632 else
633 mpi_found = data->data[i].mpi;
634
635 if (mpi_found)
636 err = GPG_ERR_NO_ERROR;
637 }
638 }
639
640 if (! err)
641 if (mpi)
642 *mpi = mpi_found;
643
644 return gcry_error (err);
645}
646
647/* Stores in NAME and MPI the named MPI value contained in the data
648 set DATA with the index IDX. If FLAGS contains GCRY_AC_FLAG_COPY,
649 store copies of the values contained in the data set. NAME or MPI
650 may be NULL. */
651gcry_error_t
652gcry_ac_data_get_index (gcry_ac_data_t data, unsigned int flags, unsigned int idx,
653 const char **name, gcry_mpi_t *mpi)
654{
655 gcry_err_code_t err = GPG_ERR_NO_ERROR;
656 gcry_mpi_t mpi_return = NULL;
657 char *name_return = NULL;
658
659 if (flags & ~(GCRY_AC_FLAG_COPY))
660 err = GPG_ERR_INV_ARG;
661 else
662 {
663 if (idx < data->data_n)
664 {
665 if (flags & GCRY_AC_FLAG_COPY)
666 {
667 /* Return copies to the user. */
668 if (name)
669 name_return = strdup (data->data[idx].name);
670 if (mpi)
671 mpi_return = gcry_mpi_copy (data->data[idx].mpi);
672
673 if (! (name_return && mpi_return))
674 {
675 if (name_return)
676 free (name_return);
677 if (mpi_return)
678 gcry_mpi_release (mpi_return);
679 err = GPG_ERR_ENOMEM;
680 }
681 }
682 else
683 {
684 name_return = (char *) data->data[idx].name;
685 mpi_return = data->data[idx].mpi;
686 }
687 }
688 else
689 err = GPG_ERR_NO_DATA;
690 }
691
692 if (! err)
693 {
694 if (name)
695 *name = name_return;
696 if (mpi)
697 *mpi = mpi_return;
698 }
699
700 return gcry_error (err);
701}
702
703/* Destroys any values contained in the data set DATA. */
704void
705gcry_ac_data_clear (gcry_ac_data_t data)
706{
707 gcry_free (data->data);
708 data->data = NULL;
709 data->data_n = 0;
710}
711
712
713
714/*
715 * Handle management.
716 */
717
718/* Creates a new handle for the algorithm ALGORITHM and store it in
719 HANDLE. FLAGS is not used yet. */
720gcry_error_t
721gcry_ac_open (gcry_ac_handle_t *handle,
722 gcry_ac_id_t algorithm, unsigned int flags)
723{
724 gcry_err_code_t err = GPG_ERR_NO_ERROR;
725 gcry_module_t module = NULL;
726 gcry_ac_handle_t handle_new;
727 const char *algorithm_name;
728
729 *handle = NULL;
730
731 /* Get name. */
732 algorithm_name = _gcry_pk_aliased_algo_name (algorithm);
733 if (! *algorithm_name)
734 err = GPG_ERR_PUBKEY_ALGO;
735
736 if (! err) /* Acquire reference to the pubkey module. */
737 err = _gcry_pk_module_lookup (algorithm, &module);
738
739 if (! err)
740 {
741 /* Allocate. */
742 handle_new = gcry_malloc (sizeof (struct gcry_ac_handle));
743 if (! handle_new)
744 err = gpg_err_code_from_errno (errno);
745 }
746
747 if (! err)
748 {
749 /* Done. */
750 handle_new->algorithm = algorithm;
751 handle_new->algorithm_name = algorithm_name;
752 handle_new->flags = flags;
753 handle_new->module = module;
754 *handle = handle_new;
755 }
756 else
757 {
758 /* Deallocate resources. */
759 if (module)
760 _gcry_pk_module_release (module);
761 }
762
763 return gcry_error (err);
764}
765
766/* Destroys the handle HANDLE. */
767void
768gcry_ac_close (gcry_ac_handle_t handle)
769{
770 /* Release reference to pubkey module. */
771 if (handle)
772 {
773 _gcry_pk_module_release (handle->module);
774 gcry_free (handle);
775 }
776}
777
778
779
780/*
781 * Key management.
782 */
783
784/* Creates a new key of type TYPE, consisting of the MPI values
785 contained in the data set DATA and stores it in KEY. */
786gcry_error_t
787gcry_ac_key_init (gcry_ac_key_t *key,
788 gcry_ac_handle_t handle,
789 gcry_ac_key_type_t type,
790 gcry_ac_data_t data)
791{
792 gcry_err_code_t err = GPG_ERR_NO_ERROR;
793 gcry_ac_data_t data_new = NULL;
794 gcry_sexp_t data_sexp = NULL;
795 gcry_ac_key_t key_new = NULL;
796
797 /* Allocate. */
798 key_new = gcry_malloc (sizeof (struct gcry_ac_key));
799 if (! key_new)
800 err = gpg_err_code_from_errno (errno);
801
802 if (! err)
803 {
804 /* Create S-expression from data set. */
805 err = gcry_ac_data_construct (ac_key_identifiers[type], 0, 0,
806 handle->algorithm_name, data, &data_sexp);
807 }
808
809 if (! err)
810 {
811 /* Copy data set. */
812 err = gcry_ac_data_copy_internal (&data_new, data);
813 }
814
815 if (! err)
816 {
817 /* Done. */
818 key_new->data_sexp = data_sexp;
819 key_new->data = data_new;
820 key_new->type = type;
821 *key = key_new;
822 }
823 else
824 {
825 /* Deallocate resources. */
826 if (key_new)
827 gcry_free (key_new);
828 if (data_sexp)
829 gcry_sexp_release (data_sexp);
830 }
831
832 return gcry_error (err);
833}
834
835/* Generates a new key pair via the handle HANDLE of NBITS bits and
836 stores it in KEY_PAIR. In case non-standard settings are wanted, a
837 pointer to a structure of type gcry_ac_key_spec_<algorithm>_t,
838 matching the selected algorithm, can be given as KEY_SPEC.
839 MISC_DATA is not used yet. */
840gcry_error_t
841gcry_ac_key_pair_generate (gcry_ac_handle_t handle, unsigned int nbits, void *key_spec,
842 gcry_ac_key_pair_t *key_pair, gcry_mpi_t **misc_data)
843{
844 gcry_err_code_t err = GPG_ERR_NO_ERROR;
845
846 gcry_ac_key_pair_t key_pair_new = NULL;
847
848 gcry_sexp_t genkey_sexp_request = NULL;
849 gcry_sexp_t genkey_sexp_reply = NULL;
850
851 char *genkey_format = NULL;
852 size_t genkey_format_n = 0;
853
854 void **arg_list = NULL;
855 size_t arg_list_n = 0;
856
857 unsigned int i = 0;
858
859 /* Allocate key pair. */
860 key_pair_new = gcry_malloc (sizeof (struct gcry_ac_key_pair));
861 if (! key_pair_new)
862 err = gpg_err_code_from_errno (errno);
863
864 if (! err)
865 {
866 /* Allocate keys. */
867 key_pair_new->secret = gcry_malloc (sizeof (struct gcry_ac_key));
868 key_pair_new->public = gcry_malloc (sizeof (struct gcry_ac_key));
869
870 if (! (key_pair_new->secret || key_pair_new->public))
871 err = gpg_err_code_from_errno (errno);
872 else
873 {
874 key_pair_new->secret->type = GCRY_AC_KEY_SECRET;
875 key_pair_new->public->type = GCRY_AC_KEY_PUBLIC;
876 key_pair_new->secret->data_sexp = NULL;
877 key_pair_new->public->data_sexp = NULL;
878 key_pair_new->secret->data = NULL;
879 key_pair_new->public->data = NULL;
880 }
881 }
882
883 if (! err)
884 {
885 /* Calculate size of the format string, that is used for
886 creating the request S-expression. */
887 genkey_format_n = 23;
888
889 /* Respect any relevant algorithm specific commands. */
890 if (key_spec)
891 for (i = 0; gcry_ac_key_generate_specs[i].algorithm; i++)
892 if (handle->algorithm == gcry_ac_key_generate_specs[i].algorithm)
893 genkey_format_n += 6;
894
895 /* Create format string. */
896 genkey_format = gcry_malloc (genkey_format_n);
897 if (! genkey_format)
898 err = gpg_err_code_from_errno (errno);
899 else
900 {
901 /* Fill format string. */
902 *genkey_format = 0;
903 strcat (genkey_format, "(genkey(%s(nbits%d)");
904 if (key_spec)
905 for (i = 0; gcry_ac_key_generate_specs[i].algorithm; i++)
906 if (handle->algorithm == gcry_ac_key_generate_specs[i].algorithm)
907 strcat (genkey_format, "(%s%m)");
908 strcat (genkey_format, "))");
909 }
910 }
911
912 if (! err)
913 {
914 /* Build list of argument pointers, the algorithm name and the
915 nbits are needed always. */
916 arg_list_n = 2;
917
918 /* Now the algorithm specific arguments. */
919 if (key_spec)
920 for (i = 0; gcry_ac_key_generate_specs[i].algorithm; i++)
921 if (handle->algorithm == gcry_ac_key_generate_specs[i].algorithm)
922 arg_list_n += 2;
923
924 /* Allocate list. */
925 arg_list = gcry_malloc (sizeof (void *) * arg_list_n);
926 if (! arg_list)
927 err = gpg_err_code_from_errno (errno);
928 else
929 {
930 /* Fill argument list. */
931
932 int j;
933
934 arg_list[0] = (void *) &handle->algorithm_name;
935 arg_list[1] = (void *) &nbits;
936
937 if (key_spec)
938 for (j = 2, i = 0; gcry_ac_key_generate_specs[i].algorithm; i++)
939 if (handle->algorithm == gcry_ac_key_generate_specs[i].algorithm)
940 {
941 /* Add name of this specification flag and the
942 according member of the spec strucuture. */
943 arg_list[j++] = (void *)(&gcry_ac_key_generate_specs[i].name);
944 arg_list[j++] = (void *)
945 (((char *) key_spec)
946 + gcry_ac_key_generate_specs[i].offset);
947 }
948 }
949 }
950
951 if (! err)
952 /* Construct final request S-expression. */
953 err = gcry_err_code (gcry_sexp_build_array (&genkey_sexp_request, NULL,
954 genkey_format, arg_list));
955
956 if (! err)
957 /* Perform genkey operation. */
958 err = gcry_err_code (gcry_pk_genkey (&genkey_sexp_reply,
959 genkey_sexp_request));
960
961 /* Split keys. */
962 if (! err)
963 {
964 key_pair_new->secret->data_sexp = gcry_sexp_find_token (genkey_sexp_reply,
965 "private-key", 0);
966 if (! key_pair_new->secret->data_sexp)
967 err = GPG_ERR_INTERNAL;
968 }
969 if (! err)
970 {
971 key_pair_new->public->data_sexp = gcry_sexp_find_token (genkey_sexp_reply,
972 "public-key", 0);
973 if (! key_pair_new->public->data_sexp)
974 err = GPG_ERR_INTERNAL;
975 }
976
977 /* Extract key material. */
978 if (! err)
979 err = gcry_ac_data_extract ("private-key", handle->algorithm_name,
980 key_pair_new->secret->data_sexp,
981 &key_pair_new->secret->data);
982 if (! err)
983 err = gcry_ac_data_extract ("public-key", handle->algorithm_name,
984 key_pair_new->public->data_sexp,
985 &key_pair_new->public->data);
986
987 /* Done. */
988
989 if (! err)
990 *key_pair = key_pair_new;
991 else
992 {
993 /* Deallocate resources. */
994
995 if (key_pair_new)
996 {
997 if (key_pair_new->secret)
998 gcry_ac_key_destroy (key_pair_new->secret);
999 if (key_pair_new->public)
1000 gcry_ac_key_destroy (key_pair_new->public);
1001
1002 gcry_free (key_pair_new);
1003 }
1004
1005 if (arg_list)
1006 gcry_free (arg_list);
1007
1008 if (genkey_format)
1009 gcry_free (genkey_format);
1010
1011 if (genkey_sexp_request)
1012 gcry_sexp_release (genkey_sexp_request);
1013 if (genkey_sexp_reply)
1014 gcry_sexp_release (genkey_sexp_reply);
1015 }
1016
1017 return gcry_error (err);
1018}
1019
1020/* Returns the key of type WHICH out of the key pair KEY_PAIR. */
1021gcry_ac_key_t
1022gcry_ac_key_pair_extract (gcry_ac_key_pair_t key_pair,
1023 gcry_ac_key_type_t witch)
1024{
1025 gcry_ac_key_t key = NULL;
1026
1027 switch (witch)
1028 {
1029 case GCRY_AC_KEY_SECRET:
1030 key = key_pair->secret;
1031 break;
1032
1033 case GCRY_AC_KEY_PUBLIC:
1034 key = key_pair->public;
1035 break;
1036 }
1037
1038 return key;
1039}
1040
1041/* Destroys the key KEY. */
1042void
1043gcry_ac_key_destroy (gcry_ac_key_t key)
1044{
1045 int i;
1046
1047 if (key)
1048 {
1049 if (key->data)
1050 {
1051 for (i = 0; i < key->data->data_n; i++)
1052 if (key->data->data[i].mpi != NULL)
1053 gcry_mpi_release (key->data->data[i].mpi);
1054 gcry_free (key->data);
1055 }
1056 if (key->data_sexp)
1057 gcry_sexp_release (key->data_sexp);
1058 gcry_free (key);
1059 }
1060}
1061
1062/* Destroys the key pair KEY_PAIR. */
1063void
1064gcry_ac_key_pair_destroy (gcry_ac_key_pair_t key_pair)
1065{
1066 if (key_pair)
1067 {
1068 gcry_ac_key_destroy (key_pair->secret);
1069 gcry_ac_key_destroy (key_pair->public);
1070 gcry_free (key_pair);
1071 }
1072}
1073
1074/* Returns the data set contained in the key KEY. */
1075gcry_ac_data_t
1076gcry_ac_key_data_get (gcry_ac_key_t key)
1077{
1078 return key->data;
1079}
1080
1081/* Verifies that the key KEY is sane via HANDLE. */
1082gcry_error_t
1083gcry_ac_key_test (gcry_ac_handle_t handle, gcry_ac_key_t key)
1084{
1085 gcry_err_code_t err;
1086
1087 err = gcry_err_code (gcry_pk_testkey (key->data_sexp));
1088
1089 return gcry_error (err);
1090}
1091
1092/* Stores the number of bits of the key KEY in NBITS via HANDLE. */
1093gcry_error_t
1094gcry_ac_key_get_nbits (gcry_ac_handle_t handle, gcry_ac_key_t key, unsigned int *nbits)
1095{
1096 gcry_err_code_t err = GPG_ERR_NO_ERROR;
1097 unsigned int n;
1098
1099 n = gcry_pk_get_nbits (key->data_sexp);
1100 if (n)
1101 *nbits = n;
1102 else
1103 err = GPG_ERR_PUBKEY_ALGO;
1104
1105 return gcry_error (err);
1106}
1107
1108/* Writes the 20 byte long key grip of the key KEY to KEY_GRIP via
1109 HANDLE. */
1110gcry_error_t
1111gcry_ac_key_get_grip (gcry_ac_handle_t handle, gcry_ac_key_t key, unsigned char *key_grip)
1112{
1113 gcry_err_code_t err = GPG_ERR_NO_ERROR;
1114 unsigned char *ret;
1115
1116 ret = gcry_pk_get_keygrip (key->data_sexp, key_grip);
1117 if (! ret)
1118 err = GPG_ERR_INV_OBJ;
1119
1120 return gcry_error (err);
1121}
1122
1123
1124
1125/*
1126 * Functions performing cryptographic operations.
1127 */
1128
1129/* Encrypts the plain text MPI value DATA_PLAIN with the key public
1130 KEY under the control of the flags FLAGS and stores the resulting
1131 data set into DATA_ENCRYPTED. */
1132gcry_error_t
1133gcry_ac_data_encrypt (gcry_ac_handle_t handle,
1134 unsigned int flags,
1135 gcry_ac_key_t key,
1136 gcry_mpi_t data_plain,
1137 gcry_ac_data_t *data_encrypted)
1138{
1139 gcry_err_code_t err = GPG_ERR_NO_ERROR;
1140 gcry_sexp_t sexp_request = NULL;
1141 gcry_sexp_t sexp_reply = NULL;
1142 char *request_format = NULL;
1143 size_t request_format_n = 0;
1144 gcry_ac_data_t data;
1145
1146 int i;
1147
1148 if (key->type != GCRY_AC_KEY_PUBLIC)
1149 err = GPG_ERR_WRONG_KEY_USAGE;
1150
1151 if (! err)
1152 {
1153 /* Calculate request format string. */
1154
1155 request_format_n += 23;
1156 for (i = 0; gcry_ac_flags[i].number; i++)
1157 if (flags & gcry_ac_flags[i].number)
1158 request_format_n += strlen (gcry_ac_flags[i].string) + 1;
1159
1160 /* Allocate request format string. */
1161 request_format = gcry_malloc (request_format_n);
1162 if (! request_format)
1163 err = gpg_err_code_from_errno (errno);
1164 }
1165
1166 if (! err)
1167 {
1168 /* Fill format string. */
1169 *request_format = 0;
1170 strcat (request_format, "(data(flags");
1171 for (i = 0; gcry_ac_flags[i].number; i++)
1172 if (flags & gcry_ac_flags[i].number)
1173 {
1174 strcat (request_format, " ");
1175 strcat (request_format, gcry_ac_flags[i].string);
1176 }
1177 strcat (request_format, ")(value%m))");
1178
1179 /* Create S-expression. */
1180 err = gcry_sexp_build (&sexp_request, NULL,
1181 request_format, data_plain);
1182 }
1183
1184 if (! err)
1185 /* Encrypt. */
1186 err = gcry_pk_encrypt (&sexp_reply, sexp_request, key->data_sexp);
1187
1188 if (! err)
1189 /* Extract data. */
1190 err = gcry_ac_data_extract ("enc-val", handle->algorithm_name,
1191 sexp_reply, &data);
1192
1193 /* Deallocate resources. */
1194
1195 if (sexp_request)
1196 gcry_sexp_release (sexp_request);
1197 if (sexp_reply)
1198 gcry_sexp_release (sexp_reply);
1199
1200 if (! err)
1201 /* Copy out. */
1202 *data_encrypted = data;
1203
1204 return gcry_error (err);
1205}
1206
1207/* Decrypts the encrypted data contained in the data set
1208 DATA_ENCRYPTED with the secret key KEY under the control of the
1209 flags FLAGS and stores the resulting plain text MPI value in
1210 DATA_PLAIN. */
1211gcry_error_t
1212gcry_ac_data_decrypt (gcry_ac_handle_t handle,
1213 unsigned int flags,
1214 gcry_ac_key_t key,
1215 gcry_mpi_t *data_plain,
1216 gcry_ac_data_t data_encrypted)
1217{
1218 gcry_err_code_t err = GPG_ERR_NO_ERROR;
1219 gcry_mpi_t data_decrypted = NULL;
1220 gcry_sexp_t sexp_request = NULL;
1221 gcry_sexp_t sexp_reply = NULL;
1222
1223 if (key->type != GCRY_AC_KEY_SECRET)
1224 err = GPG_ERR_WRONG_KEY_USAGE;
1225
1226 if (! err)
1227 /* Create S-expression from data. */
1228 err = gcry_ac_data_construct ("enc-val", 1, flags, handle->algorithm_name,
1229 data_encrypted, &sexp_request);
1230
1231 if (! err)
1232 /* Decrypt. */
1233 err = gcry_pk_decrypt (&sexp_reply, sexp_request, key->data_sexp);
1234
1235 if (! err)
1236 {
1237 /* Extract plain text. */
1238
1239 gcry_sexp_t l;
1240
1241 l = gcry_sexp_find_token (sexp_reply, "value", 0);
1242 if (! l)
1243 err = GPG_ERR_GENERAL;
1244 else
1245 {
1246 data_decrypted = gcry_sexp_nth_mpi (l, 1, GCRYMPI_FMT_USG);
1247 if (! data_decrypted)
1248 err = GPG_ERR_GENERAL;
1249 gcry_sexp_release (l);
1250 }
1251 }
1252
1253 /* Done. */
1254
1255 if (err)
1256 {
1257 /* Deallocate resources. */
1258 if (sexp_request)
1259 gcry_sexp_release (sexp_request);
1260 if (sexp_reply)
1261 gcry_sexp_release (sexp_reply);
1262 }
1263 else
1264 *data_plain = data_decrypted;
1265
1266 return gcry_error (err);
1267
1268}
1269
1270/* Signs the data contained in DATA with the secret key KEY and stores
1271 the resulting signature data set in DATA_SIGNATURE. */
1272gcry_error_t
1273gcry_ac_data_sign (gcry_ac_handle_t handle,
1274 gcry_ac_key_t key,
1275 gcry_mpi_t data,
1276 gcry_ac_data_t *data_signature)
1277{
1278 gcry_err_code_t err = GPG_ERR_NO_ERROR;
1279 gcry_sexp_t sexp_request = NULL;
1280 gcry_sexp_t sexp_reply = NULL;
1281 gcry_ac_data_t ac_data;
1282
1283 if (key->type != GCRY_AC_KEY_SECRET)
1284 err = GPG_ERR_WRONG_KEY_USAGE;
1285
1286 if (! err)
1287 /* Create S-expression holding the data. */
1288 err = gcry_sexp_build (&sexp_request, NULL,
1289 "(data(flags)(value%m))", data);
1290 if (! err)
1291 /* Sign. */
1292 err = gcry_pk_sign (&sexp_reply, sexp_request, key->data_sexp);
1293
1294 if (! err)
1295 /* Extract data. */
1296 err = gcry_ac_data_extract ("sig-val", handle->algorithm_name,
1297 sexp_reply, &ac_data);
1298
1299 /* Done. */
1300
1301 if (sexp_request)
1302 gcry_sexp_release (sexp_request);
1303 if (sexp_reply)
1304 gcry_sexp_release (sexp_reply);
1305
1306 if (! err)
1307 *data_signature = ac_data;
1308
1309 return gcry_error (err);
1310}
1311
1312/* Verifies that the signature contained in the data set
1313 DATA_SIGNATURE is indeed the result of signing the data contained
1314 in DATA with the secret key belonging to the public key KEY. */
1315gcry_error_t
1316gcry_ac_data_verify (gcry_ac_handle_t handle,
1317 gcry_ac_key_t key,
1318 gcry_mpi_t data,
1319 gcry_ac_data_t data_signature)
1320{
1321 gcry_err_code_t err = GPG_ERR_NO_ERROR;
1322 gcry_sexp_t sexp_request = NULL;
1323 gcry_sexp_t sexp_data = NULL;
1324
1325 if (key->type != GCRY_AC_KEY_PUBLIC)
1326 err = GPG_ERR_WRONG_KEY_USAGE;
1327
1328 if (! err)
1329 /* Construct S-expression holding the signature data. */
1330 err = gcry_ac_data_construct ("sig-val", 1, 0, handle->algorithm_name,
1331 data_signature, &sexp_request);
1332
1333 if (! err)
1334 /* Construct S-expression holding the data. */
1335 err = gcry_sexp_build (&sexp_data, NULL,
1336 "(data(flags)(value%m))", data);
1337
1338 if (! err)
1339 /* Verify signature. */
1340 err = gcry_pk_verify (sexp_request, sexp_data, key->data_sexp);
1341
1342 /* Done. */
1343
1344 if (sexp_request)
1345 gcry_sexp_release (sexp_request);
1346 if (sexp_data)
1347 gcry_sexp_release (sexp_data);
1348
1349 return gcry_error (err);
1350}
1351
1352
1353
1354/*
1355 * General functions.
1356 */
1357
1358/* Stores the textual representation of the algorithm whose id is
1359 given in ALGORITHM in NAME. */
1360gcry_error_t
1361gcry_ac_id_to_name (gcry_ac_id_t algorithm, const char **name)
1362{
1363 gcry_err_code_t err = GPG_ERR_NO_ERROR;
1364 const char *n;
1365
1366 n = gcry_pk_algo_name (algorithm);
1367 if (*n)
1368 *name = n;
1369 else
1370 err = GPG_ERR_PUBKEY_ALGO;
1371
1372 return gcry_error (err);
1373}
1374
1375/* Stores the numeric ID of the algorithm whose textual representation
1376 is contained in NAME in ALGORITHM. */
1377gcry_error_t
1378gcry_ac_name_to_id (const char *name, gcry_ac_id_t *algorithm)
1379{
1380 gcry_err_code_t err = GPG_ERR_NO_ERROR;
1381 int algo;
1382
1383 algo = gcry_pk_map_name (name);
1384 if (algo)
1385 *algorithm = algo;
1386 else
1387 err = GPG_ERR_PUBKEY_ALGO;
1388
1389 return gcry_error (err);
1390}
diff --git a/pwmanager/libcrypt/cipher/arcfour.c b/pwmanager/libcrypt/cipher/arcfour.c
new file mode 100644
index 0000000..6bb0555
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/arcfour.c
@@ -0,0 +1,156 @@
1/* arcfour.c - The arcfour stream cipher
2 *Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 *
20 * For a description of the algorithm, see:
21 * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1996.
22 * ISBN 0-471-11709-9. Pages 397 ff.
23 */
24
25
26#include <config.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include "types.h"
31#include "g10lib.h"
32#include "cipher.h"
33
34static const char *selftest(void);
35
36typedef struct {
37 int idx_i, idx_j;
38 byte sbox[256];
39} ARCFOUR_context;
40
41static void
42do_encrypt_stream( ARCFOUR_context *ctx,
43 byte *outbuf, const byte *inbuf, unsigned int length )
44{
45 register int i = ctx->idx_i;
46 register int j = ctx->idx_j;
47 register byte *sbox = ctx->sbox;
48 register int t;
49
50 while ( length-- )
51 {
52 i++;
53 i = i & 255; /* The and-op seems to be faster than the mod-op. */
54 j += sbox[i];
55 j &= 255;
56 t = sbox[i]; sbox[i] = sbox[j]; sbox[j] = t;
57 *outbuf++ = *inbuf++ ^ sbox[(sbox[i] + sbox[j]) & 255];
58 }
59
60 ctx->idx_i = i;
61 ctx->idx_j = j;
62}
63
64static void
65encrypt_stream (void *context,
66 byte *outbuf, const byte *inbuf, unsigned int length)
67{
68 ARCFOUR_context *ctx = (ARCFOUR_context *) context;
69 do_encrypt_stream (ctx, outbuf, inbuf, length );
70 _gcry_burn_stack (64);
71}
72
73
74static gcry_err_code_t
75do_arcfour_setkey (void *context, const byte *key, unsigned int keylen)
76{
77 static int initialized;
78 static const char* selftest_failed;
79 int i, j;
80 byte karr[256];
81 ARCFOUR_context *ctx = (ARCFOUR_context *) context;
82
83 if (!initialized )
84 {
85 initialized = 1;
86 selftest_failed = selftest();
87 if( selftest_failed )
88 log_error ("ARCFOUR selftest failed (%s)\n", selftest_failed );
89 }
90 if( selftest_failed )
91 return GPG_ERR_SELFTEST_FAILED;
92
93 if( keylen < 40/8 ) /* we want at least 40 bits */
94 return GPG_ERR_INV_KEYLEN;
95
96 ctx->idx_i = ctx->idx_j = 0;
97 for (i=0; i < 256; i++ )
98 ctx->sbox[i] = i;
99 for (i=0; i < 256; i++ )
100 karr[i] = key[i%keylen];
101 for (i=j=0; i < 256; i++ )
102 {
103 int t;
104 j = (j + ctx->sbox[i] + karr[i]) % 256;
105 t = ctx->sbox[i];
106 ctx->sbox[i] = ctx->sbox[j];
107 ctx->sbox[j] = t;
108 }
109 memset( karr, 0, 256 );
110
111 return GPG_ERR_NO_ERROR;
112}
113
114static gcry_err_code_t
115arcfour_setkey ( void *context, const byte *key, unsigned int keylen )
116{
117 ARCFOUR_context *ctx = (ARCFOUR_context *) context;
118 gcry_err_code_t rc = do_arcfour_setkey (ctx, key, keylen );
119 _gcry_burn_stack (300);
120 return rc;
121}
122
123
124static const char*
125selftest(void)
126{
127 ARCFOUR_context ctx;
128 byte scratch[16];
129
130 /* Test vector from Cryptlib labeled there: "from the
131 State/Commerce Department". */
132 static byte key_1[] =
133 { 0x61, 0x8A, 0x63, 0xD2, 0xFB };
134 static byte plaintext_1[] =
135 { 0xDC, 0xEE, 0x4C, 0xF9, 0x2C };
136 static const byte ciphertext_1[] =
137 { 0xF1, 0x38, 0x29, 0xC9, 0xDE };
138
139 arcfour_setkey( &ctx, key_1, sizeof(key_1));
140 encrypt_stream( &ctx, scratch, plaintext_1, sizeof(plaintext_1));
141 if ( memcmp (scratch, ciphertext_1, sizeof (ciphertext_1)))
142 return "Arcfour encryption test 1 failed.";
143 arcfour_setkey( &ctx, key_1, sizeof(key_1));
144 encrypt_stream(&ctx, scratch, scratch, sizeof(plaintext_1)); /* decrypt */
145 if ( memcmp (scratch, plaintext_1, sizeof (plaintext_1)))
146 return "Arcfour decryption test 1 failed.";
147 return NULL;
148}
149
150
151gcry_cipher_spec_t _gcry_cipher_spec_arcfour =
152 {
153 "ARCFOUR", NULL, NULL, 1, 128, sizeof (ARCFOUR_context),
154 arcfour_setkey, NULL, NULL, encrypt_stream, encrypt_stream,
155 };
156
diff --git a/pwmanager/libcrypt/cipher/bithelp.h b/pwmanager/libcrypt/cipher/bithelp.h
new file mode 100644
index 0000000..1505324
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/bithelp.h
@@ -0,0 +1,54 @@
1/* bithelp.h - Some bit manipulation helpers
2 *Copyright (C) 1999, 2002 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20#ifndef G10_BITHELP_H
21#define G10_BITHELP_H
22
23
24/****************
25 * Rotate the 32 bit unsigned integer X by N bits left/right
26 */
27#if defined(__GNUC__) && defined(__i386__)
28static inline u32
29rol( u32 x, int n)
30{
31 __asm__("roll %%cl,%0"
32 :"=r" (x)
33 :"0" (x),"c" (n));
34 return x;
35}
36#else
37#define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )
38#endif
39
40#if defined(__GNUC__) && defined(__i386__)
41static inline u32
42ror(u32 x, int n)
43{
44 __asm__("rorl %%cl,%0"
45 :"=r" (x)
46 :"0" (x),"c" (n));
47 return x;
48}
49#else
50#define ror(x,n) ( ((x) >> (n)) | ((x) << (32-(n))) )
51#endif
52
53
54#endif /*G10_BITHELP_H*/
diff --git a/pwmanager/libcrypt/cipher/blowfish.c b/pwmanager/libcrypt/cipher/blowfish.c
new file mode 100644
index 0000000..66b2e8e
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/blowfish.c
@@ -0,0 +1,605 @@
1/* blowfish.c - Blowfish encryption
2 *Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 *
20 * For a description of the algorithm, see:
21 * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1996.
22 * ISBN 0-471-11709-9. Pages 336 ff.
23 */
24
25/* Test values:
26 * key "abcdefghijklmnopqrstuvwxyz";
27 * plain "BLOWFISH"
28 * cipher 32 4E D0 FE F4 13 A2 03
29 *
30 */
31
32#include <config.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <assert.h>
37#include "types.h"
38#include "g10lib.h"
39#include "cipher.h"
40
41#define BLOWFISH_BLOCKSIZE 8
42#define BLOWFISH_ROUNDS 16
43
44typedef struct {
45 u32 s0[256];
46 u32 s1[256];
47 u32 s2[256];
48 u32 s3[256];
49 u32 p[BLOWFISH_ROUNDS+2];
50} BLOWFISH_context;
51
52static gcry_err_code_t bf_setkey (void *c, const byte *key, unsigned keylen);
53static void encrypt_block (void *bc, byte *outbuf, const byte *inbuf);
54static void decrypt_block (void *bc, byte *outbuf, const byte *inbuf);
55
56
57/* precomputed S boxes */
58static const u32 ks0[256] = {
59 0xD1310BA6,0x98DFB5AC,0x2FFD72DB,0xD01ADFB7,0xB8E1AFED,0x6A267E96,
60 0xBA7C9045,0xF12C7F99,0x24A19947,0xB3916CF7,0x0801F2E2,0x858EFC16,
61 0x636920D8,0x71574E69,0xA458FEA3,0xF4933D7E,0x0D95748F,0x728EB658,
62 0x718BCD58,0x82154AEE,0x7B54A41D,0xC25A59B5,0x9C30D539,0x2AF26013,
63 0xC5D1B023,0x286085F0,0xCA417918,0xB8DB38EF,0x8E79DCB0,0x603A180E,
64 0x6C9E0E8B,0xB01E8A3E,0xD71577C1,0xBD314B27,0x78AF2FDA,0x55605C60,
65 0xE65525F3,0xAA55AB94,0x57489862,0x63E81440,0x55CA396A,0x2AAB10B6,
66 0xB4CC5C34,0x1141E8CE,0xA15486AF,0x7C72E993,0xB3EE1411,0x636FBC2A,
67 0x2BA9C55D,0x741831F6,0xCE5C3E16,0x9B87931E,0xAFD6BA33,0x6C24CF5C,
68 0x7A325381,0x28958677,0x3B8F4898,0x6B4BB9AF,0xC4BFE81B,0x66282193,
69 0x61D809CC,0xFB21A991,0x487CAC60,0x5DEC8032,0xEF845D5D,0xE98575B1,
70 0xDC262302,0xEB651B88,0x23893E81,0xD396ACC5,0x0F6D6FF3,0x83F44239,
71 0x2E0B4482,0xA4842004,0x69C8F04A,0x9E1F9B5E,0x21C66842,0xF6E96C9A,
72 0x670C9C61,0xABD388F0,0x6A51A0D2,0xD8542F68,0x960FA728,0xAB5133A3,
73 0x6EEF0B6C,0x137A3BE4,0xBA3BF050,0x7EFB2A98,0xA1F1651D,0x39AF0176,
74 0x66CA593E,0x82430E88,0x8CEE8619,0x456F9FB4,0x7D84A5C3,0x3B8B5EBE,
75 0xE06F75D8,0x85C12073,0x401A449F,0x56C16AA6,0x4ED3AA62,0x363F7706,
76 0x1BFEDF72,0x429B023D,0x37D0D724,0xD00A1248,0xDB0FEAD3,0x49F1C09B,
77 0x075372C9,0x80991B7B,0x25D479D8,0xF6E8DEF7,0xE3FE501A,0xB6794C3B,
78 0x976CE0BD,0x04C006BA,0xC1A94FB6,0x409F60C4,0x5E5C9EC2,0x196A2463,
79 0x68FB6FAF,0x3E6C53B5,0x1339B2EB,0x3B52EC6F,0x6DFC511F,0x9B30952C,
80 0xCC814544,0xAF5EBD09,0xBEE3D004,0xDE334AFD,0x660F2807,0x192E4BB3,
81 0xC0CBA857,0x45C8740F,0xD20B5F39,0xB9D3FBDB,0x5579C0BD,0x1A60320A,
82 0xD6A100C6,0x402C7279,0x679F25FE,0xFB1FA3CC,0x8EA5E9F8,0xDB3222F8,
83 0x3C7516DF,0xFD616B15,0x2F501EC8,0xAD0552AB,0x323DB5FA,0xFD238760,
84 0x53317B48,0x3E00DF82,0x9E5C57BB,0xCA6F8CA0,0x1A87562E,0xDF1769DB,
85 0xD542A8F6,0x287EFFC3,0xAC6732C6,0x8C4F5573,0x695B27B0,0xBBCA58C8,
86 0xE1FFA35D,0xB8F011A0,0x10FA3D98,0xFD2183B8,0x4AFCB56C,0x2DD1D35B,
87 0x9A53E479,0xB6F84565,0xD28E49BC,0x4BFB9790,0xE1DDF2DA,0xA4CB7E33,
88 0x62FB1341,0xCEE4C6E8,0xEF20CADA,0x36774C01,0xD07E9EFE,0x2BF11FB4,
89 0x95DBDA4D,0xAE909198,0xEAAD8E71,0x6B93D5A0,0xD08ED1D0,0xAFC725E0,
90 0x8E3C5B2F,0x8E7594B7,0x8FF6E2FB,0xF2122B64,0x8888B812,0x900DF01C,
91 0x4FAD5EA0,0x688FC31C,0xD1CFF191,0xB3A8C1AD,0x2F2F2218,0xBE0E1777,
92 0xEA752DFE,0x8B021FA1,0xE5A0CC0F,0xB56F74E8,0x18ACF3D6,0xCE89E299,
93 0xB4A84FE0,0xFD13E0B7,0x7CC43B81,0xD2ADA8D9,0x165FA266,0x80957705,
94 0x93CC7314,0x211A1477,0xE6AD2065,0x77B5FA86,0xC75442F5,0xFB9D35CF,
95 0xEBCDAF0C,0x7B3E89A0,0xD6411BD3,0xAE1E7E49,0x00250E2D,0x2071B35E,
96 0x226800BB,0x57B8E0AF,0x2464369B,0xF009B91E,0x5563911D,0x59DFA6AA,
97 0x78C14389,0xD95A537F,0x207D5BA2,0x02E5B9C5,0x83260376,0x6295CFA9,
98 0x11C81968,0x4E734A41,0xB3472DCA,0x7B14A94A,0x1B510052,0x9A532915,
99 0xD60F573F,0xBC9BC6E4,0x2B60A476,0x81E67400,0x08BA6FB5,0x571BE91F,
100 0xF296EC6B,0x2A0DD915,0xB6636521,0xE7B9F9B6,0xFF34052E,0xC5855664,
101 0x53B02D5D,0xA99F8FA1,0x08BA4799,0x6E85076A };
102
103static const u32 ks1[256] = {
104 0x4B7A70E9,0xB5B32944,0xDB75092E,0xC4192623,0xAD6EA6B0,0x49A7DF7D,
105 0x9CEE60B8,0x8FEDB266,0xECAA8C71,0x699A17FF,0x5664526C,0xC2B19EE1,
106 0x193602A5,0x75094C29,0xA0591340,0xE4183A3E,0x3F54989A,0x5B429D65,
107 0x6B8FE4D6,0x99F73FD6,0xA1D29C07,0xEFE830F5,0x4D2D38E6,0xF0255DC1,
108 0x4CDD2086,0x8470EB26,0x6382E9C6,0x021ECC5E,0x09686B3F,0x3EBAEFC9,
109 0x3C971814,0x6B6A70A1,0x687F3584,0x52A0E286,0xB79C5305,0xAA500737,
110 0x3E07841C,0x7FDEAE5C,0x8E7D44EC,0x5716F2B8,0xB03ADA37,0xF0500C0D,
111 0xF01C1F04,0x0200B3FF,0xAE0CF51A,0x3CB574B2,0x25837A58,0xDC0921BD,
112 0xD19113F9,0x7CA92FF6,0x94324773,0x22F54701,0x3AE5E581,0x37C2DADC,
113 0xC8B57634,0x9AF3DDA7,0xA9446146,0x0FD0030E,0xECC8C73E,0xA4751E41,
114 0xE238CD99,0x3BEA0E2F,0x3280BBA1,0x183EB331,0x4E548B38,0x4F6DB908,
115 0x6F420D03,0xF60A04BF,0x2CB81290,0x24977C79,0x5679B072,0xBCAF89AF,
116 0xDE9A771F,0xD9930810,0xB38BAE12,0xDCCF3F2E,0x5512721F,0x2E6B7124,
117 0x501ADDE6,0x9F84CD87,0x7A584718,0x7408DA17,0xBC9F9ABC,0xE94B7D8C,
118 0xEC7AEC3A,0xDB851DFA,0x63094366,0xC464C3D2,0xEF1C1847,0x3215D908,
119 0xDD433B37,0x24C2BA16,0x12A14D43,0x2A65C451,0x50940002,0x133AE4DD,
120 0x71DFF89E,0x10314E55,0x81AC77D6,0x5F11199B,0x043556F1,0xD7A3C76B,
121 0x3C11183B,0x5924A509,0xF28FE6ED,0x97F1FBFA,0x9EBABF2C,0x1E153C6E,
122 0x86E34570,0xEAE96FB1,0x860E5E0A,0x5A3E2AB3,0x771FE71C,0x4E3D06FA,
123 0x2965DCB9,0x99E71D0F,0x803E89D6,0x5266C825,0x2E4CC978,0x9C10B36A,
124 0xC6150EBA,0x94E2EA78,0xA5FC3C53,0x1E0A2DF4,0xF2F74EA7,0x361D2B3D,
125 0x1939260F,0x19C27960,0x5223A708,0xF71312B6,0xEBADFE6E,0xEAC31F66,
126 0xE3BC4595,0xA67BC883,0xB17F37D1,0x018CFF28,0xC332DDEF,0xBE6C5AA5,
127 0x65582185,0x68AB9802,0xEECEA50F,0xDB2F953B,0x2AEF7DAD,0x5B6E2F84,
128 0x1521B628,0x29076170,0xECDD4775,0x619F1510,0x13CCA830,0xEB61BD96,
129 0x0334FE1E,0xAA0363CF,0xB5735C90,0x4C70A239,0xD59E9E0B,0xCBAADE14,
130 0xEECC86BC,0x60622CA7,0x9CAB5CAB,0xB2F3846E,0x648B1EAF,0x19BDF0CA,
131 0xA02369B9,0x655ABB50,0x40685A32,0x3C2AB4B3,0x319EE9D5,0xC021B8F7,
132 0x9B540B19,0x875FA099,0x95F7997E,0x623D7DA8,0xF837889A,0x97E32D77,
133 0x11ED935F,0x16681281,0x0E358829,0xC7E61FD6,0x96DEDFA1,0x7858BA99,
134 0x57F584A5,0x1B227263,0x9B83C3FF,0x1AC24696,0xCDB30AEB,0x532E3054,
135 0x8FD948E4,0x6DBC3128,0x58EBF2EF,0x34C6FFEA,0xFE28ED61,0xEE7C3C73,
136 0x5D4A14D9,0xE864B7E3,0x42105D14,0x203E13E0,0x45EEE2B6,0xA3AAABEA,
137 0xDB6C4F15,0xFACB4FD0,0xC742F442,0xEF6ABBB5,0x654F3B1D,0x41CD2105,
138 0xD81E799E,0x86854DC7,0xE44B476A,0x3D816250,0xCF62A1F2,0x5B8D2646,
139 0xFC8883A0,0xC1C7B6A3,0x7F1524C3,0x69CB7492,0x47848A0B,0x5692B285,
140 0x095BBF00,0xAD19489D,0x1462B174,0x23820E00,0x58428D2A,0x0C55F5EA,
141 0x1DADF43E,0x233F7061,0x3372F092,0x8D937E41,0xD65FECF1,0x6C223BDB,
142 0x7CDE3759,0xCBEE7460,0x4085F2A7,0xCE77326E,0xA6078084,0x19F8509E,
143 0xE8EFD855,0x61D99735,0xA969A7AA,0xC50C06C2,0x5A04ABFC,0x800BCADC,
144 0x9E447A2E,0xC3453484,0xFDD56705,0x0E1E9EC9,0xDB73DBD3,0x105588CD,
145 0x675FDA79,0xE3674340,0xC5C43465,0x713E38D8,0x3D28F89E,0xF16DFF20,
146 0x153E21E7,0x8FB03D4A,0xE6E39F2B,0xDB83ADF7 };
147
148static const u32 ks2[256] = {
149 0xE93D5A68,0x948140F7,0xF64C261C,0x94692934,0x411520F7,0x7602D4F7,
150 0xBCF46B2E,0xD4A20068,0xD4082471,0x3320F46A,0x43B7D4B7,0x500061AF,
151 0x1E39F62E,0x97244546,0x14214F74,0xBF8B8840,0x4D95FC1D,0x96B591AF,
152 0x70F4DDD3,0x66A02F45,0xBFBC09EC,0x03BD9785,0x7FAC6DD0,0x31CB8504,
153 0x96EB27B3,0x55FD3941,0xDA2547E6,0xABCA0A9A,0x28507825,0x530429F4,
154 0x0A2C86DA,0xE9B66DFB,0x68DC1462,0xD7486900,0x680EC0A4,0x27A18DEE,
155 0x4F3FFEA2,0xE887AD8C,0xB58CE006,0x7AF4D6B6,0xAACE1E7C,0xD3375FEC,
156 0xCE78A399,0x406B2A42,0x20FE9E35,0xD9F385B9,0xEE39D7AB,0x3B124E8B,
157 0x1DC9FAF7,0x4B6D1856,0x26A36631,0xEAE397B2,0x3A6EFA74,0xDD5B4332,
158 0x6841E7F7,0xCA7820FB,0xFB0AF54E,0xD8FEB397,0x454056AC,0xBA489527,
159 0x55533A3A,0x20838D87,0xFE6BA9B7,0xD096954B,0x55A867BC,0xA1159A58,
160 0xCCA92963,0x99E1DB33,0xA62A4A56,0x3F3125F9,0x5EF47E1C,0x9029317C,
161 0xFDF8E802,0x04272F70,0x80BB155C,0x05282CE3,0x95C11548,0xE4C66D22,
162 0x48C1133F,0xC70F86DC,0x07F9C9EE,0x41041F0F,0x404779A4,0x5D886E17,
163 0x325F51EB,0xD59BC0D1,0xF2BCC18F,0x41113564,0x257B7834,0x602A9C60,
164 0xDFF8E8A3,0x1F636C1B,0x0E12B4C2,0x02E1329E,0xAF664FD1,0xCAD18115,
165 0x6B2395E0,0x333E92E1,0x3B240B62,0xEEBEB922,0x85B2A20E,0xE6BA0D99,
166 0xDE720C8C,0x2DA2F728,0xD0127845,0x95B794FD,0x647D0862,0xE7CCF5F0,
167 0x5449A36F,0x877D48FA,0xC39DFD27,0xF33E8D1E,0x0A476341,0x992EFF74,
168 0x3A6F6EAB,0xF4F8FD37,0xA812DC60,0xA1EBDDF8,0x991BE14C,0xDB6E6B0D,
169 0xC67B5510,0x6D672C37,0x2765D43B,0xDCD0E804,0xF1290DC7,0xCC00FFA3,
170 0xB5390F92,0x690FED0B,0x667B9FFB,0xCEDB7D9C,0xA091CF0B,0xD9155EA3,
171 0xBB132F88,0x515BAD24,0x7B9479BF,0x763BD6EB,0x37392EB3,0xCC115979,
172 0x8026E297,0xF42E312D,0x6842ADA7,0xC66A2B3B,0x12754CCC,0x782EF11C,
173 0x6A124237,0xB79251E7,0x06A1BBE6,0x4BFB6350,0x1A6B1018,0x11CAEDFA,
174 0x3D25BDD8,0xE2E1C3C9,0x44421659,0x0A121386,0xD90CEC6E,0xD5ABEA2A,
175 0x64AF674E,0xDA86A85F,0xBEBFE988,0x64E4C3FE,0x9DBC8057,0xF0F7C086,
176 0x60787BF8,0x6003604D,0xD1FD8346,0xF6381FB0,0x7745AE04,0xD736FCCC,
177 0x83426B33,0xF01EAB71,0xB0804187,0x3C005E5F,0x77A057BE,0xBDE8AE24,
178 0x55464299,0xBF582E61,0x4E58F48F,0xF2DDFDA2,0xF474EF38,0x8789BDC2,
179 0x5366F9C3,0xC8B38E74,0xB475F255,0x46FCD9B9,0x7AEB2661,0x8B1DDF84,
180 0x846A0E79,0x915F95E2,0x466E598E,0x20B45770,0x8CD55591,0xC902DE4C,
181 0xB90BACE1,0xBB8205D0,0x11A86248,0x7574A99E,0xB77F19B6,0xE0A9DC09,
182 0x662D09A1,0xC4324633,0xE85A1F02,0x09F0BE8C,0x4A99A025,0x1D6EFE10,
183 0x1AB93D1D,0x0BA5A4DF,0xA186F20F,0x2868F169,0xDCB7DA83,0x573906FE,
184 0xA1E2CE9B,0x4FCD7F52,0x50115E01,0xA70683FA,0xA002B5C4,0x0DE6D027,
185 0x9AF88C27,0x773F8641,0xC3604C06,0x61A806B5,0xF0177A28,0xC0F586E0,
186 0x006058AA,0x30DC7D62,0x11E69ED7,0x2338EA63,0x53C2DD94,0xC2C21634,
187 0xBBCBEE56,0x90BCB6DE,0xEBFC7DA1,0xCE591D76,0x6F05E409,0x4B7C0188,
188 0x39720A3D,0x7C927C24,0x86E3725F,0x724D9DB9,0x1AC15BB4,0xD39EB8FC,
189 0xED545578,0x08FCA5B5,0xD83D7CD3,0x4DAD0FC4,0x1E50EF5E,0xB161E6F8,
190 0xA28514D9,0x6C51133C,0x6FD5C7E7,0x56E14EC4,0x362ABFCE,0xDDC6C837,
191 0xD79A3234,0x92638212,0x670EFA8E,0x406000E0 };
192
193static const u32 ks3[256] = {
194 0x3A39CE37,0xD3FAF5CF,0xABC27737,0x5AC52D1B,0x5CB0679E,0x4FA33742,
195 0xD3822740,0x99BC9BBE,0xD5118E9D,0xBF0F7315,0xD62D1C7E,0xC700C47B,
196 0xB78C1B6B,0x21A19045,0xB26EB1BE,0x6A366EB4,0x5748AB2F,0xBC946E79,
197 0xC6A376D2,0x6549C2C8,0x530FF8EE,0x468DDE7D,0xD5730A1D,0x4CD04DC6,
198 0x2939BBDB,0xA9BA4650,0xAC9526E8,0xBE5EE304,0xA1FAD5F0,0x6A2D519A,
199 0x63EF8CE2,0x9A86EE22,0xC089C2B8,0x43242EF6,0xA51E03AA,0x9CF2D0A4,
200 0x83C061BA,0x9BE96A4D,0x8FE51550,0xBA645BD6,0x2826A2F9,0xA73A3AE1,
201 0x4BA99586,0xEF5562E9,0xC72FEFD3,0xF752F7DA,0x3F046F69,0x77FA0A59,
202 0x80E4A915,0x87B08601,0x9B09E6AD,0x3B3EE593,0xE990FD5A,0x9E34D797,
203 0x2CF0B7D9,0x022B8B51,0x96D5AC3A,0x017DA67D,0xD1CF3ED6,0x7C7D2D28,
204 0x1F9F25CF,0xADF2B89B,0x5AD6B472,0x5A88F54C,0xE029AC71,0xE019A5E6,
205 0x47B0ACFD,0xED93FA9B,0xE8D3C48D,0x283B57CC,0xF8D56629,0x79132E28,
206 0x785F0191,0xED756055,0xF7960E44,0xE3D35E8C,0x15056DD4,0x88F46DBA,
207 0x03A16125,0x0564F0BD,0xC3EB9E15,0x3C9057A2,0x97271AEC,0xA93A072A,
208 0x1B3F6D9B,0x1E6321F5,0xF59C66FB,0x26DCF319,0x7533D928,0xB155FDF5,
209 0x03563482,0x8ABA3CBB,0x28517711,0xC20AD9F8,0xABCC5167,0xCCAD925F,
210 0x4DE81751,0x3830DC8E,0x379D5862,0x9320F991,0xEA7A90C2,0xFB3E7BCE,
211 0x5121CE64,0x774FBE32,0xA8B6E37E,0xC3293D46,0x48DE5369,0x6413E680,
212 0xA2AE0810,0xDD6DB224,0x69852DFD,0x09072166,0xB39A460A,0x6445C0DD,
213 0x586CDECF,0x1C20C8AE,0x5BBEF7DD,0x1B588D40,0xCCD2017F,0x6BB4E3BB,
214 0xDDA26A7E,0x3A59FF45,0x3E350A44,0xBCB4CDD5,0x72EACEA8,0xFA6484BB,
215 0x8D6612AE,0xBF3C6F47,0xD29BE463,0x542F5D9E,0xAEC2771B,0xF64E6370,
216 0x740E0D8D,0xE75B1357,0xF8721671,0xAF537D5D,0x4040CB08,0x4EB4E2CC,
217 0x34D2466A,0x0115AF84,0xE1B00428,0x95983A1D,0x06B89FB4,0xCE6EA048,
218 0x6F3F3B82,0x3520AB82,0x011A1D4B,0x277227F8,0x611560B1,0xE7933FDC,
219 0xBB3A792B,0x344525BD,0xA08839E1,0x51CE794B,0x2F32C9B7,0xA01FBAC9,
220 0xE01CC87E,0xBCC7D1F6,0xCF0111C3,0xA1E8AAC7,0x1A908749,0xD44FBD9A,
221 0xD0DADECB,0xD50ADA38,0x0339C32A,0xC6913667,0x8DF9317C,0xE0B12B4F,
222 0xF79E59B7,0x43F5BB3A,0xF2D519FF,0x27D9459C,0xBF97222C,0x15E6FC2A,
223 0x0F91FC71,0x9B941525,0xFAE59361,0xCEB69CEB,0xC2A86459,0x12BAA8D1,
224 0xB6C1075E,0xE3056A0C,0x10D25065,0xCB03A442,0xE0EC6E0E,0x1698DB3B,
225 0x4C98A0BE,0x3278E964,0x9F1F9532,0xE0D392DF,0xD3A0342B,0x8971F21E,
226 0x1B0A7441,0x4BA3348C,0xC5BE7120,0xC37632D8,0xDF359F8D,0x9B992F2E,
227 0xE60B6F47,0x0FE3F11D,0xE54CDA54,0x1EDAD891,0xCE6279CF,0xCD3E7E6F,
228 0x1618B166,0xFD2C1D05,0x848FD2C5,0xF6FB2299,0xF523F357,0xA6327623,
229 0x93A83531,0x56CCCD02,0xACF08162,0x5A75EBB5,0x6E163697,0x88D273CC,
230 0xDE966292,0x81B949D0,0x4C50901B,0x71C65614,0xE6C6C7BD,0x327A140A,
231 0x45E1D006,0xC3F27B9A,0xC9AA53FD,0x62A80F00,0xBB25BFE2,0x35BDD2F6,
232 0x71126905,0xB2040222,0xB6CBCF7C,0xCD769C2B,0x53113EC0,0x1640E3D3,
233 0x38ABBD60,0x2547ADF0,0xBA38209C,0xF746CE76,0x77AFA1C5,0x20756060,
234 0x85CBFE4E,0x8AE88DD8,0x7AAAF9B0,0x4CF9AA7E,0x1948C25C,0x02FB8A8C,
235 0x01C36AE4,0xD6EBE1F9,0x90D4F869,0xA65CDEA0,0x3F09252D,0xC208E69F,
236 0xB74E6132,0xCE77E25B,0x578FDFE3,0x3AC372E6 };
237
238static const u32 ps[BLOWFISH_ROUNDS+2] = {
239 0x243F6A88,0x85A308D3,0x13198A2E,0x03707344,0xA4093822,0x299F31D0,
240 0x082EFA98,0xEC4E6C89,0x452821E6,0x38D01377,0xBE5466CF,0x34E90C6C,
241 0xC0AC29B7,0xC97C50DD,0x3F84D5B5,0xB5470917,0x9216D5D9,0x8979FB1B };
242
243
244
245#if BLOWFISH_ROUNDS != 16
246static inline u32
247function_F( BLOWFISH_context *bc, u32 x )
248{
249 u16 a, b, c, d;
250
251#ifdef WORDS_BIGENDIAN
252 a = ((byte*)&x)[0];
253 b = ((byte*)&x)[1];
254 c = ((byte*)&x)[2];
255 d = ((byte*)&x)[3];
256#else
257 a = ((byte*)&x)[3];
258 b = ((byte*)&x)[2];
259 c = ((byte*)&x)[1];
260 d = ((byte*)&x)[0];
261#endif
262
263 return ((bc->s0[a] + bc->s1[b]) ^ bc->s2[c] ) + bc->s3[d];
264}
265#endif
266
267#ifdef WORDS_BIGENDIAN
268 #define F(x) ((( s0[((byte*)&x)[0]] + s1[((byte*)&x)[1]]) \
269 ^ s2[((byte*)&x)[2]]) + s3[((byte*)&x)[3]] )
270#else
271 #define F(x) ((( s0[((byte*)&x)[3]] + s1[((byte*)&x)[2]]) \
272 ^ s2[((byte*)&x)[1]]) + s3[((byte*)&x)[0]] )
273#endif
274#define R(l,r,i) do { l ^= p[i]; r ^= F(l); } while(0)
275
276
277static void
278do_encrypt ( BLOWFISH_context *bc, u32 *ret_xl, u32 *ret_xr )
279{
280#if BLOWFISH_ROUNDS == 16
281 u32 xl, xr, *s0, *s1, *s2, *s3, *p;
282
283 xl = *ret_xl;
284 xr = *ret_xr;
285 p = bc->p;
286 s0 = bc->s0;
287 s1 = bc->s1;
288 s2 = bc->s2;
289 s3 = bc->s3;
290
291 R( xl, xr,0);
292 R( xr, xl,1);
293 R( xl, xr,2);
294 R( xr, xl,3);
295 R( xl, xr,4);
296 R( xr, xl,5);
297 R( xl, xr,6);
298 R( xr, xl,7);
299 R( xl, xr,8);
300 R( xr, xl,9);
301 R( xl, xr, 10);
302 R( xr, xl, 11);
303 R( xl, xr, 12);
304 R( xr, xl, 13);
305 R( xl, xr, 14);
306 R( xr, xl, 15);
307
308 xl ^= p[BLOWFISH_ROUNDS];
309 xr ^= p[BLOWFISH_ROUNDS+1];
310
311 *ret_xl = xr;
312 *ret_xr = xl;
313
314#else
315 u32 xl, xr, temp, *p;
316 int i;
317
318 xl = *ret_xl;
319 xr = *ret_xr;
320 p = bc->p;
321
322 for(i=0; i < BLOWFISH_ROUNDS; i++ )
323 {
324 xl ^= p[i];
325 xr ^= function_F(bc, xl);
326 temp = xl;
327 xl = xr;
328 xr = temp;
329 }
330 temp = xl;
331 xl = xr;
332 xr = temp;
333
334 xr ^= p[BLOWFISH_ROUNDS];
335 xl ^= p[BLOWFISH_ROUNDS+1];
336
337 *ret_xl = xl;
338 *ret_xr = xr;
339#endif
340}
341
342
343static void
344decrypt ( BLOWFISH_context *bc, u32 *ret_xl, u32 *ret_xr )
345{
346#if BLOWFISH_ROUNDS == 16
347 u32 xl, xr, *s0, *s1, *s2, *s3, *p;
348
349 xl = *ret_xl;
350 xr = *ret_xr;
351 p = bc->p;
352 s0 = bc->s0;
353 s1 = bc->s1;
354 s2 = bc->s2;
355 s3 = bc->s3;
356
357 R( xl, xr, 17);
358 R( xr, xl, 16);
359 R( xl, xr, 15);
360 R( xr, xl, 14);
361 R( xl, xr, 13);
362 R( xr, xl, 12);
363 R( xl, xr, 11);
364 R( xr, xl, 10);
365 R( xl, xr,9);
366 R( xr, xl,8);
367 R( xl, xr,7);
368 R( xr, xl,6);
369 R( xl, xr,5);
370 R( xr, xl,4);
371 R( xl, xr,3);
372 R( xr, xl,2);
373
374 xl ^= p[1];
375 xr ^= p[0];
376
377 *ret_xl = xr;
378 *ret_xr = xl;
379
380#else
381 u32 xl, xr, temp, *p;
382 int i;
383
384 xl = *ret_xl;
385 xr = *ret_xr;
386 p = bc->p;
387
388 for (i=BLOWFISH_ROUNDS+1; i > 1; i-- )
389 {
390 xl ^= p[i];
391 xr ^= function_F(bc, xl);
392 temp = xl;
393 xl = xr;
394 xr = temp;
395 }
396
397 temp = xl;
398 xl = xr;
399 xr = temp;
400
401 xr ^= p[1];
402 xl ^= p[0];
403
404 *ret_xl = xl;
405 *ret_xr = xr;
406#endif
407}
408
409#undef F
410#undef R
411
412static void
413do_encrypt_block ( BLOWFISH_context *bc, byte *outbuf, const byte *inbuf )
414{
415 u32 d1, d2;
416
417 d1 = inbuf[0] << 24 | inbuf[1] << 16 | inbuf[2] << 8 | inbuf[3];
418 d2 = inbuf[4] << 24 | inbuf[5] << 16 | inbuf[6] << 8 | inbuf[7];
419 do_encrypt( bc, &d1, &d2 );
420 outbuf[0] = (d1 >> 24) & 0xff;
421 outbuf[1] = (d1 >> 16) & 0xff;
422 outbuf[2] = (d1 >>8) & 0xff;
423 outbuf[3] = d1 & 0xff;
424 outbuf[4] = (d2 >> 24) & 0xff;
425 outbuf[5] = (d2 >> 16) & 0xff;
426 outbuf[6] = (d2 >>8) & 0xff;
427 outbuf[7] = d2 & 0xff;
428}
429
430static void
431encrypt_block (void *context, byte *outbuf, const byte *inbuf)
432{
433 BLOWFISH_context *bc = (BLOWFISH_context *) context;
434 do_encrypt_block (bc, outbuf, inbuf);
435 _gcry_burn_stack (64);
436}
437
438
439static void
440do_decrypt_block (BLOWFISH_context *bc, byte *outbuf, const byte *inbuf)
441{
442 u32 d1, d2;
443
444 d1 = inbuf[0] << 24 | inbuf[1] << 16 | inbuf[2] << 8 | inbuf[3];
445 d2 = inbuf[4] << 24 | inbuf[5] << 16 | inbuf[6] << 8 | inbuf[7];
446 decrypt( bc, &d1, &d2 );
447 outbuf[0] = (d1 >> 24) & 0xff;
448 outbuf[1] = (d1 >> 16) & 0xff;
449 outbuf[2] = (d1 >>8) & 0xff;
450 outbuf[3] = d1 & 0xff;
451 outbuf[4] = (d2 >> 24) & 0xff;
452 outbuf[5] = (d2 >> 16) & 0xff;
453 outbuf[6] = (d2 >>8) & 0xff;
454 outbuf[7] = d2 & 0xff;
455}
456
457static void
458decrypt_block (void *context, byte *outbuf, const byte *inbuf)
459{
460 BLOWFISH_context *bc = (BLOWFISH_context *) context;
461 do_decrypt_block (bc, outbuf, inbuf);
462 _gcry_burn_stack (64);
463}
464
465
466static const char*
467selftest(void)
468{
469 BLOWFISH_context c;
470 byte plain[] = "BLOWFISH";
471 byte buffer[8];
472 byte plain3[] = { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 };
473 byte key3[] = { 0x41, 0x79, 0x6E, 0xA0, 0x52, 0x61, 0x6E, 0xE4 };
474 byte cipher3[] = { 0xE1, 0x13, 0xF4, 0x10, 0x2C, 0xFC, 0xCE, 0x43 };
475
476 bf_setkey( (void *) &c, "abcdefghijklmnopqrstuvwxyz", 26 );
477 encrypt_block( (void *) &c, buffer, plain );
478 if( memcmp( buffer, "\x32\x4E\xD0\xFE\xF4\x13\xA2\x03", 8 ) )
479 return "Blowfish selftest failed (1).";
480 decrypt_block( (void *) &c, buffer, buffer );
481 if( memcmp( buffer, plain, 8 ) )
482 return "Blowfish selftest failed (2).";
483
484 bf_setkey( (void *) &c, key3, 8 );
485 encrypt_block( (void *) &c, buffer, plain3 );
486 if( memcmp( buffer, cipher3, 8 ) )
487 return "Blowfish selftest failed (3).";
488 decrypt_block( (void *) &c, buffer, buffer );
489 if( memcmp( buffer, plain3, 8 ) )
490 return "Blowfish selftest failed (4).";
491 return NULL;
492}
493
494
495
496static gcry_err_code_t
497do_bf_setkey (BLOWFISH_context *c, const byte *key, unsigned keylen)
498{
499 int i, j;
500 u32 data, datal, datar;
501 static int initialized;
502 static const char *selftest_failed;
503
504 if( !initialized )
505 {
506 initialized = 1;
507 selftest_failed = selftest();
508 if( selftest_failed )
509 log_error ("%s\n", selftest_failed );
510 }
511 if( selftest_failed )
512 return GPG_ERR_SELFTEST_FAILED;
513
514 for(i=0; i < BLOWFISH_ROUNDS+2; i++ )
515 c->p[i] = ps[i];
516 for(i=0; i < 256; i++ )
517 {
518 c->s0[i] = ks0[i];
519 c->s1[i] = ks1[i];
520 c->s2[i] = ks2[i];
521 c->s3[i] = ks3[i];
522 }
523
524 for(i=j=0; i < BLOWFISH_ROUNDS+2; i++ )
525 {
526#ifdef WORDS_BIGENDIAN
527 ((byte*)&data)[0] = key[j];
528 ((byte*)&data)[1] = key[(j+1)%keylen];
529 ((byte*)&data)[2] = key[(j+2)%keylen];
530 ((byte*)&data)[3] = key[(j+3)%keylen];
531#else
532 ((byte*)&data)[3] = key[j];
533 ((byte*)&data)[2] = key[(j+1)%keylen];
534 ((byte*)&data)[1] = key[(j+2)%keylen];
535 ((byte*)&data)[0] = key[(j+3)%keylen];
536#endif
537 c->p[i] ^= data;
538 j = (j+4) % keylen;
539 }
540
541 datal = datar = 0;
542 for(i=0; i < BLOWFISH_ROUNDS+2; i += 2 )
543 {
544 do_encrypt( c, &datal, &datar );
545 c->p[i] = datal;
546 c->p[i+1] = datar;
547 }
548 for(i=0; i < 256; i += 2 )
549 {
550 do_encrypt( c, &datal, &datar );
551 c->s0[i] = datal;
552 c->s0[i+1] = datar;
553 }
554 for(i=0; i < 256; i += 2 )
555 {
556 do_encrypt( c, &datal, &datar );
557 c->s1[i] = datal;
558 c->s1[i+1] = datar;
559 }
560 for(i=0; i < 256; i += 2 )
561 {
562 do_encrypt( c, &datal, &datar );
563 c->s2[i] = datal;
564 c->s2[i+1] = datar;
565 }
566 for(i=0; i < 256; i += 2 )
567 {
568 do_encrypt( c, &datal, &datar );
569 c->s3[i] = datal;
570 c->s3[i+1] = datar;
571 }
572
573
574 /* Check for weak key. A weak key is a key in which a value in
575 the P-array (here c) occurs more than once per table. */
576 for(i=0; i < 255; i++ )
577 {
578 for( j=i+1; j < 256; j++)
579 {
580 if( (c->s0[i] == c->s0[j]) || (c->s1[i] == c->s1[j]) ||
581 (c->s2[i] == c->s2[j]) || (c->s3[i] == c->s3[j]) )
582 return GPG_ERR_WEAK_KEY;
583 }
584 }
585
586 return GPG_ERR_NO_ERROR;
587}
588
589
590static gcry_err_code_t
591bf_setkey (void *context, const byte *key, unsigned keylen)
592{
593 BLOWFISH_context *c = (BLOWFISH_context *) context;
594 gcry_err_code_t rc = do_bf_setkey (c, key, keylen);
595 _gcry_burn_stack (64);
596 return rc;
597}
598
599
600gcry_cipher_spec_t _gcry_cipher_spec_blowfish =
601 {
602 "BLOWFISH", NULL, NULL, BLOWFISH_BLOCKSIZE, 128,
603 sizeof (BLOWFISH_context),
604 bf_setkey, encrypt_block, decrypt_block,
605 };
diff --git a/pwmanager/libcrypt/cipher/cast5.c b/pwmanager/libcrypt/cipher/cast5.c
new file mode 100644
index 0000000..866d2b4
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/cast5.c
@@ -0,0 +1,620 @@
1/* cast5.c - CAST5 cipher (RFC2144)
2 *Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21/* Test vectors:
22 *
23 * 128-bit key = 01 23 45 67 12 34 56 78 23 45 67 89 34 56 78 9A
24 * plaintext = 01 23 45 67 89 AB CD EF
25 * ciphertext = 23 8B 4F E5 84 7E 44 B2
26 *
27 * 80-bit key = 01 23 45 67 12 34 56 78 23 45
28 * = 01 23 45 67 12 34 56 78 23 45 00 00 00 00 00 00
29 * plaintext = 01 23 45 67 89 AB CD EF
30 * ciphertext = EB 6A 71 1A 2C 02 27 1B
31 *
32 * 40-bit key = 01 23 45 67 12
33 * = 01 23 45 67 12 00 00 00 00 00 00 00 00 00 00 00
34 * plaintext = 01 23 45 67 89 AB CD EF
35 * ciphertext = 7A C8 16 D1 6E 9B 30 2E
36 */
37
38#include <config.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include "g10lib.h"
43#include "types.h"
44#include "cipher.h"
45
46#define CAST5_BLOCKSIZE 8
47
48typedef struct {
49 u32 Km[16];
50 byte Kr[16];
51} CAST5_context;
52
53static gcry_err_code_t cast_setkey (void *c, const byte *key, unsigned keylen);
54static void encrypt_block (void *c, byte *outbuf, const byte *inbuf);
55static void decrypt_block (void *c, byte *outbuf, const byte *inbuf);
56
57
58
59
60static const u32 s1[256] = {
610x30fb40d4, 0x9fa0ff0b, 0x6beccd2f, 0x3f258c7a, 0x1e213f2f, 0x9c004dd3, 0x6003e540, 0xcf9fc949,
620xbfd4af27, 0x88bbbdb5, 0xe2034090, 0x98d09675, 0x6e63a0e0, 0x15c361d2, 0xc2e7661d, 0x22d4ff8e,
630x28683b6f, 0xc07fd059, 0xff2379c8, 0x775f50e2, 0x43c340d3, 0xdf2f8656, 0x887ca41a, 0xa2d2bd2d,
640xa1c9e0d6, 0x346c4819, 0x61b76d87, 0x22540f2f, 0x2abe32e1, 0xaa54166b, 0x22568e3a, 0xa2d341d0,
650x66db40c8, 0xa784392f, 0x004dff2f, 0x2db9d2de, 0x97943fac, 0x4a97c1d8, 0x527644b7, 0xb5f437a7,
660xb82cbaef, 0xd751d159, 0x6ff7f0ed, 0x5a097a1f, 0x827b68d0, 0x90ecf52e, 0x22b0c054, 0xbc8e5935,
670x4b6d2f7f, 0x50bb64a2, 0xd2664910, 0xbee5812d, 0xb7332290, 0xe93b159f, 0xb48ee411, 0x4bff345d,
680xfd45c240, 0xad31973f, 0xc4f6d02e, 0x55fc8165, 0xd5b1caad, 0xa1ac2dae, 0xa2d4b76d, 0xc19b0c50,
690x882240f2, 0x0c6e4f38, 0xa4e4bfd7, 0x4f5ba272, 0x564c1d2f, 0xc59c5319, 0xb949e354, 0xb04669fe,
700xb1b6ab8a, 0xc71358dd, 0x6385c545, 0x110f935d, 0x57538ad5, 0x6a390493, 0xe63d37e0, 0x2a54f6b3,
710x3a787d5f, 0x6276a0b5, 0x19a6fcdf, 0x7a42206a, 0x29f9d4d5, 0xf61b1891, 0xbb72275e, 0xaa508167,
720x38901091, 0xc6b505eb, 0x84c7cb8c, 0x2ad75a0f, 0x874a1427, 0xa2d1936b, 0x2ad286af, 0xaa56d291,
730xd7894360, 0x425c750d, 0x93b39e26, 0x187184c9, 0x6c00b32d, 0x73e2bb14, 0xa0bebc3c, 0x54623779,
740x64459eab, 0x3f328b82, 0x7718cf82, 0x59a2cea6, 0x04ee002e, 0x89fe78e6, 0x3fab0950, 0x325ff6c2,
750x81383f05, 0x6963c5c8, 0x76cb5ad6, 0xd49974c9, 0xca180dcf, 0x380782d5, 0xc7fa5cf6, 0x8ac31511,
760x35e79e13, 0x47da91d0, 0xf40f9086, 0xa7e2419e, 0x31366241, 0x051ef495, 0xaa573b04, 0x4a805d8d,
770x548300d0, 0x00322a3c, 0xbf64cddf, 0xba57a68e, 0x75c6372b, 0x50afd341, 0xa7c13275, 0x915a0bf5,
780x6b54bfab, 0x2b0b1426, 0xab4cc9d7, 0x449ccd82, 0xf7fbf265, 0xab85c5f3, 0x1b55db94, 0xaad4e324,
790xcfa4bd3f, 0x2deaa3e2, 0x9e204d02, 0xc8bd25ac, 0xeadf55b3, 0xd5bd9e98, 0xe31231b2, 0x2ad5ad6c,
800x954329de, 0xadbe4528, 0xd8710f69, 0xaa51c90f, 0xaa786bf6, 0x22513f1e, 0xaa51a79b, 0x2ad344cc,
810x7b5a41f0, 0xd37cfbad, 0x1b069505, 0x41ece491, 0xb4c332e6, 0x032268d4, 0xc9600acc, 0xce387e6d,
820xbf6bb16c, 0x6a70fb78, 0x0d03d9c9, 0xd4df39de, 0xe01063da, 0x4736f464, 0x5ad328d8, 0xb347cc96,
830x75bb0fc3, 0x98511bfb, 0x4ffbcc35, 0xb58bcf6a, 0xe11f0abc, 0xbfc5fe4a, 0xa70aec10, 0xac39570a,
840x3f04442f, 0x6188b153, 0xe0397a2e, 0x5727cb79, 0x9ceb418f, 0x1cacd68d, 0x2ad37c96, 0x0175cb9d,
850xc69dff09, 0xc75b65f0, 0xd9db40d8, 0xec0e7779, 0x4744ead4, 0xb11c3274, 0xdd24cb9e, 0x7e1c54bd,
860xf01144f9, 0xd2240eb1, 0x9675b3fd, 0xa3ac3755, 0xd47c27af, 0x51c85f4d, 0x56907596, 0xa5bb15e6,
870x580304f0, 0xca042cf1, 0x011a37ea, 0x8dbfaadb, 0x35ba3e4a, 0x3526ffa0, 0xc37b4d09, 0xbc306ed9,
880x98a52666, 0x5648f725, 0xff5e569d, 0x0ced63d0, 0x7c63b2cf, 0x700b45e1, 0xd5ea50f1, 0x85a92872,
890xaf1fbda7, 0xd4234870, 0xa7870bf3, 0x2d3b4d79, 0x42e04198, 0x0cd0ede7, 0x26470db8, 0xf881814c,
900x474d6ad7, 0x7c0c5e5c, 0xd1231959, 0x381b7298, 0xf5d2f4db, 0xab838653, 0x6e2f1e23, 0x83719c9e,
910xbd91e046, 0x9a56456e, 0xdc39200c, 0x20c8c571, 0x962bda1c, 0xe1e696ff, 0xb141ab08, 0x7cca89b9,
920x1a69e783, 0x02cc4843, 0xa2f7c579, 0x429ef47d, 0x427b169c, 0x5ac9f049, 0xdd8f0f00, 0x5c8165bf
93};
94static const u32 s2[256] = {
950x1f201094, 0xef0ba75b, 0x69e3cf7e, 0x393f4380, 0xfe61cf7a, 0xeec5207a, 0x55889c94, 0x72fc0651,
960xada7ef79, 0x4e1d7235, 0xd55a63ce, 0xde0436ba, 0x99c430ef, 0x5f0c0794, 0x18dcdb7d, 0xa1d6eff3,
970xa0b52f7b, 0x59e83605, 0xee15b094, 0xe9ffd909, 0xdc440086, 0xef944459, 0xba83ccb3, 0xe0c3cdfb,
980xd1da4181, 0x3b092ab1, 0xf997f1c1, 0xa5e6cf7b, 0x01420ddb, 0xe4e7ef5b, 0x25a1ff41, 0xe180f806,
990x1fc41080, 0x179bee7a, 0xd37ac6a9, 0xfe5830a4, 0x98de8b7f, 0x77e83f4e, 0x79929269, 0x24fa9f7b,
1000xe113c85b, 0xacc40083, 0xd7503525, 0xf7ea615f, 0x62143154, 0x0d554b63, 0x5d681121, 0xc866c359,
1010x3d63cf73, 0xcee234c0, 0xd4d87e87, 0x5c672b21, 0x071f6181, 0x39f7627f, 0x361e3084, 0xe4eb573b,
1020x602f64a4, 0xd63acd9c, 0x1bbc4635, 0x9e81032d, 0x2701f50c, 0x99847ab4, 0xa0e3df79, 0xba6cf38c,
1030x10843094, 0x2537a95e, 0xf46f6ffe, 0xa1ff3b1f, 0x208cfb6a, 0x8f458c74, 0xd9e0a227, 0x4ec73a34,
1040xfc884f69, 0x3e4de8df, 0xef0e0088, 0x3559648d, 0x8a45388c, 0x1d804366, 0x721d9bfd, 0xa58684bb,
1050xe8256333, 0x844e8212, 0x128d8098, 0xfed33fb4, 0xce280ae1, 0x27e19ba5, 0xd5a6c252, 0xe49754bd,
1060xc5d655dd, 0xeb667064, 0x77840b4d, 0xa1b6a801, 0x84db26a9, 0xe0b56714, 0x21f043b7, 0xe5d05860,
1070x54f03084, 0x066ff472, 0xa31aa153, 0xdadc4755, 0xb5625dbf, 0x68561be6, 0x83ca6b94, 0x2d6ed23b,
1080xeccf01db, 0xa6d3d0ba, 0xb6803d5c, 0xaf77a709, 0x33b4a34c, 0x397bc8d6, 0x5ee22b95, 0x5f0e5304,
1090x81ed6f61, 0x20e74364, 0xb45e1378, 0xde18639b, 0x881ca122, 0xb96726d1, 0x8049a7e8, 0x22b7da7b,
1100x5e552d25, 0x5272d237, 0x79d2951c, 0xc60d894c, 0x488cb402, 0x1ba4fe5b, 0xa4b09f6b, 0x1ca815cf,
1110xa20c3005, 0x8871df63, 0xb9de2fcb, 0x0cc6c9e9, 0x0beeff53, 0xe3214517, 0xb4542835, 0x9f63293c,
1120xee41e729, 0x6e1d2d7c, 0x50045286, 0x1e6685f3, 0xf33401c6, 0x30a22c95, 0x31a70850, 0x60930f13,
1130x73f98417, 0xa1269859, 0xec645c44, 0x52c877a9, 0xcdff33a6, 0xa02b1741, 0x7cbad9a2, 0x2180036f,
1140x50d99c08, 0xcb3f4861, 0xc26bd765, 0x64a3f6ab, 0x80342676, 0x25a75e7b, 0xe4e6d1fc, 0x20c710e6,
1150xcdf0b680, 0x17844d3b, 0x31eef84d, 0x7e0824e4, 0x2ccb49eb, 0x846a3bae, 0x8ff77888, 0xee5d60f6,
1160x7af75673, 0x2fdd5cdb, 0xa11631c1, 0x30f66f43, 0xb3faec54, 0x157fd7fa, 0xef8579cc, 0xd152de58,
1170xdb2ffd5e, 0x8f32ce19, 0x306af97a, 0x02f03ef8, 0x99319ad5, 0xc242fa0f, 0xa7e3ebb0, 0xc68e4906,
1180xb8da230c, 0x80823028, 0xdcdef3c8, 0xd35fb171, 0x088a1bc8, 0xbec0c560, 0x61a3c9e8, 0xbca8f54d,
1190xc72feffa, 0x22822e99, 0x82c570b4, 0xd8d94e89, 0x8b1c34bc, 0x301e16e6, 0x273be979, 0xb0ffeaa6,
1200x61d9b8c6, 0x00b24869, 0xb7ffce3f, 0x08dc283b, 0x43daf65a, 0xf7e19798, 0x7619b72f, 0x8f1c9ba4,
1210xdc8637a0, 0x16a7d3b1, 0x9fc393b7, 0xa7136eeb, 0xc6bcc63e, 0x1a513742, 0xef6828bc, 0x520365d6,
1220x2d6a77ab, 0x3527ed4b, 0x821fd216, 0x095c6e2e, 0xdb92f2fb, 0x5eea29cb, 0x145892f5, 0x91584f7f,
1230x5483697b, 0x2667a8cc, 0x85196048, 0x8c4bacea, 0x833860d4, 0x0d23e0f9, 0x6c387e8a, 0x0ae6d249,
1240xb284600c, 0xd835731d, 0xdcb1c647, 0xac4c56ea, 0x3ebd81b3, 0x230eabb0, 0x6438bc87, 0xf0b5b1fa,
1250x8f5ea2b3, 0xfc184642, 0x0a036b7a, 0x4fb089bd, 0x649da589, 0xa345415e, 0x5c038323, 0x3e5d3bb9,
1260x43d79572, 0x7e6dd07c, 0x06dfdf1e, 0x6c6cc4ef, 0x7160a539, 0x73bfbe70, 0x83877605, 0x4523ecf1
127};
128static const u32 s3[256] = {
1290x8defc240, 0x25fa5d9f, 0xeb903dbf, 0xe810c907, 0x47607fff, 0x369fe44b, 0x8c1fc644, 0xaececa90,
1300xbeb1f9bf, 0xeefbcaea, 0xe8cf1950, 0x51df07ae, 0x920e8806, 0xf0ad0548, 0xe13c8d83, 0x927010d5,
1310x11107d9f, 0x07647db9, 0xb2e3e4d4, 0x3d4f285e, 0xb9afa820, 0xfade82e0, 0xa067268b, 0x8272792e,
1320x553fb2c0, 0x489ae22b, 0xd4ef9794, 0x125e3fbc, 0x21fffcee, 0x825b1bfd, 0x9255c5ed, 0x1257a240,
1330x4e1a8302, 0xbae07fff, 0x528246e7, 0x8e57140e, 0x3373f7bf, 0x8c9f8188, 0xa6fc4ee8, 0xc982b5a5,
1340xa8c01db7, 0x579fc264, 0x67094f31, 0xf2bd3f5f, 0x40fff7c1, 0x1fb78dfc, 0x8e6bd2c1, 0x437be59b,
1350x99b03dbf, 0xb5dbc64b, 0x638dc0e6, 0x55819d99, 0xa197c81c, 0x4a012d6e, 0xc5884a28, 0xccc36f71,
1360xb843c213, 0x6c0743f1, 0x8309893c, 0x0feddd5f, 0x2f7fe850, 0xd7c07f7e, 0x02507fbf, 0x5afb9a04,
1370xa747d2d0, 0x1651192e, 0xaf70bf3e, 0x58c31380, 0x5f98302e, 0x727cc3c4, 0x0a0fb402, 0x0f7fef82,
1380x8c96fdad, 0x5d2c2aae, 0x8ee99a49, 0x50da88b8, 0x8427f4a0, 0x1eac5790, 0x796fb449, 0x8252dc15,
1390xefbd7d9b, 0xa672597d, 0xada840d8, 0x45f54504, 0xfa5d7403, 0xe83ec305, 0x4f91751a, 0x925669c2,
1400x23efe941, 0xa903f12e, 0x60270df2, 0x0276e4b6, 0x94fd6574, 0x927985b2, 0x8276dbcb, 0x02778176,
1410xf8af918d, 0x4e48f79e, 0x8f616ddf, 0xe29d840e, 0x842f7d83, 0x340ce5c8, 0x96bbb682, 0x93b4b148,
1420xef303cab, 0x984faf28, 0x779faf9b, 0x92dc560d, 0x224d1e20, 0x8437aa88, 0x7d29dc96, 0x2756d3dc,
1430x8b907cee, 0xb51fd240, 0xe7c07ce3, 0xe566b4a1, 0xc3e9615e, 0x3cf8209d, 0x6094d1e3, 0xcd9ca341,
1440x5c76460e, 0x00ea983b, 0xd4d67881, 0xfd47572c, 0xf76cedd9, 0xbda8229c, 0x127dadaa, 0x438a074e,
1450x1f97c090, 0x081bdb8a, 0x93a07ebe, 0xb938ca15, 0x97b03cff, 0x3dc2c0f8, 0x8d1ab2ec, 0x64380e51,
1460x68cc7bfb, 0xd90f2788, 0x12490181, 0x5de5ffd4, 0xdd7ef86a, 0x76a2e214, 0xb9a40368, 0x925d958f,
1470x4b39fffa, 0xba39aee9, 0xa4ffd30b, 0xfaf7933b, 0x6d498623, 0x193cbcfa, 0x27627545, 0x825cf47a,
1480x61bd8ba0, 0xd11e42d1, 0xcead04f4, 0x127ea392, 0x10428db7, 0x8272a972, 0x9270c4a8, 0x127de50b,
1490x285ba1c8, 0x3c62f44f, 0x35c0eaa5, 0xe805d231, 0x428929fb, 0xb4fcdf82, 0x4fb66a53, 0x0e7dc15b,
1500x1f081fab, 0x108618ae, 0xfcfd086d, 0xf9ff2889, 0x694bcc11, 0x236a5cae, 0x12deca4d, 0x2c3f8cc5,
1510xd2d02dfe, 0xf8ef5896, 0xe4cf52da, 0x95155b67, 0x494a488c, 0xb9b6a80c, 0x5c8f82bc, 0x89d36b45,
1520x3a609437, 0xec00c9a9, 0x44715253, 0x0a874b49, 0xd773bc40, 0x7c34671c, 0x02717ef6, 0x4feb5536,
1530xa2d02fff, 0xd2bf60c4, 0xd43f03c0, 0x50b4ef6d, 0x07478cd1, 0x006e1888, 0xa2e53f55, 0xb9e6d4bc,
1540xa2048016, 0x97573833, 0xd7207d67, 0xde0f8f3d, 0x72f87b33, 0xabcc4f33, 0x7688c55d, 0x7b00a6b0,
1550x947b0001, 0x570075d2, 0xf9bb88f8, 0x8942019e, 0x4264a5ff, 0x856302e0, 0x72dbd92b, 0xee971b69,
1560x6ea22fde, 0x5f08ae2b, 0xaf7a616d, 0xe5c98767, 0xcf1febd2, 0x61efc8c2, 0xf1ac2571, 0xcc8239c2,
1570x67214cb8, 0xb1e583d1, 0xb7dc3e62, 0x7f10bdce, 0xf90a5c38, 0x0ff0443d, 0x606e6dc6, 0x60543a49,
1580x5727c148, 0x2be98a1d, 0x8ab41738, 0x20e1be24, 0xaf96da0f, 0x68458425, 0x99833be5, 0x600d457d,
1590x282f9350, 0x8334b362, 0xd91d1120, 0x2b6d8da0, 0x642b1e31, 0x9c305a00, 0x52bce688, 0x1b03588a,
1600xf7baefd5, 0x4142ed9c, 0xa4315c11, 0x83323ec5, 0xdfef4636, 0xa133c501, 0xe9d3531c, 0xee353783
161};
162static const u32 s4[256] = {
1630x9db30420, 0x1fb6e9de, 0xa7be7bef, 0xd273a298, 0x4a4f7bdb, 0x64ad8c57, 0x85510443, 0xfa020ed1,
1640x7e287aff, 0xe60fb663, 0x095f35a1, 0x79ebf120, 0xfd059d43, 0x6497b7b1, 0xf3641f63, 0x241e4adf,
1650x28147f5f, 0x4fa2b8cd, 0xc9430040, 0x0cc32220, 0xfdd30b30, 0xc0a5374f, 0x1d2d00d9, 0x24147b15,
1660xee4d111a, 0x0fca5167, 0x71ff904c, 0x2d195ffe, 0x1a05645f, 0x0c13fefe, 0x081b08ca, 0x05170121,
1670x80530100, 0xe83e5efe, 0xac9af4f8, 0x7fe72701, 0xd2b8ee5f, 0x06df4261, 0xbb9e9b8a, 0x7293ea25,
1680xce84ffdf, 0xf5718801, 0x3dd64b04, 0xa26f263b, 0x7ed48400, 0x547eebe6, 0x446d4ca0, 0x6cf3d6f5,
1690x2649abdf, 0xaea0c7f5, 0x36338cc1, 0x503f7e93, 0xd3772061, 0x11b638e1, 0x72500e03, 0xf80eb2bb,
1700xabe0502e, 0xec8d77de, 0x57971e81, 0xe14f6746, 0xc9335400, 0x6920318f, 0x081dbb99, 0xffc304a5,
1710x4d351805, 0x7f3d5ce3, 0xa6c866c6, 0x5d5bcca9, 0xdaec6fea, 0x9f926f91, 0x9f46222f, 0x3991467d,
1720xa5bf6d8e, 0x1143c44f, 0x43958302, 0xd0214eeb, 0x022083b8, 0x3fb6180c, 0x18f8931e, 0x281658e6,
1730x26486e3e, 0x8bd78a70, 0x7477e4c1, 0xb506e07c, 0xf32d0a25, 0x79098b02, 0xe4eabb81, 0x28123b23,
1740x69dead38, 0x1574ca16, 0xdf871b62, 0x211c40b7, 0xa51a9ef9, 0x0014377b, 0x041e8ac8, 0x09114003,
1750xbd59e4d2, 0xe3d156d5, 0x4fe876d5, 0x2f91a340, 0x557be8de, 0x00eae4a7, 0x0ce5c2ec, 0x4db4bba6,
1760xe756bdff, 0xdd3369ac, 0xec17b035, 0x06572327, 0x99afc8b0, 0x56c8c391, 0x6b65811c, 0x5e146119,
1770x6e85cb75, 0xbe07c002, 0xc2325577, 0x893ff4ec, 0x5bbfc92d, 0xd0ec3b25, 0xb7801ab7, 0x8d6d3b24,
1780x20c763ef, 0xc366a5fc, 0x9c382880, 0x0ace3205, 0xaac9548a, 0xeca1d7c7, 0x041afa32, 0x1d16625a,
1790x6701902c, 0x9b757a54, 0x31d477f7, 0x9126b031, 0x36cc6fdb, 0xc70b8b46, 0xd9e66a48, 0x56e55a79,
1800x026a4ceb, 0x52437eff, 0x2f8f76b4, 0x0df980a5, 0x8674cde3, 0xedda04eb, 0x17a9be04, 0x2c18f4df,
1810xb7747f9d, 0xab2af7b4, 0xefc34d20, 0x2e096b7c, 0x1741a254, 0xe5b6a035, 0x213d42f6, 0x2c1c7c26,
1820x61c2f50f, 0x6552daf9, 0xd2c231f8, 0x25130f69, 0xd8167fa2, 0x0418f2c8, 0x001a96a6, 0x0d1526ab,
1830x63315c21, 0x5e0a72ec, 0x49bafefd, 0x187908d9, 0x8d0dbd86, 0x311170a7, 0x3e9b640c, 0xcc3e10d7,
1840xd5cad3b6, 0x0caec388, 0xf73001e1, 0x6c728aff, 0x71eae2a1, 0x1f9af36e, 0xcfcbd12f, 0xc1de8417,
1850xac07be6b, 0xcb44a1d8, 0x8b9b0f56, 0x013988c3, 0xb1c52fca, 0xb4be31cd, 0xd8782806, 0x12a3a4e2,
1860x6f7de532, 0x58fd7eb6, 0xd01ee900, 0x24adffc2, 0xf4990fc5, 0x9711aac5, 0x001d7b95, 0x82e5e7d2,
1870x109873f6, 0x00613096, 0xc32d9521, 0xada121ff, 0x29908415, 0x7fbb977f, 0xaf9eb3db, 0x29c9ed2a,
1880x5ce2a465, 0xa730f32c, 0xd0aa3fe8, 0x8a5cc091, 0xd49e2ce7, 0x0ce454a9, 0xd60acd86, 0x015f1919,
1890x77079103, 0xdea03af6, 0x78a8565e, 0xdee356df, 0x21f05cbe, 0x8b75e387, 0xb3c50651, 0xb8a5c3ef,
1900xd8eeb6d2, 0xe523be77, 0xc2154529, 0x2f69efdf, 0xafe67afb, 0xf470c4b2, 0xf3e0eb5b, 0xd6cc9876,
1910x39e4460c, 0x1fda8538, 0x1987832f, 0xca007367, 0xa99144f8, 0x296b299e, 0x492fc295, 0x9266beab,
1920xb5676e69, 0x9bd3ddda, 0xdf7e052f, 0xdb25701c, 0x1b5e51ee, 0xf65324e6, 0x6afce36c, 0x0316cc04,
1930x8644213e, 0xb7dc59d0, 0x7965291f, 0xccd6fd43, 0x41823979, 0x932bcdf6, 0xb657c34d, 0x4edfd282,
1940x7ae5290c, 0x3cb9536b, 0x851e20fe, 0x9833557e, 0x13ecf0b0, 0xd3ffb372, 0x3f85c5c1, 0x0aef7ed2
195};
196static const u32 s5[256] = {
1970x7ec90c04, 0x2c6e74b9, 0x9b0e66df, 0xa6337911, 0xb86a7fff, 0x1dd358f5, 0x44dd9d44, 0x1731167f,
1980x08fbf1fa, 0xe7f511cc, 0xd2051b00, 0x735aba00, 0x2ab722d8, 0x386381cb, 0xacf6243a, 0x69befd7a,
1990xe6a2e77f, 0xf0c720cd, 0xc4494816, 0xccf5c180, 0x38851640, 0x15b0a848, 0xe68b18cb, 0x4caadeff,
2000x5f480a01, 0x0412b2aa, 0x259814fc, 0x41d0efe2, 0x4e40b48d, 0x248eb6fb, 0x8dba1cfe, 0x41a99b02,
2010x1a550a04, 0xba8f65cb, 0x7251f4e7, 0x95a51725, 0xc106ecd7, 0x97a5980a, 0xc539b9aa, 0x4d79fe6a,
2020xf2f3f763, 0x68af8040, 0xed0c9e56, 0x11b4958b, 0xe1eb5a88, 0x8709e6b0, 0xd7e07156, 0x4e29fea7,
2030x6366e52d, 0x02d1c000, 0xc4ac8e05, 0x9377f571, 0x0c05372a, 0x578535f2, 0x2261be02, 0xd642a0c9,
2040xdf13a280, 0x74b55bd2, 0x682199c0, 0xd421e5ec, 0x53fb3ce8, 0xc8adedb3, 0x28a87fc9, 0x3d959981,
2050x5c1ff900, 0xfe38d399, 0x0c4eff0b, 0x062407ea, 0xaa2f4fb1, 0x4fb96976, 0x90c79505, 0xb0a8a774,
2060xef55a1ff, 0xe59ca2c2, 0xa6b62d27, 0xe66a4263, 0xdf65001f, 0x0ec50966, 0xdfdd55bc, 0x29de0655,
2070x911e739a, 0x17af8975, 0x32c7911c, 0x89f89468, 0x0d01e980, 0x524755f4, 0x03b63cc9, 0x0cc844b2,
2080xbcf3f0aa, 0x87ac36e9, 0xe53a7426, 0x01b3d82b, 0x1a9e7449, 0x64ee2d7e, 0xcddbb1da, 0x01c94910,
2090xb868bf80, 0x0d26f3fd, 0x9342ede7, 0x04a5c284, 0x636737b6, 0x50f5b616, 0xf24766e3, 0x8eca36c1,
2100x136e05db, 0xfef18391, 0xfb887a37, 0xd6e7f7d4, 0xc7fb7dc9, 0x3063fcdf, 0xb6f589de, 0xec2941da,
2110x26e46695, 0xb7566419, 0xf654efc5, 0xd08d58b7, 0x48925401, 0xc1bacb7f, 0xe5ff550f, 0xb6083049,
2120x5bb5d0e8, 0x87d72e5a, 0xab6a6ee1, 0x223a66ce, 0xc62bf3cd, 0x9e0885f9, 0x68cb3e47, 0x086c010f,
2130xa21de820, 0xd18b69de, 0xf3f65777, 0xfa02c3f6, 0x407edac3, 0xcbb3d550, 0x1793084d, 0xb0d70eba,
2140x0ab378d5, 0xd951fb0c, 0xded7da56, 0x4124bbe4, 0x94ca0b56, 0x0f5755d1, 0xe0e1e56e, 0x6184b5be,
2150x580a249f, 0x94f74bc0, 0xe327888e, 0x9f7b5561, 0xc3dc0280, 0x05687715, 0x646c6bd7, 0x44904db3,
2160x66b4f0a3, 0xc0f1648a, 0x697ed5af, 0x49e92ff6, 0x309e374f, 0x2cb6356a, 0x85808573, 0x4991f840,
2170x76f0ae02, 0x083be84d, 0x28421c9a, 0x44489406, 0x736e4cb8, 0xc1092910, 0x8bc95fc6, 0x7d869cf4,
2180x134f616f, 0x2e77118d, 0xb31b2be1, 0xaa90b472, 0x3ca5d717, 0x7d161bba, 0x9cad9010, 0xaf462ba2,
2190x9fe459d2, 0x45d34559, 0xd9f2da13, 0xdbc65487, 0xf3e4f94e, 0x176d486f, 0x097c13ea, 0x631da5c7,
2200x445f7382, 0x175683f4, 0xcdc66a97, 0x70be0288, 0xb3cdcf72, 0x6e5dd2f3, 0x20936079, 0x459b80a5,
2210xbe60e2db, 0xa9c23101, 0xeba5315c, 0x224e42f2, 0x1c5c1572, 0xf6721b2c, 0x1ad2fff3, 0x8c25404e,
2220x324ed72f, 0x4067b7fd, 0x0523138e, 0x5ca3bc78, 0xdc0fd66e, 0x75922283, 0x784d6b17, 0x58ebb16e,
2230x44094f85, 0x3f481d87, 0xfcfeae7b, 0x77b5ff76, 0x8c2302bf, 0xaaf47556, 0x5f46b02a, 0x2b092801,
2240x3d38f5f7, 0x0ca81f36, 0x52af4a8a, 0x66d5e7c0, 0xdf3b0874, 0x95055110, 0x1b5ad7a8, 0xf61ed5ad,
2250x6cf6e479, 0x20758184, 0xd0cefa65, 0x88f7be58, 0x4a046826, 0x0ff6f8f3, 0xa09c7f70, 0x5346aba0,
2260x5ce96c28, 0xe176eda3, 0x6bac307f, 0x376829d2, 0x85360fa9, 0x17e3fe2a, 0x24b79767, 0xf5a96b20,
2270xd6cd2595, 0x68ff1ebf, 0x7555442c, 0xf19f06be, 0xf9e0659a, 0xeeb9491d, 0x34010718, 0xbb30cab8,
2280xe822fe15, 0x88570983, 0x750e6249, 0xda627e55, 0x5e76ffa8, 0xb1534546, 0x6d47de08, 0xefe9e7d4
229};
230static const u32 s6[256] = {
2310xf6fa8f9d, 0x2cac6ce1, 0x4ca34867, 0xe2337f7c, 0x95db08e7, 0x016843b4, 0xeced5cbc, 0x325553ac,
2320xbf9f0960, 0xdfa1e2ed, 0x83f0579d, 0x63ed86b9, 0x1ab6a6b8, 0xde5ebe39, 0xf38ff732, 0x8989b138,
2330x33f14961, 0xc01937bd, 0xf506c6da, 0xe4625e7e, 0xa308ea99, 0x4e23e33c, 0x79cbd7cc, 0x48a14367,
2340xa3149619, 0xfec94bd5, 0xa114174a, 0xeaa01866, 0xa084db2d, 0x09a8486f, 0xa888614a, 0x2900af98,
2350x01665991, 0xe1992863, 0xc8f30c60, 0x2e78ef3c, 0xd0d51932, 0xcf0fec14, 0xf7ca07d2, 0xd0a82072,
2360xfd41197e, 0x9305a6b0, 0xe86be3da, 0x74bed3cd, 0x372da53c, 0x4c7f4448, 0xdab5d440, 0x6dba0ec3,
2370x083919a7, 0x9fbaeed9, 0x49dbcfb0, 0x4e670c53, 0x5c3d9c01, 0x64bdb941, 0x2c0e636a, 0xba7dd9cd,
2380xea6f7388, 0xe70bc762, 0x35f29adb, 0x5c4cdd8d, 0xf0d48d8c, 0xb88153e2, 0x08a19866, 0x1ae2eac8,
2390x284caf89, 0xaa928223, 0x9334be53, 0x3b3a21bf, 0x16434be3, 0x9aea3906, 0xefe8c36e, 0xf890cdd9,
2400x80226dae, 0xc340a4a3, 0xdf7e9c09, 0xa694a807, 0x5b7c5ecc, 0x221db3a6, 0x9a69a02f, 0x68818a54,
2410xceb2296f, 0x53c0843a, 0xfe893655, 0x25bfe68a, 0xb4628abc, 0xcf222ebf, 0x25ac6f48, 0xa9a99387,
2420x53bddb65, 0xe76ffbe7, 0xe967fd78, 0x0ba93563, 0x8e342bc1, 0xe8a11be9, 0x4980740d, 0xc8087dfc,
2430x8de4bf99, 0xa11101a0, 0x7fd37975, 0xda5a26c0, 0xe81f994f, 0x9528cd89, 0xfd339fed, 0xb87834bf,
2440x5f04456d, 0x22258698, 0xc9c4c83b, 0x2dc156be, 0x4f628daa, 0x57f55ec5, 0xe2220abe, 0xd2916ebf,
2450x4ec75b95, 0x24f2c3c0, 0x42d15d99, 0xcd0d7fa0, 0x7b6e27ff, 0xa8dc8af0, 0x7345c106, 0xf41e232f,
2460x35162386, 0xe6ea8926, 0x3333b094, 0x157ec6f2, 0x372b74af, 0x692573e4, 0xe9a9d848, 0xf3160289,
2470x3a62ef1d, 0xa787e238, 0xf3a5f676, 0x74364853, 0x20951063, 0x4576698d, 0xb6fad407, 0x592af950,
2480x36f73523, 0x4cfb6e87, 0x7da4cec0, 0x6c152daa, 0xcb0396a8, 0xc50dfe5d, 0xfcd707ab, 0x0921c42f,
2490x89dff0bb, 0x5fe2be78, 0x448f4f33, 0x754613c9, 0x2b05d08d, 0x48b9d585, 0xdc049441, 0xc8098f9b,
2500x7dede786, 0xc39a3373, 0x42410005, 0x6a091751, 0x0ef3c8a6, 0x890072d6, 0x28207682, 0xa9a9f7be,
2510xbf32679d, 0xd45b5b75, 0xb353fd00, 0xcbb0e358, 0x830f220a, 0x1f8fb214, 0xd372cf08, 0xcc3c4a13,
2520x8cf63166, 0x061c87be, 0x88c98f88, 0x6062e397, 0x47cf8e7a, 0xb6c85283, 0x3cc2acfb, 0x3fc06976,
2530x4e8f0252, 0x64d8314d, 0xda3870e3, 0x1e665459, 0xc10908f0, 0x513021a5, 0x6c5b68b7, 0x822f8aa0,
2540x3007cd3e, 0x74719eef, 0xdc872681, 0x073340d4, 0x7e432fd9, 0x0c5ec241, 0x8809286c, 0xf592d891,
2550x08a930f6, 0x957ef305, 0xb7fbffbd, 0xc266e96f, 0x6fe4ac98, 0xb173ecc0, 0xbc60b42a, 0x953498da,
2560xfba1ae12, 0x2d4bd736, 0x0f25faab, 0xa4f3fceb, 0xe2969123, 0x257f0c3d, 0x9348af49, 0x361400bc,
2570xe8816f4a, 0x3814f200, 0xa3f94043, 0x9c7a54c2, 0xbc704f57, 0xda41e7f9, 0xc25ad33a, 0x54f4a084,
2580xb17f5505, 0x59357cbe, 0xedbd15c8, 0x7f97c5ab, 0xba5ac7b5, 0xb6f6deaf, 0x3a479c3a, 0x5302da25,
2590x653d7e6a, 0x54268d49, 0x51a477ea, 0x5017d55b, 0xd7d25d88, 0x44136c76, 0x0404a8c8, 0xb8e5a121,
2600xb81a928a, 0x60ed5869, 0x97c55b96, 0xeaec991b, 0x29935913, 0x01fdb7f1, 0x088e8dfa, 0x9ab6f6f5,
2610x3b4cbf9f, 0x4a5de3ab, 0xe6051d35, 0xa0e1d855, 0xd36b4cf1, 0xf544edeb, 0xb0e93524, 0xbebb8fbd,
2620xa2d762cf, 0x49c92f54, 0x38b5f331, 0x7128a454, 0x48392905, 0xa65b1db8, 0x851c97bd, 0xd675cf2f
263};
264static const u32 s7[256] = {
2650x85e04019, 0x332bf567, 0x662dbfff, 0xcfc65693, 0x2a8d7f6f, 0xab9bc912, 0xde6008a1, 0x2028da1f,
2660x0227bce7, 0x4d642916, 0x18fac300, 0x50f18b82, 0x2cb2cb11, 0xb232e75c, 0x4b3695f2, 0xb28707de,
2670xa05fbcf6, 0xcd4181e9, 0xe150210c, 0xe24ef1bd, 0xb168c381, 0xfde4e789, 0x5c79b0d8, 0x1e8bfd43,
2680x4d495001, 0x38be4341, 0x913cee1d, 0x92a79c3f, 0x089766be, 0xbaeeadf4, 0x1286becf, 0xb6eacb19,
2690x2660c200, 0x7565bde4, 0x64241f7a, 0x8248dca9, 0xc3b3ad66, 0x28136086, 0x0bd8dfa8, 0x356d1cf2,
2700x107789be, 0xb3b2e9ce, 0x0502aa8f, 0x0bc0351e, 0x166bf52a, 0xeb12ff82, 0xe3486911, 0xd34d7516,
2710x4e7b3aff, 0x5f43671b, 0x9cf6e037, 0x4981ac83, 0x334266ce, 0x8c9341b7, 0xd0d854c0, 0xcb3a6c88,
2720x47bc2829, 0x4725ba37, 0xa66ad22b, 0x7ad61f1e, 0x0c5cbafa, 0x4437f107, 0xb6e79962, 0x42d2d816,
2730x0a961288, 0xe1a5c06e, 0x13749e67, 0x72fc081a, 0xb1d139f7, 0xf9583745, 0xcf19df58, 0xbec3f756,
2740xc06eba30, 0x07211b24, 0x45c28829, 0xc95e317f, 0xbc8ec511, 0x38bc46e9, 0xc6e6fa14, 0xbae8584a,
2750xad4ebc46, 0x468f508b, 0x7829435f, 0xf124183b, 0x821dba9f, 0xaff60ff4, 0xea2c4e6d, 0x16e39264,
2760x92544a8b, 0x009b4fc3, 0xaba68ced, 0x9ac96f78, 0x06a5b79a, 0xb2856e6e, 0x1aec3ca9, 0xbe838688,
2770x0e0804e9, 0x55f1be56, 0xe7e5363b, 0xb3a1f25d, 0xf7debb85, 0x61fe033c, 0x16746233, 0x3c034c28,
2780xda6d0c74, 0x79aac56c, 0x3ce4e1ad, 0x51f0c802, 0x98f8f35a, 0x1626a49f, 0xeed82b29, 0x1d382fe3,
2790x0c4fb99a, 0xbb325778, 0x3ec6d97b, 0x6e77a6a9, 0xcb658b5c, 0xd45230c7, 0x2bd1408b, 0x60c03eb7,
2800xb9068d78, 0xa33754f4, 0xf430c87d, 0xc8a71302, 0xb96d8c32, 0xebd4e7be, 0xbe8b9d2d, 0x7979fb06,
2810xe7225308, 0x8b75cf77, 0x11ef8da4, 0xe083c858, 0x8d6b786f, 0x5a6317a6, 0xfa5cf7a0, 0x5dda0033,
2820xf28ebfb0, 0xf5b9c310, 0xa0eac280, 0x08b9767a, 0xa3d9d2b0, 0x79d34217, 0x021a718d, 0x9ac6336a,
2830x2711fd60, 0x438050e3, 0x069908a8, 0x3d7fedc4, 0x826d2bef, 0x4eeb8476, 0x488dcf25, 0x36c9d566,
2840x28e74e41, 0xc2610aca, 0x3d49a9cf, 0xbae3b9df, 0xb65f8de6, 0x92aeaf64, 0x3ac7d5e6, 0x9ea80509,
2850xf22b017d, 0xa4173f70, 0xdd1e16c3, 0x15e0d7f9, 0x50b1b887, 0x2b9f4fd5, 0x625aba82, 0x6a017962,
2860x2ec01b9c, 0x15488aa9, 0xd716e740, 0x40055a2c, 0x93d29a22, 0xe32dbf9a, 0x058745b9, 0x3453dc1e,
2870xd699296e, 0x496cff6f, 0x1c9f4986, 0xdfe2ed07, 0xb87242d1, 0x19de7eae, 0x053e561a, 0x15ad6f8c,
2880x66626c1c, 0x7154c24c, 0xea082b2a, 0x93eb2939, 0x17dcb0f0, 0x58d4f2ae, 0x9ea294fb, 0x52cf564c,
2890x9883fe66, 0x2ec40581, 0x763953c3, 0x01d6692e, 0xd3a0c108, 0xa1e7160e, 0xe4f2dfa6, 0x693ed285,
2900x74904698, 0x4c2b0edd, 0x4f757656, 0x5d393378, 0xa132234f, 0x3d321c5d, 0xc3f5e194, 0x4b269301,
2910xc79f022f, 0x3c997e7e, 0x5e4f9504, 0x3ffafbbd, 0x76f7ad0e, 0x296693f4, 0x3d1fce6f, 0xc61e45be,
2920xd3b5ab34, 0xf72bf9b7, 0x1b0434c0, 0x4e72b567, 0x5592a33d, 0xb5229301, 0xcfd2a87f, 0x60aeb767,
2930x1814386b, 0x30bcc33d, 0x38a0c07d, 0xfd1606f2, 0xc363519b, 0x589dd390, 0x5479f8e6, 0x1cb8d647,
2940x97fd61a9, 0xea7759f4, 0x2d57539d, 0x569a58cf, 0xe84e63ad, 0x462e1b78, 0x6580f87e, 0xf3817914,
2950x91da55f4, 0x40a230f3, 0xd1988f35, 0xb6e318d2, 0x3ffa50bc, 0x3d40f021, 0xc3c0bdae, 0x4958c24c,
2960x518f36b2, 0x84b1d370, 0x0fedce83, 0x878ddada, 0xf2a279c7, 0x94e01be8, 0x90716f4b, 0x954b8aa3
297};
298static const u32 s8[256] = {
2990xe216300d, 0xbbddfffc, 0xa7ebdabd, 0x35648095, 0x7789f8b7, 0xe6c1121b, 0x0e241600, 0x052ce8b5,
3000x11a9cfb0, 0xe5952f11, 0xece7990a, 0x9386d174, 0x2a42931c, 0x76e38111, 0xb12def3a, 0x37ddddfc,
3010xde9adeb1, 0x0a0cc32c, 0xbe197029, 0x84a00940, 0xbb243a0f, 0xb4d137cf, 0xb44e79f0, 0x049eedfd,
3020x0b15a15d, 0x480d3168, 0x8bbbde5a, 0x669ded42, 0xc7ece831, 0x3f8f95e7, 0x72df191b, 0x7580330d,
3030x94074251, 0x5c7dcdfa, 0xabbe6d63, 0xaa402164, 0xb301d40a, 0x02e7d1ca, 0x53571dae, 0x7a3182a2,
3040x12a8ddec, 0xfdaa335d, 0x176f43e8, 0x71fb46d4, 0x38129022, 0xce949ad4, 0xb84769ad, 0x965bd862,
3050x82f3d055, 0x66fb9767, 0x15b80b4e, 0x1d5b47a0, 0x4cfde06f, 0xc28ec4b8, 0x57e8726e, 0x647a78fc,
3060x99865d44, 0x608bd593, 0x6c200e03, 0x39dc5ff6, 0x5d0b00a3, 0xae63aff2, 0x7e8bd632, 0x70108c0c,
3070xbbd35049, 0x2998df04, 0x980cf42a, 0x9b6df491, 0x9e7edd53, 0x06918548, 0x58cb7e07, 0x3b74ef2e,
3080x522fffb1, 0xd24708cc, 0x1c7e27cd, 0xa4eb215b, 0x3cf1d2e2, 0x19b47a38, 0x424f7618, 0x35856039,
3090x9d17dee7, 0x27eb35e6, 0xc9aff67b, 0x36baf5b8, 0x09c467cd, 0xc18910b1, 0xe11dbf7b, 0x06cd1af8,
3100x7170c608, 0x2d5e3354, 0xd4de495a, 0x64c6d006, 0xbcc0c62c, 0x3dd00db3, 0x708f8f34, 0x77d51b42,
3110x264f620f, 0x24b8d2bf, 0x15c1b79e, 0x46a52564, 0xf8d7e54e, 0x3e378160, 0x7895cda5, 0x859c15a5,
3120xe6459788, 0xc37bc75f, 0xdb07ba0c, 0x0676a3ab, 0x7f229b1e, 0x31842e7b, 0x24259fd7, 0xf8bef472,
3130x835ffcb8, 0x6df4c1f2, 0x96f5b195, 0xfd0af0fc, 0xb0fe134c, 0xe2506d3d, 0x4f9b12ea, 0xf215f225,
3140xa223736f, 0x9fb4c428, 0x25d04979, 0x34c713f8, 0xc4618187, 0xea7a6e98, 0x7cd16efc, 0x1436876c,
3150xf1544107, 0xbedeee14, 0x56e9af27, 0xa04aa441, 0x3cf7c899, 0x92ecbae6, 0xdd67016d, 0x151682eb,
3160xa842eedf, 0xfdba60b4, 0xf1907b75, 0x20e3030f, 0x24d8c29e, 0xe139673b, 0xefa63fb8, 0x71873054,
3170xb6f2cf3b, 0x9f326442, 0xcb15a4cc, 0xb01a4504, 0xf1e47d8d, 0x844a1be5, 0xbae7dfdc, 0x42cbda70,
3180xcd7dae0a, 0x57e85b7a, 0xd53f5af6, 0x20cf4d8c, 0xcea4d428, 0x79d130a4, 0x3486ebfb, 0x33d3cddc,
3190x77853b53, 0x37effcb5, 0xc5068778, 0xe580b3e6, 0x4e68b8f4, 0xc5c8b37e, 0x0d809ea2, 0x398feb7c,
3200x132a4f94, 0x43b7950e, 0x2fee7d1c, 0x223613bd, 0xdd06caa2, 0x37df932b, 0xc4248289, 0xacf3ebc3,
3210x5715f6b7, 0xef3478dd, 0xf267616f, 0xc148cbe4, 0x9052815e, 0x5e410fab, 0xb48a2465, 0x2eda7fa4,
3220xe87b40e4, 0xe98ea084, 0x5889e9e1, 0xefd390fc, 0xdd07d35b, 0xdb485694, 0x38d7e5b2, 0x57720101,
3230x730edebc, 0x5b643113, 0x94917e4f, 0x503c2fba, 0x646f1282, 0x7523d24a, 0xe0779695, 0xf9c17a8f,
3240x7a5b2121, 0xd187b896, 0x29263a4d, 0xba510cdf, 0x81f47c9f, 0xad1163ed, 0xea7b5965, 0x1a00726e,
3250x11403092, 0x00da6d77, 0x4a0cdd61, 0xad1f4603, 0x605bdfb0, 0x9eedc364, 0x22ebe6a8, 0xcee7d28a,
3260xa0e736a0, 0x5564a6b9, 0x10853209, 0xc7eb8f37, 0x2de705ca, 0x8951570f, 0xdf09822b, 0xbd691a6c,
3270xaa12e4f2, 0x87451c0f, 0xe0f6a27a, 0x3ada4819, 0x4cf1764f, 0x0d771c2b, 0x67cdb156, 0x350d8384,
3280x5938fa0f, 0x42399ef3, 0x36997b07, 0x0e84093d, 0x4aa93e61, 0x8360d87b, 0x1fa98b0c, 0x1149382c,
3290xe97625a5, 0x0614d1b7, 0x0e25244b, 0x0c768347, 0x589e8d82, 0x0d2059d1, 0xa466bb1e, 0xf8da0a82,
3300x04f19130, 0xba6e4ec0, 0x99265164, 0x1ee7230d, 0x50b2ad80, 0xeaee6801, 0x8db2a283, 0xea8bf59e
331};
332
333
334#if defined(__GNUC__) && defined(__i386__)
335static inline u32
336rol(int n, u32 x)
337{
338 __asm__("roll %%cl,%0"
339 :"=r" (x)
340 :"0" (x),"c" (n));
341 return x;
342}
343#else
344#define rol(n,x) ( ((x) << (n)) | ((x) >> (32-(n))) )
345#endif
346
347#define F1(D,m,r) ( (I = ((m) + (D))), (I=rol((r),I)), \
348 (((s1[I >> 24] ^ s2[(I>>16)&0xff]) - s3[(I>>8)&0xff]) + s4[I&0xff]) )
349#define F2(D,m,r) ( (I = ((m) ^ (D))), (I=rol((r),I)), \
350 (((s1[I >> 24] - s2[(I>>16)&0xff]) + s3[(I>>8)&0xff]) ^ s4[I&0xff]) )
351#define F3(D,m,r) ( (I = ((m) - (D))), (I=rol((r),I)), \
352 (((s1[I >> 24] + s2[(I>>16)&0xff]) ^ s3[(I>>8)&0xff]) - s4[I&0xff]) )
353
354static void
355do_encrypt_block( CAST5_context *c, byte *outbuf, const byte *inbuf )
356{
357 u32 l, r, t;
358 u32 I; /* used by the Fx macros */
359 u32 *Km;
360 byte *Kr;
361
362 Km = c->Km;
363 Kr = c->Kr;
364
365 /* (L0,R0) <-- (m1...m64).(Split the plaintext into left and
366 * right 32-bit halves L0 = m1...m32 and R0 = m33...m64.)
367 */
368 l = inbuf[0] << 24 | inbuf[1] << 16 | inbuf[2] << 8 | inbuf[3];
369 r = inbuf[4] << 24 | inbuf[5] << 16 | inbuf[6] << 8 | inbuf[7];
370
371 /* (16 rounds) for i from 1 to 16, compute Li and Ri as follows:
372 *Li = Ri-1;
373 *Ri = Li-1 ^ f(Ri-1,Kmi,Kri), where f is defined in Section 2.2
374 * Rounds 1, 4, 7, 10, 13, and 16 use f function Type 1.
375 * Rounds 2, 5, 8, 11, and 14 use f function Type 2.
376 * Rounds 3, 6, 9, 12, and 15 use f function Type 3.
377 */
378
379 t = l; l = r; r = t ^ F1(r, Km[ 0], Kr[ 0]);
380 t = l; l = r; r = t ^ F2(r, Km[ 1], Kr[ 1]);
381 t = l; l = r; r = t ^ F3(r, Km[ 2], Kr[ 2]);
382 t = l; l = r; r = t ^ F1(r, Km[ 3], Kr[ 3]);
383 t = l; l = r; r = t ^ F2(r, Km[ 4], Kr[ 4]);
384 t = l; l = r; r = t ^ F3(r, Km[ 5], Kr[ 5]);
385 t = l; l = r; r = t ^ F1(r, Km[ 6], Kr[ 6]);
386 t = l; l = r; r = t ^ F2(r, Km[ 7], Kr[ 7]);
387 t = l; l = r; r = t ^ F3(r, Km[ 8], Kr[ 8]);
388 t = l; l = r; r = t ^ F1(r, Km[ 9], Kr[ 9]);
389 t = l; l = r; r = t ^ F2(r, Km[10], Kr[10]);
390 t = l; l = r; r = t ^ F3(r, Km[11], Kr[11]);
391 t = l; l = r; r = t ^ F1(r, Km[12], Kr[12]);
392 t = l; l = r; r = t ^ F2(r, Km[13], Kr[13]);
393 t = l; l = r; r = t ^ F3(r, Km[14], Kr[14]);
394 t = l; l = r; r = t ^ F1(r, Km[15], Kr[15]);
395
396 /* c1...c64 <-- (R16,L16).(Exchange final blocks L16, R16 and
397 *concatenate to form the ciphertext.) */
398 outbuf[0] = (r >> 24) & 0xff;
399 outbuf[1] = (r >> 16) & 0xff;
400 outbuf[2] = (r >> 8) & 0xff;
401 outbuf[3] = r & 0xff;
402 outbuf[4] = (l >> 24) & 0xff;
403 outbuf[5] = (l >> 16) & 0xff;
404 outbuf[6] = (l >> 8) & 0xff;
405 outbuf[7] = l & 0xff;
406}
407
408static void
409encrypt_block (void *context , byte *outbuf, const byte *inbuf)
410{
411 CAST5_context *c = (CAST5_context *) context;
412 do_encrypt_block (c, outbuf, inbuf);
413 _gcry_burn_stack (20+4*sizeof(void*));
414}
415
416
417static void
418do_decrypt_block (CAST5_context *c, byte *outbuf, const byte *inbuf )
419{
420 u32 l, r, t;
421 u32 I;
422 u32 *Km;
423 byte *Kr;
424
425 Km = c->Km;
426 Kr = c->Kr;
427
428 l = inbuf[0] << 24 | inbuf[1] << 16 | inbuf[2] << 8 | inbuf[3];
429 r = inbuf[4] << 24 | inbuf[5] << 16 | inbuf[6] << 8 | inbuf[7];
430
431 t = l; l = r; r = t ^ F1(r, Km[15], Kr[15]);
432 t = l; l = r; r = t ^ F3(r, Km[14], Kr[14]);
433 t = l; l = r; r = t ^ F2(r, Km[13], Kr[13]);
434 t = l; l = r; r = t ^ F1(r, Km[12], Kr[12]);
435 t = l; l = r; r = t ^ F3(r, Km[11], Kr[11]);
436 t = l; l = r; r = t ^ F2(r, Km[10], Kr[10]);
437 t = l; l = r; r = t ^ F1(r, Km[ 9], Kr[ 9]);
438 t = l; l = r; r = t ^ F3(r, Km[ 8], Kr[ 8]);
439 t = l; l = r; r = t ^ F2(r, Km[ 7], Kr[ 7]);
440 t = l; l = r; r = t ^ F1(r, Km[ 6], Kr[ 6]);
441 t = l; l = r; r = t ^ F3(r, Km[ 5], Kr[ 5]);
442 t = l; l = r; r = t ^ F2(r, Km[ 4], Kr[ 4]);
443 t = l; l = r; r = t ^ F1(r, Km[ 3], Kr[ 3]);
444 t = l; l = r; r = t ^ F3(r, Km[ 2], Kr[ 2]);
445 t = l; l = r; r = t ^ F2(r, Km[ 1], Kr[ 1]);
446 t = l; l = r; r = t ^ F1(r, Km[ 0], Kr[ 0]);
447
448 outbuf[0] = (r >> 24) & 0xff;
449 outbuf[1] = (r >> 16) & 0xff;
450 outbuf[2] = (r >> 8) & 0xff;
451 outbuf[3] = r & 0xff;
452 outbuf[4] = (l >> 24) & 0xff;
453 outbuf[5] = (l >> 16) & 0xff;
454 outbuf[6] = (l >> 8) & 0xff;
455 outbuf[7] = l & 0xff;
456}
457
458static void
459decrypt_block (void *context, byte *outbuf, const byte *inbuf)
460{
461 CAST5_context *c = (CAST5_context *) context;
462 do_decrypt_block (c, outbuf, inbuf);
463 _gcry_burn_stack (20+4*sizeof(void*));
464}
465
466
467static const char*
468selftest(void)
469{
470 CAST5_context c;
471 byte key[16] = { 0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78,
472 0x23, 0x45, 0x67, 0x89, 0x34, 0x56, 0x78, 0x9A };
473 byte plain[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
474 byte cipher[8]= { 0x23, 0x8B, 0x4F, 0xE5, 0x84, 0x7E, 0x44, 0xB2 };
475 byte buffer[8];
476
477 cast_setkey( &c, key, 16 );
478 encrypt_block( &c, buffer, plain );
479 if( memcmp( buffer, cipher, 8 ) )
480 return "1";
481 decrypt_block( &c, buffer, buffer );
482 if( memcmp( buffer, plain, 8 ) )
483 return "2";
484
485#if 0 /* full maintenance test */
486 {
487 int i;
488 byte a0[16] = { 0x01,0x23,0x45,0x67,0x12,0x34,0x56,0x78,
489 0x23,0x45,0x67,0x89,0x34,0x56,0x78,0x9A };
490 byte b0[16] = { 0x01,0x23,0x45,0x67,0x12,0x34,0x56,0x78,
491 0x23,0x45,0x67,0x89,0x34,0x56,0x78,0x9A };
492 byte a1[16] = { 0xEE,0xA9,0xD0,0xA2,0x49,0xFD,0x3B,0xA6,
493 0xB3,0x43,0x6F,0xB8,0x9D,0x6D,0xCA,0x92 };
494 byte b1[16] = { 0xB2,0xC9,0x5E,0xB0,0x0C,0x31,0xAD,0x71,
495 0x80,0xAC,0x05,0xB8,0xE8,0x3D,0x69,0x6E };
496
497 for(i=0; i < 1000000; i++ ) {
498 cast_setkey( &c, b0, 16 );
499 encrypt_block( &c, a0, a0 );
500 encrypt_block( &c, a0+8, a0+8 );
501 cast_setkey( &c, a0, 16 );
502 encrypt_block( &c, b0, b0 );
503 encrypt_block( &c, b0+8, b0+8 );
504 }
505 if( memcmp( a0, a1, 16 ) || memcmp( b0, b1, 16 ) )
506 return "3";
507
508 }
509#endif
510 return NULL;
511}
512
513
514static void
515key_schedule( u32 *x, u32 *z, u32 *k )
516{
517
518#define xi(i) ((x[(i)/4] >> (8*(3-((i)%4)))) & 0xff)
519#define zi(i) ((z[(i)/4] >> (8*(3-((i)%4)))) & 0xff)
520
521 z[0] = x[0] ^ s5[xi(13)]^s6[xi(15)]^s7[xi(12)]^s8[xi(14)]^s7[xi( 8)];
522 z[1] = x[2] ^ s5[zi( 0)]^s6[zi( 2)]^s7[zi( 1)]^s8[zi( 3)]^s8[xi(10)];
523 z[2] = x[3] ^ s5[zi( 7)]^s6[zi( 6)]^s7[zi( 5)]^s8[zi( 4)]^s5[xi( 9)];
524 z[3] = x[1] ^ s5[zi(10)]^s6[zi( 9)]^s7[zi(11)]^s8[zi( 8)]^s6[xi(11)];
525 k[0] = s5[zi( 8)]^s6[zi( 9)]^s7[zi( 7)]^s8[zi( 6)]^s5[zi( 2)];
526 k[1] = s5[zi(10)]^s6[zi(11)]^s7[zi( 5)]^s8[zi( 4)]^s6[zi( 6)];
527 k[2] = s5[zi(12)]^s6[zi(13)]^s7[zi( 3)]^s8[zi( 2)]^s7[zi( 9)];
528 k[3] = s5[zi(14)]^s6[zi(15)]^s7[zi( 1)]^s8[zi( 0)]^s8[zi(12)];
529
530 x[0] = z[2] ^ s5[zi( 5)]^s6[zi( 7)]^s7[zi( 4)]^s8[zi( 6)]^s7[zi( 0)];
531 x[1] = z[0] ^ s5[xi( 0)]^s6[xi( 2)]^s7[xi( 1)]^s8[xi( 3)]^s8[zi( 2)];
532 x[2] = z[1] ^ s5[xi( 7)]^s6[xi( 6)]^s7[xi( 5)]^s8[xi( 4)]^s5[zi( 1)];
533 x[3] = z[3] ^ s5[xi(10)]^s6[xi( 9)]^s7[xi(11)]^s8[xi( 8)]^s6[zi( 3)];
534 k[4] = s5[xi( 3)]^s6[xi( 2)]^s7[xi(12)]^s8[xi(13)]^s5[xi( 8)];
535 k[5] = s5[xi( 1)]^s6[xi( 0)]^s7[xi(14)]^s8[xi(15)]^s6[xi(13)];
536 k[6] = s5[xi( 7)]^s6[xi( 6)]^s7[xi( 8)]^s8[xi( 9)]^s7[xi( 3)];
537 k[7] = s5[xi( 5)]^s6[xi( 4)]^s7[xi(10)]^s8[xi(11)]^s8[xi( 7)];
538
539 z[0] = x[0] ^ s5[xi(13)]^s6[xi(15)]^s7[xi(12)]^s8[xi(14)]^s7[xi( 8)];
540 z[1] = x[2] ^ s5[zi( 0)]^s6[zi( 2)]^s7[zi( 1)]^s8[zi( 3)]^s8[xi(10)];
541 z[2] = x[3] ^ s5[zi( 7)]^s6[zi( 6)]^s7[zi( 5)]^s8[zi( 4)]^s5[xi( 9)];
542 z[3] = x[1] ^ s5[zi(10)]^s6[zi( 9)]^s7[zi(11)]^s8[zi( 8)]^s6[xi(11)];
543 k[8] = s5[zi( 3)]^s6[zi( 2)]^s7[zi(12)]^s8[zi(13)]^s5[zi( 9)];
544 k[9] = s5[zi( 1)]^s6[zi( 0)]^s7[zi(14)]^s8[zi(15)]^s6[zi(12)];
545 k[10]= s5[zi( 7)]^s6[zi( 6)]^s7[zi( 8)]^s8[zi( 9)]^s7[zi( 2)];
546 k[11]= s5[zi( 5)]^s6[zi( 4)]^s7[zi(10)]^s8[zi(11)]^s8[zi( 6)];
547
548 x[0] = z[2] ^ s5[zi( 5)]^s6[zi( 7)]^s7[zi( 4)]^s8[zi( 6)]^s7[zi( 0)];
549 x[1] = z[0] ^ s5[xi( 0)]^s6[xi( 2)]^s7[xi( 1)]^s8[xi( 3)]^s8[zi( 2)];
550 x[2] = z[1] ^ s5[xi( 7)]^s6[xi( 6)]^s7[xi( 5)]^s8[xi( 4)]^s5[zi( 1)];
551 x[3] = z[3] ^ s5[xi(10)]^s6[xi( 9)]^s7[xi(11)]^s8[xi( 8)]^s6[zi( 3)];
552 k[12]= s5[xi( 8)]^s6[xi( 9)]^s7[xi( 7)]^s8[xi( 6)]^s5[xi( 3)];
553 k[13]= s5[xi(10)]^s6[xi(11)]^s7[xi( 5)]^s8[xi( 4)]^s6[xi( 7)];
554 k[14]= s5[xi(12)]^s6[xi(13)]^s7[xi( 3)]^s8[xi( 2)]^s7[xi( 8)];
555 k[15]= s5[xi(14)]^s6[xi(15)]^s7[xi( 1)]^s8[xi( 0)]^s8[xi(13)];
556
557#undef xi
558#undef zi
559}
560
561
562static gcry_err_code_t
563do_cast_setkey( CAST5_context *c, const byte *key, unsigned keylen )
564{
565 static int initialized;
566 static const char* selftest_failed;
567 int i;
568 u32 x[4];
569 u32 z[4];
570 u32 k[16];
571
572 if( !initialized )
573 {
574 initialized = 1;
575 selftest_failed = selftest();
576 if( selftest_failed )
577 log_error ("CAST5 selftest failed (%s).\n", selftest_failed );
578 }
579 if( selftest_failed )
580 return GPG_ERR_SELFTEST_FAILED;
581
582 if( keylen != 16 )
583 return GPG_ERR_INV_KEYLEN;
584
585 x[0] = key[0] << 24 | key[1] << 16 | key[2] << 8 | key[3];
586 x[1] = key[4] << 24 | key[5] << 16 | key[6] << 8 | key[7];
587 x[2] = key[8] << 24 | key[9] << 16 | key[10] << 8 | key[11];
588 x[3] = key[12] << 24 | key[13] << 16 | key[14] << 8 | key[15];
589
590 key_schedule( x, z, k );
591 for(i=0; i < 16; i++ )
592 c->Km[i] = k[i];
593 key_schedule( x, z, k );
594 for(i=0; i < 16; i++ )
595 c->Kr[i] = k[i] & 0x1f;
596
597 memset(&x,0, sizeof x);
598 memset(&z,0, sizeof z);
599 memset(&k,0, sizeof k);
600
601#undef xi
602#undef zi
603 return GPG_ERR_NO_ERROR;
604}
605
606static gcry_err_code_t
607cast_setkey (void *context, const byte *key, unsigned keylen )
608{
609 CAST5_context *c = (CAST5_context *) context;
610 gcry_err_code_t rc = do_cast_setkey (c, key, keylen);
611 _gcry_burn_stack (96+7*sizeof(void*));
612 return rc;
613}
614
615
616gcry_cipher_spec_t _gcry_cipher_spec_cast5 =
617 {
618 "CAST5", NULL, NULL, CAST5_BLOCKSIZE, 128, sizeof (CAST5_context),
619 cast_setkey, encrypt_block, decrypt_block,
620 };
diff --git a/pwmanager/libcrypt/cipher/cipher.c b/pwmanager/libcrypt/cipher/cipher.c
new file mode 100644
index 0000000..78ea940
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/cipher.c
@@ -0,0 +1,1391 @@
1 /* cipher.c -cipher dispatcher
2 * Copyright (C) 1998,1999,2000,2001,2002,2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <config.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <errno.h>
26#include <assert.h>
27
28#include "g10lib.h"
29#include "cipher.h"
30#include "ath.h"
31
32#define MAX_BLOCKSIZE 16
33#define TABLE_SIZE 14
34#define CTX_MAGIC_NORMAL 0x24091964
35#define CTX_MAGIC_SECURE 0x46919042
36
37/* This is the list of the default ciphers, which are included in
38 libgcrypt. */
39static struct cipher_table_entry
40{
41 gcry_cipher_spec_t *cipher;
42 unsigned int algorithm;
43} cipher_table[] =
44 {
45#if USE_BLOWFISH
46 { &_gcry_cipher_spec_blowfish, GCRY_CIPHER_BLOWFISH },
47#endif
48#if USE_DES
49 { &_gcry_cipher_spec_des, GCRY_CIPHER_DES },
50 { &_gcry_cipher_spec_tripledes, GCRY_CIPHER_3DES },
51#endif
52#if USE_ARCFOUR
53 { &_gcry_cipher_spec_arcfour, GCRY_CIPHER_ARCFOUR },
54#endif
55#if USE_CAST5
56 { &_gcry_cipher_spec_cast5, GCRY_CIPHER_CAST5 },
57#endif
58#if USE_AES
59 { &_gcry_cipher_spec_aes, GCRY_CIPHER_AES },
60 { &_gcry_cipher_spec_aes192, GCRY_CIPHER_AES192 },
61 { &_gcry_cipher_spec_aes256, GCRY_CIPHER_AES256 },
62#endif
63#if USE_TWOFISH
64 { &_gcry_cipher_spec_twofish, GCRY_CIPHER_TWOFISH },
65 { &_gcry_cipher_spec_twofish128, GCRY_CIPHER_TWOFISH128 },
66#endif
67#if USE_SERPENT
68 { &_gcry_cipher_spec_serpent128, GCRY_CIPHER_SERPENT128 },
69 { &_gcry_cipher_spec_serpent192, GCRY_CIPHER_SERPENT192 },
70 { &_gcry_cipher_spec_serpent256, GCRY_CIPHER_SERPENT256 },
71#endif
72#ifdef USE_RFC2268
73 { &_gcry_cipher_spec_rfc2268_40, GCRY_CIPHER_RFC2268_40 },
74#endif
75 { NULL },
76 };
77
78/* List of registered ciphers. */
79static gcry_module_t ciphers_registered;
80
81/* This is the lock protecting CIPHERS_REGISTERED. */
82static ath_mutex_t ciphers_registered_lock = ATH_MUTEX_INITIALIZER;
83
84/* Flag to check wether the default ciphers have already been
85 registered. */
86static int default_ciphers_registered;
87
88/* Convenient macro for registering the default ciphers. */
89#define REGISTER_DEFAULT_CIPHERS \
90 do \
91 { \
92 ath_mutex_lock (&ciphers_registered_lock); \
93 if (! default_ciphers_registered) \
94 { \
95 gcry_cipher_register_default (); \
96 default_ciphers_registered = 1; \
97 } \
98 ath_mutex_unlock (&ciphers_registered_lock); \
99 } \
100 while (0)
101
102/* The handle structure. */
103struct gcry_cipher_handle
104{
105 int magic;
106 size_t actual_handle_size; /* Allocated size of this handle. */
107 gcry_cipher_spec_t *cipher;
108 gcry_module_t module;
109 int mode;
110 unsigned int flags;
111 unsigned char iv[MAX_BLOCKSIZE];/* (this should be ulong aligned) */
112 unsigned char lastiv[MAX_BLOCKSIZE];
113 int unused; /* in IV */
114 unsigned char ctr[MAX_BLOCKSIZE]; /* For Counter (CTR) mode. */
115 PROPERLY_ALIGNED_TYPE context;
116};
117
118
119/* These dummy functions are used in case a cipher implementation
120 refuses to provide it's own functions. */
121
122static gcry_err_code_t
123dummy_setkey (void *c, const unsigned char *key, unsigned keylen)
124{
125 return GPG_ERR_NO_ERROR;
126}
127
128static void
129dummy_encrypt_block (void *c,
130 unsigned char *outbuf, const unsigned char *inbuf)
131{
132 BUG();
133}
134
135static void
136dummy_decrypt_block (void *c,
137 unsigned char *outbuf, const unsigned char *inbuf)
138{
139 BUG();
140}
141
142static void
143dummy_encrypt_stream (void *c,
144 unsigned char *outbuf, const unsigned char *inbuf,
145 unsigned int n)
146{
147 BUG();
148}
149
150static void
151dummy_decrypt_stream (void *c,
152 unsigned char *outbuf, const unsigned char *inbuf,
153 unsigned int n)
154{
155 BUG();
156}
157
158
159/* Internal function. Register all the ciphers included in
160 CIPHER_TABLE. Note, that this function gets only used by the macro
161 REGISTER_DEFAULT_CIPHERS which protects it using a mutex. */
162static void
163gcry_cipher_register_default (void)
164{
165 gcry_err_code_t err = GPG_ERR_NO_ERROR;
166 int i;
167
168 for (i = 0; !err && cipher_table[i].cipher; i++)
169 {
170 if (! cipher_table[i].cipher->setkey)
171 cipher_table[i].cipher->setkey = dummy_setkey;
172 if (! cipher_table[i].cipher->encrypt)
173 cipher_table[i].cipher->encrypt = dummy_encrypt_block;
174 if (! cipher_table[i].cipher->decrypt)
175 cipher_table[i].cipher->decrypt = dummy_decrypt_block;
176 if (! cipher_table[i].cipher->stencrypt)
177 cipher_table[i].cipher->stencrypt = dummy_encrypt_stream;
178 if (! cipher_table[i].cipher->stdecrypt)
179 cipher_table[i].cipher->stdecrypt = dummy_decrypt_stream;
180
181 err = _gcry_module_add (&ciphers_registered,
182 cipher_table[i].algorithm,
183 (void *) cipher_table[i].cipher,
184 NULL);
185 }
186
187 if (err)
188 BUG ();
189}
190
191/* Internal callback function. Used via _gcry_module_lookup. */
192static int
193gcry_cipher_lookup_func_name (void *spec, void *data)
194{
195 gcry_cipher_spec_t *cipher = (gcry_cipher_spec_t *) spec;
196 char *name = (char *) data;
197 const char **aliases = cipher->aliases;
198 int i, ret = ! stricmp (name, cipher->name);
199
200 if (aliases)
201 for (i = 0; aliases[i] && (! ret); i++)
202 ret = ! stricmp (name, aliases[i]);
203
204 return ret;
205}
206
207/* Internal callback function. Used via _gcry_module_lookup. */
208static int
209gcry_cipher_lookup_func_oid (void *spec, void *data)
210{
211 gcry_cipher_spec_t *cipher = (gcry_cipher_spec_t *) spec;
212 char *oid = (char *) data;
213 gcry_cipher_oid_spec_t *oid_specs = cipher->oids;
214 int ret = 0, i;
215
216 if (oid_specs)
217 for (i = 0; oid_specs[i].oid && (! ret); i++)
218 if (! stricmp (oid, oid_specs[i].oid))
219 ret = 1;
220
221 return ret;
222}
223
224/* Internal function. Lookup a cipher entry by it's name. */
225static gcry_module_t
226gcry_cipher_lookup_name (const char *name)
227{
228 gcry_module_t cipher;
229
230 cipher = _gcry_module_lookup (ciphers_registered, (void *) name,
231 gcry_cipher_lookup_func_name);
232
233 return cipher;
234}
235
236/* Internal function. Lookup a cipher entry by it's oid. */
237static gcry_module_t
238gcry_cipher_lookup_oid (const char *oid)
239{
240 gcry_module_t cipher;
241
242 cipher = _gcry_module_lookup (ciphers_registered, (void *) oid,
243 gcry_cipher_lookup_func_oid);
244
245 return cipher;
246}
247
248/* Register a new cipher module whose specification can be found in
249 CIPHER. On success, a new algorithm ID is stored in ALGORITHM_ID
250 and a pointer representhing this module is stored in MODULE. */
251gcry_error_t
252gcry_cipher_register (gcry_cipher_spec_t *cipher,
253 unsigned int *algorithm_id,
254 gcry_module_t *module)
255{
256 gcry_err_code_t err = 0;
257 gcry_module_t mod;
258
259 ath_mutex_lock (&ciphers_registered_lock);
260 err = _gcry_module_add (&ciphers_registered, 0,
261 (void *) cipher, &mod);
262 ath_mutex_unlock (&ciphers_registered_lock);
263
264 if (! err)
265 {
266 *module = mod;
267 *algorithm_id = mod->mod_id;
268 }
269
270 return gcry_error (err);
271}
272
273/* Unregister the cipher identified by MODULE, which must have been
274 registered with gcry_cipher_register. */
275void
276gcry_cipher_unregister (gcry_module_t module)
277{
278 ath_mutex_lock (&ciphers_registered_lock);
279 _gcry_module_release (module);
280 ath_mutex_unlock (&ciphers_registered_lock);
281}
282
283/* Locate the OID in the oid table and return the index or -1 when not
284 found. An opitonal "oid." or "OID." prefix in OID is ignored, the
285 OID is expected to be in standard IETF dotted notation. The
286 internal algorithm number is returned in ALGORITHM unless it
287 ispassed as NULL. A pointer to the specification of the module
288 implementing this algorithm is return in OID_SPEC unless passed as
289 NULL.*/
290static int
291search_oid (const char *oid, int *algorithm, gcry_cipher_oid_spec_t *oid_spec)
292{
293 gcry_module_t module;
294 int ret = 0;
295
296 if (oid && ((! strncmp (oid, "oid.", 4))
297 || (! strncmp (oid, "OID.", 4))))
298 oid += 4;
299
300 module = gcry_cipher_lookup_oid (oid);
301 if (module)
302 {
303 gcry_cipher_spec_t *cipher = module->spec;
304 int i;
305
306 for (i = 0; cipher->oids[i].oid && !ret; i++)
307 if (! stricmp (oid, cipher->oids[i].oid))
308 {
309 if (algorithm)
310 *algorithm = module->mod_id;
311 if (oid_spec)
312 *oid_spec = cipher->oids[i];
313 ret = 1;
314 }
315 _gcry_module_release (module);
316 }
317
318 return ret;
319}
320
321/* Map STRING to the cipher algorithm identifier. Returns the
322 algorithm ID of the cipher for the given name or 0 if the name is
323 not known. It is valid to pass NULL for STRING which results in a
324 return value of 0. */
325int
326gcry_cipher_map_name (const char *string)
327{
328 gcry_module_t cipher;
329 int ret, algorithm = 0;
330
331 if (! string)
332 return 0;
333
334 REGISTER_DEFAULT_CIPHERS;
335
336 /* If the string starts with a digit (optionally prefixed with
337 either "OID." or "oid."), we first look into our table of ASN.1
338 object identifiers to figure out the algorithm */
339
340 ath_mutex_lock (&ciphers_registered_lock);
341
342 ret = search_oid (string, &algorithm, NULL);
343 if (! ret)
344 {
345 cipher = gcry_cipher_lookup_name (string);
346 if (cipher)
347 {
348 algorithm = cipher->mod_id;
349 _gcry_module_release (cipher);
350 }
351 }
352
353 ath_mutex_unlock (&ciphers_registered_lock);
354
355 return algorithm;
356}
357
358
359/* Given a STRING with an OID in dotted decimal notation, this
360 function returns the cipher mode (GCRY_CIPHER_MODE_*) associated
361 with that OID or 0 if no mode is known. Passing NULL for string
362 yields a return value of 0. */
363int
364gcry_cipher_mode_from_oid (const char *string)
365{
366 gcry_cipher_oid_spec_t oid_spec;
367 int ret = 0, mode = 0;
368
369 if (!string)
370 return 0;
371
372 ath_mutex_lock (&ciphers_registered_lock);
373 ret = search_oid (string, NULL, &oid_spec);
374 if (ret)
375 mode = oid_spec.mode;
376 ath_mutex_unlock (&ciphers_registered_lock);
377
378 return mode;
379}
380
381
382/* Map the cipher algorithm identifier ALGORITHM to a string
383 representing this algorithm. This string is the default name as
384 used by Libgcrypt. NULL is returned for an unknown algorithm. */
385static const char *
386cipher_algo_to_string (int algorithm)
387{
388 gcry_module_t cipher;
389 const char *name = NULL;
390
391 REGISTER_DEFAULT_CIPHERS;
392
393 ath_mutex_lock (&ciphers_registered_lock);
394 cipher = _gcry_module_lookup_id (ciphers_registered, algorithm);
395 if (cipher)
396 {
397 name = ((gcry_cipher_spec_t *) cipher->spec)->name;
398 _gcry_module_release (cipher);
399 }
400 ath_mutex_unlock (&ciphers_registered_lock);
401
402 return name;
403}
404
405/* Map the cipher algorithm identifier ALGORITHM to a string
406 representing this algorithm. This string is the default name as
407 used by Libgcrypt. An pointer to an empty string is returned for
408 an unknown algorithm. NULL is never returned. */
409const char *
410gcry_cipher_algo_name (int algorithm)
411{
412 const char *s = cipher_algo_to_string (algorithm);
413 return s ? s : "";
414}
415
416
417/* Flag the cipher algorithm with the identifier ALGORITHM as
418 disabled. There is no error return, the function does nothing for
419 unknown algorithms. Disabled algorithms are vitually not available
420 in Libgcrypt. */
421static void
422disable_cipher_algo (int algorithm)
423{
424 gcry_module_t cipher;
425
426 REGISTER_DEFAULT_CIPHERS;
427
428 ath_mutex_lock (&ciphers_registered_lock);
429 cipher = _gcry_module_lookup_id (ciphers_registered, algorithm);
430 if (cipher)
431 {
432 if (! (cipher->flags & FLAG_MODULE_DISABLED))
433 cipher->flags |= FLAG_MODULE_DISABLED;
434 _gcry_module_release (cipher);
435 }
436 ath_mutex_unlock (&ciphers_registered_lock);
437}
438
439
440/* Return 0 if the cipher algorithm with indentifier ALGORITHM is
441 available. Returns a basic error code value if it is not available. */
442static gcry_err_code_t
443check_cipher_algo (int algorithm)
444{
445 gcry_err_code_t err = GPG_ERR_NO_ERROR;
446 gcry_module_t cipher;
447
448 REGISTER_DEFAULT_CIPHERS;
449
450 ath_mutex_lock (&ciphers_registered_lock);
451 cipher = _gcry_module_lookup_id (ciphers_registered, algorithm);
452 if (cipher)
453 {
454 if (cipher->flags & FLAG_MODULE_DISABLED)
455 err = GPG_ERR_CIPHER_ALGO;
456 _gcry_module_release (cipher);
457 }
458 else
459 err = GPG_ERR_CIPHER_ALGO;
460 ath_mutex_unlock (&ciphers_registered_lock);
461
462 return err;
463}
464
465
466/* Return the standard length of the key for the cipher algorithm with
467 the identifier ALGORITHM. This function expects a valid algorithm
468 and will abort if the algorithm is not available or the length of
469 the key is not known. */
470static unsigned int
471cipher_get_keylen (int algorithm)
472{
473 gcry_module_t cipher;
474 unsigned len = 0;
475
476 REGISTER_DEFAULT_CIPHERS;
477
478 ath_mutex_lock (&ciphers_registered_lock);
479 cipher = _gcry_module_lookup_id (ciphers_registered, algorithm);
480 if (cipher)
481 {
482 len = ((gcry_cipher_spec_t *) cipher->spec)->keylen;
483 if (! len)
484 log_bug ("cipher %d w/o key length\n", algorithm);
485 _gcry_module_release (cipher);
486 }
487 else
488 log_bug ("cipher %d not found\n", algorithm);
489 ath_mutex_unlock (&ciphers_registered_lock);
490
491 return len;
492}
493
494/* Return the block length of the cipher algorithm with the identifier
495 ALGORITHM. This function expects a valid algorithm and will abort
496 if the algorithm is not available or the length of the key is not
497 known. */
498static unsigned int
499cipher_get_blocksize (int algorithm)
500{
501 gcry_module_t cipher;
502 unsigned len = 0;
503
504 REGISTER_DEFAULT_CIPHERS;
505
506 ath_mutex_lock (&ciphers_registered_lock);
507 cipher = _gcry_module_lookup_id (ciphers_registered, algorithm);
508 if (cipher)
509 {
510 len = ((gcry_cipher_spec_t *) cipher->spec)->blocksize;
511 if (! len)
512 log_bug ("cipher %d w/o blocksize\n", algorithm);
513 _gcry_module_release (cipher);
514 }
515 else
516 log_bug ("cipher %d not found\n", algorithm);
517 ath_mutex_unlock (&ciphers_registered_lock);
518
519 return len;
520}
521
522
523/*
524 Open a cipher handle for use with cipher algorithm ALGORITHM, using
525 the cipher mode MODE (one of the GCRY_CIPHER_MODE_*) and return a
526 handle in HANDLE. Put NULL into HANDLE and return an error code if
527 something goes wrong. FLAGS may be used to modify the
528 operation. The defined flags are:
529
530 GCRY_CIPHER_SECURE: allocate all internal buffers in secure memory.
531 GCRY_CIPHER_ENABLE_SYNC: Enable the sync operation as used in OpenPGP.
532 GCRY_CIPHER_CBC_CTS: Enable CTS mode.
533 GCRY_CIPHER_CBC_MAC: Enable MAC mode.
534
535 Values for these flags may be combined using OR.
536 */
537gcry_error_t
538gcry_cipher_open (gcry_cipher_hd_t *handle,
539 int algo, int mode, unsigned int flags)
540{
541 int secure = (flags & GCRY_CIPHER_SECURE);
542 gcry_cipher_spec_t *cipher = NULL;
543 gcry_module_t module = NULL;
544 gcry_cipher_hd_t h = NULL;
545 gcry_err_code_t err = 0;
546
547 /* If the application missed to call the random poll function, we do
548 it here to ensure that it is used once in a while. */
549 _gcry_fast_random_poll ();
550
551 REGISTER_DEFAULT_CIPHERS;
552
553 /* Fetch the according module and check wether the cipher is marked
554 available for use. */
555 ath_mutex_lock (&ciphers_registered_lock);
556 module = _gcry_module_lookup_id (ciphers_registered, algo);
557 if (module)
558 {
559 /* Found module. */
560
561 if (module->flags & FLAG_MODULE_DISABLED)
562 {
563 /* Not available for use. */
564 err = GPG_ERR_CIPHER_ALGO;
565 _gcry_module_release (module);
566 }
567 else
568 cipher = (gcry_cipher_spec_t *) module->spec;
569 }
570 else
571 err = GPG_ERR_CIPHER_ALGO;
572 ath_mutex_unlock (&ciphers_registered_lock);
573
574 /* check flags */
575 if ((! err)
576 && ((flags & ~(0
577 | GCRY_CIPHER_SECURE
578 | GCRY_CIPHER_ENABLE_SYNC
579 | GCRY_CIPHER_CBC_CTS
580 | GCRY_CIPHER_CBC_MAC))
581 || (flags & GCRY_CIPHER_CBC_CTS & GCRY_CIPHER_CBC_MAC)))
582 err = GPG_ERR_CIPHER_ALGO;
583
584 /* check that a valid mode has been requested */
585 if (! err)
586 switch (mode)
587 {
588 case GCRY_CIPHER_MODE_ECB:
589 case GCRY_CIPHER_MODE_CBC:
590 case GCRY_CIPHER_MODE_CFB:
591 case GCRY_CIPHER_MODE_CTR:
592 if ((cipher->encrypt == dummy_encrypt_block)
593 || (cipher->decrypt == dummy_decrypt_block))
594 err = GPG_ERR_INV_CIPHER_MODE;
595 break;
596
597 case GCRY_CIPHER_MODE_STREAM:
598 if ((cipher->stencrypt == dummy_encrypt_stream)
599 || (cipher->stdecrypt == dummy_decrypt_stream))
600 err = GPG_ERR_INV_CIPHER_MODE;
601 break;
602
603 case GCRY_CIPHER_MODE_NONE:
604 /* FIXME: issue a warning when this mode is used */
605 break;
606
607 default:
608 err = GPG_ERR_INV_CIPHER_MODE;
609 }
610
611 /* ? FIXME: perform selftest here and mark this with a flag in
612 cipher_table ? */
613
614 if (! err)
615 {
616 size_t size = (sizeof (*h)
617 + 2 * cipher->contextsize
618 - sizeof (PROPERLY_ALIGNED_TYPE));
619
620 if (secure)
621 h = gcry_calloc_secure (1, size);
622 else
623 h = gcry_calloc (1, size);
624
625 if (! h)
626 err = gpg_err_code_from_errno (errno);
627 else
628 {
629 h->magic = secure ? CTX_MAGIC_SECURE : CTX_MAGIC_NORMAL;
630 h->actual_handle_size = size;
631 h->cipher = cipher;
632 h->module = module;
633 h->mode = mode;
634 h->flags = flags;
635 }
636 }
637
638 /* Done. */
639
640 if (err)
641 {
642 if (module)
643 {
644 /* Release module. */
645 ath_mutex_lock (&ciphers_registered_lock);
646 _gcry_module_release (module);
647 ath_mutex_unlock (&ciphers_registered_lock);
648 }
649 }
650
651 *handle = err ? NULL : h;
652
653 return gcry_error (err);
654}
655
656
657/* Release all resources associated with the cipher handle H. H may be
658 NULL in which case this is a no-operation. */
659void
660gcry_cipher_close (gcry_cipher_hd_t h)
661{
662 if (! h)
663 return;
664
665 if ((h->magic != CTX_MAGIC_SECURE)
666 && (h->magic != CTX_MAGIC_NORMAL))
667 _gcry_fatal_error(GPG_ERR_INTERNAL,
668 "gcry_cipher_close: already closed/invalid handle");
669 else
670 h->magic = 0;
671
672 /* Release module. */
673 ath_mutex_lock (&ciphers_registered_lock);
674 _gcry_module_release (h->module);
675 ath_mutex_unlock (&ciphers_registered_lock);
676
677 /* We always want to wipe out the memory even when the context has
678 been allocated in secure memory. The user might have disabled
679 secure memory or is using his own implementation which does not
680 do the wiping. To accomplish this we need to keep track of the
681 actual size of this structure because we have no way to known
682 how large the allocated area was when using a standard malloc. */
683 wipememory (h, h->actual_handle_size);
684
685 gcry_free (h);
686}
687
688
689/* Set the key to be used for the encryption context C to KEY with
690 length KEYLEN. The length should match the required length. */
691static gcry_error_t
692cipher_setkey (gcry_cipher_hd_t c, byte *key, unsigned keylen)
693{
694 gcry_err_code_t ret;
695
696 ret = (*c->cipher->setkey) (&c->context.c, key, keylen);
697 if (! ret)
698 /* Duplicate initial context. */
699 memcpy ((void *) ((char *) &c->context.c + c->cipher->contextsize),
700 (void *) &c->context.c,
701 c->cipher->contextsize);
702
703 return gcry_error (ret);
704}
705
706
707/* Set the IV to be used for the encryption context C to IV with
708 length IVLEN. The length should match the required length. */
709static void
710cipher_setiv( gcry_cipher_hd_t c, const byte *iv, unsigned ivlen )
711{
712 memset( c->iv, 0, c->cipher->blocksize );
713 if( iv ) {
714 if( ivlen != c->cipher->blocksize )
715 log_info("WARNING: cipher_setiv: ivlen=%u blklen=%u\n",
716 ivlen, (unsigned) c->cipher->blocksize );
717 if (ivlen > c->cipher->blocksize)
718 ivlen = c->cipher->blocksize;
719 memcpy( c->iv, iv, ivlen );
720 }
721 c->unused = 0;
722}
723
724
725/* Reset the cipher context to the initial contex. This is basically
726 the same as an release followed by a new. */
727static void
728cipher_reset (gcry_cipher_hd_t c)
729{
730 memcpy (&c->context.c,
731 (char *) &c->context.c + c->cipher->contextsize,
732 c->cipher->contextsize);
733 memset (c->iv, 0, c->cipher->blocksize);
734 memset (c->lastiv, 0, c->cipher->blocksize);
735 memset (c->ctr, 0, c->cipher->blocksize);
736}
737
738
739static void
740do_ecb_encrypt( gcry_cipher_hd_t c, byte *outbuf, const byte *inbuf,
741 unsigned int nblocks )
742{
743 unsigned int n;
744
745 for(n=0; n < nblocks; n++ ) {
746 c->cipher->encrypt ( &c->context.c, outbuf, (byte*)/*arggg*/inbuf );
747 inbuf += c->cipher->blocksize;
748 outbuf += c->cipher->blocksize;
749 }
750}
751
752static void
753do_ecb_decrypt( gcry_cipher_hd_t c, byte *outbuf, const byte *inbuf,
754 unsigned int nblocks )
755{
756 unsigned n;
757
758 for(n=0; n < nblocks; n++ ) {
759 c->cipher->decrypt ( &c->context.c, outbuf, (byte*)/*arggg*/inbuf );
760 inbuf += c->cipher->blocksize;
761 outbuf += c->cipher->blocksize;
762 }
763}
764
765static void
766do_cbc_encrypt( gcry_cipher_hd_t c, byte *outbuf, const byte *inbuf,
767 unsigned int nbytes )
768{
769 unsigned int n;
770 byte *ivp;
771 int i;
772 size_t blocksize = c->cipher->blocksize;
773 unsigned nblocks = nbytes / blocksize;
774
775 if ((c->flags & GCRY_CIPHER_CBC_CTS) && nbytes > blocksize) {
776 if ((nbytes % blocksize) == 0)
777 nblocks--;
778 }
779
780 for(n=0; n < nblocks; n++ ) {
781 /* fixme: the xor should work on words and not on
782 * bytes. Maybe it is a good idea to enhance the cipher backend
783 * API to allow for CBC handling direct in the backend */
784 for(ivp=c->iv,i=0; i < blocksize; i++ )
785 outbuf[i] = inbuf[i] ^ *ivp++;
786 c->cipher->encrypt ( &c->context.c, outbuf, outbuf );
787 memcpy(c->iv, outbuf, blocksize );
788 inbuf += c->cipher->blocksize;
789 if (!(c->flags & GCRY_CIPHER_CBC_MAC))
790 outbuf += c->cipher->blocksize;
791 }
792
793 if ((c->flags & GCRY_CIPHER_CBC_CTS) && nbytes > blocksize)
794 {
795 int restbytes;
796
797 if ((nbytes % blocksize) == 0)
798 restbytes = blocksize;
799 else
800 restbytes = nbytes % blocksize;
801
802 memcpy(outbuf, outbuf - c->cipher->blocksize, restbytes);
803 outbuf -= c->cipher->blocksize;
804
805 for(ivp=c->iv,i=0; i < restbytes; i++ )
806 outbuf[i] = inbuf[i] ^ *ivp++;
807 for(; i < blocksize; i++ )
808 outbuf[i] = 0 ^ *ivp++;
809
810 c->cipher->encrypt ( &c->context.c, outbuf, outbuf );
811 memcpy(c->iv, outbuf, blocksize );
812 }
813}
814
815static void
816do_cbc_decrypt( gcry_cipher_hd_t c, byte *outbuf, const byte *inbuf,
817 unsigned int nbytes )
818{
819 unsigned int n;
820 byte *ivp;
821 int i;
822 size_t blocksize = c->cipher->blocksize;
823 unsigned int nblocks = nbytes / blocksize;
824
825 if ((c->flags & GCRY_CIPHER_CBC_CTS) && nbytes > blocksize) {
826 nblocks--;
827 if ((nbytes % blocksize) == 0)
828 nblocks--;
829 memcpy(c->lastiv, c->iv, blocksize );
830 }
831
832 for(n=0; n < nblocks; n++ ) {
833 /* Because outbuf and inbuf might be the same, we have
834 * to save the original ciphertext block. We use lastiv
835 * for this here because it is not used otherwise. */
836 memcpy(c->lastiv, inbuf, blocksize );
837 c->cipher->decrypt ( &c->context.c, outbuf, inbuf );
838 for(ivp=c->iv,i=0; i < blocksize; i++ )
839 outbuf[i] ^= *ivp++;
840 memcpy(c->iv, c->lastiv, blocksize );
841 inbuf += c->cipher->blocksize;
842 outbuf += c->cipher->blocksize;
843 }
844
845 if ((c->flags & GCRY_CIPHER_CBC_CTS) && nbytes > blocksize) {
846 int restbytes;
847
848 if ((nbytes % blocksize) == 0)
849 restbytes = blocksize;
850 else
851 restbytes = nbytes % blocksize;
852
853 memcpy(c->lastiv, c->iv, blocksize ); /* save Cn-2 */
854 memcpy(c->iv, inbuf + blocksize, restbytes ); /* save Cn */
855
856 c->cipher->decrypt ( &c->context.c, outbuf, inbuf );
857 for(ivp=c->iv,i=0; i < restbytes; i++ )
858 outbuf[i] ^= *ivp++;
859
860 memcpy(outbuf + blocksize, outbuf, restbytes);
861 for(i=restbytes; i < blocksize; i++)
862 c->iv[i] = outbuf[i];
863 c->cipher->decrypt ( &c->context.c, outbuf, c->iv );
864 for(ivp=c->lastiv,i=0; i < blocksize; i++ )
865 outbuf[i] ^= *ivp++;
866 /* c->lastiv is now really lastlastiv, does this matter? */
867 }
868}
869
870
871static void
872do_cfb_encrypt( gcry_cipher_hd_t c,
873 byte *outbuf, const byte *inbuf, unsigned nbytes )
874{
875 byte *ivp;
876 size_t blocksize = c->cipher->blocksize;
877
878 if( nbytes <= c->unused ) {
879 /* Short enough to be encoded by the remaining XOR mask. */
880 /* XOR the input with the IV and store input into IV. */
881 for (ivp=c->iv+c->cipher->blocksize - c->unused;
882 nbytes;
883 nbytes--, c->unused-- )
884 *outbuf++ = (*ivp++ ^= *inbuf++);
885 return;
886 }
887
888 if( c->unused ) {
889 /* XOR the input with the IV and store input into IV */
890 nbytes -= c->unused;
891 for(ivp=c->iv+blocksize - c->unused; c->unused; c->unused-- )
892 *outbuf++ = (*ivp++ ^= *inbuf++);
893 }
894
895 /* Now we can process complete blocks. */
896 while( nbytes >= blocksize ) {
897 int i;
898 /* Encrypt the IV (and save the current one). */
899 memcpy( c->lastiv, c->iv, blocksize );
900 c->cipher->encrypt ( &c->context.c, c->iv, c->iv );
901 /* XOR the input with the IV and store input into IV */
902 for(ivp=c->iv,i=0; i < blocksize; i++ )
903 *outbuf++ = (*ivp++ ^= *inbuf++);
904 nbytes -= blocksize;
905 }
906 if( nbytes ) { /* process the remaining bytes */
907 /* encrypt the IV (and save the current one) */
908 memcpy( c->lastiv, c->iv, blocksize );
909 c->cipher->encrypt ( &c->context.c, c->iv, c->iv );
910 c->unused = blocksize;
911 /* and apply the xor */
912 c->unused -= nbytes;
913 for(ivp=c->iv; nbytes; nbytes-- )
914 *outbuf++ = (*ivp++ ^= *inbuf++);
915 }
916}
917
918static void
919do_cfb_decrypt( gcry_cipher_hd_t c,
920 byte *outbuf, const byte *inbuf, unsigned int nbytes )
921{
922 byte *ivp;
923 ulong temp;
924 size_t blocksize = c->cipher->blocksize;
925
926 if( nbytes <= c->unused ) {
927 /* Short enough to be encoded by the remaining XOR mask. */
928 /* XOR the input with the IV and store input into IV. */
929 for(ivp=c->iv+blocksize - c->unused; nbytes; nbytes--,c->unused--) {
930 temp = *inbuf++;
931 *outbuf++ = *ivp ^ temp;
932 *ivp++ = temp;
933 }
934 return;
935 }
936
937 if( c->unused ) {
938 /* XOR the input with the IV and store input into IV. */
939 nbytes -= c->unused;
940 for(ivp=c->iv+blocksize - c->unused; c->unused; c->unused-- ) {
941 temp = *inbuf++;
942 *outbuf++ = *ivp ^ temp;
943 *ivp++ = temp;
944 }
945 }
946
947 /* now we can process complete blocks */
948 while( nbytes >= blocksize ) {
949 int i;
950 /* encrypt the IV (and save the current one) */
951 memcpy( c->lastiv, c->iv, blocksize );
952 c->cipher->encrypt ( &c->context.c, c->iv, c->iv );
953 /* XOR the input with the IV and store input into IV */
954 for(ivp=c->iv,i=0; i < blocksize; i++ ) {
955 temp = *inbuf++;
956 *outbuf++ = *ivp ^ temp;
957 *ivp++ = temp;
958 }
959 nbytes -= blocksize;
960 }
961 if( nbytes ) { /* process the remaining bytes */
962 /* encrypt the IV (and save the current one) */
963 memcpy( c->lastiv, c->iv, blocksize );
964 c->cipher->encrypt ( &c->context.c, c->iv, c->iv );
965 c->unused = blocksize;
966 /* and apply the xor */
967 c->unused -= nbytes;
968 for(ivp=c->iv; nbytes; nbytes-- ) {
969 temp = *inbuf++;
970 *outbuf++ = *ivp ^ temp;
971 *ivp++ = temp;
972 }
973 }
974}
975
976
977static void
978do_ctr_encrypt( gcry_cipher_hd_t c, byte *outbuf, const byte *inbuf,
979 unsigned int nbytes )
980{
981 unsigned int n;
982 byte tmp[MAX_BLOCKSIZE];
983 int i;
984
985 for(n=0; n < nbytes; n++)
986 {
987 if ((n % c->cipher->blocksize) == 0)
988 {
989 c->cipher->encrypt (&c->context.c, tmp, c->ctr);
990
991 for (i = c->cipher->blocksize; i > 0; i--)
992 {
993 c->ctr[i-1]++;
994 if (c->ctr[i-1] != 0)
995 break;
996 }
997 }
998
999 /* XOR input with encrypted counter and store in output. */
1000 outbuf[n] = inbuf[n] ^ tmp[n % c->cipher->blocksize];
1001 }
1002}
1003
1004static void
1005do_ctr_decrypt( gcry_cipher_hd_t c, byte *outbuf, const byte *inbuf,
1006 unsigned int nbytes )
1007{
1008 do_ctr_encrypt (c, outbuf, inbuf, nbytes);
1009}
1010
1011
1012/****************
1013 * Encrypt INBUF to OUTBUF with the mode selected at open.
1014 * inbuf and outbuf may overlap or be the same.
1015 * Depending on the mode some contraints apply to NBYTES.
1016 */
1017static gcry_err_code_t
1018cipher_encrypt (gcry_cipher_hd_t c, byte *outbuf,
1019 const byte *inbuf, unsigned int nbytes)
1020{
1021 gcry_err_code_t rc = GPG_ERR_NO_ERROR;
1022
1023 switch( c->mode ) {
1024 case GCRY_CIPHER_MODE_ECB:
1025 if (!(nbytes%c->cipher->blocksize))
1026 do_ecb_encrypt(c, outbuf, inbuf, nbytes/c->cipher->blocksize );
1027 else
1028 rc = GPG_ERR_INV_ARG;
1029 break;
1030 case GCRY_CIPHER_MODE_CBC:
1031 if (!(nbytes%c->cipher->blocksize)
1032 || (nbytes > c->cipher->blocksize
1033 && (c->flags & GCRY_CIPHER_CBC_CTS)))
1034 do_cbc_encrypt(c, outbuf, inbuf, nbytes );
1035 else
1036 rc = GPG_ERR_INV_ARG;
1037 break;
1038 case GCRY_CIPHER_MODE_CFB:
1039 do_cfb_encrypt(c, outbuf, inbuf, nbytes );
1040 break;
1041 case GCRY_CIPHER_MODE_CTR:
1042 do_ctr_encrypt(c, outbuf, inbuf, nbytes );
1043 break;
1044 case GCRY_CIPHER_MODE_STREAM:
1045 c->cipher->stencrypt ( &c->context.c,
1046 outbuf, (byte*)/*arggg*/inbuf, nbytes );
1047 break;
1048 case GCRY_CIPHER_MODE_NONE:
1049 if( inbuf != outbuf )
1050 memmove( outbuf, inbuf, nbytes );
1051 break;
1052 default:
1053 log_fatal("cipher_encrypt: invalid mode %d\n", c->mode );
1054 rc = GPG_ERR_INV_CIPHER_MODE;
1055 break;
1056 }
1057 return rc;
1058}
1059
1060
1061/****************
1062 * Encrypt IN and write it to OUT. If IN is NULL, in-place encryption has
1063 * been requested.
1064 */
1065gcry_error_t
1066gcry_cipher_encrypt (gcry_cipher_hd_t h, byte *out, size_t outsize,
1067 const byte *in, size_t inlen)
1068{
1069 gcry_err_code_t err;
1070
1071 if (!in)
1072 /* Caller requested in-place encryption. */
1073 /* Actullay cipher_encrypt() does not need to know about it, but
1074 * we may change this to get better performance. */
1075 err = cipher_encrypt (h, out, out, outsize);
1076 else if (outsize < ((h->flags & GCRY_CIPHER_CBC_MAC) ?
1077 h->cipher->blocksize : inlen))
1078 err = GPG_ERR_TOO_SHORT;
1079 else if ((h->mode == GCRY_CIPHER_MODE_ECB
1080 || (h->mode == GCRY_CIPHER_MODE_CBC
1081 && (! ((h->flags & GCRY_CIPHER_CBC_CTS)
1082 && (inlen > h->cipher->blocksize)))))
1083 && (inlen % h->cipher->blocksize))
1084 err = GPG_ERR_INV_ARG;
1085 else
1086 err = cipher_encrypt (h, out, in, inlen);
1087
1088 if (err && out)
1089 memset (out, 0x42, outsize); /* Failsafe: Make sure that the
1090 plaintext will never make it into
1091 OUT. */
1092
1093 return gcry_error (err);
1094}
1095
1096
1097
1098/****************
1099 * Decrypt INBUF to OUTBUF with the mode selected at open.
1100 * inbuf and outbuf may overlap or be the same.
1101 * Depending on the mode some some contraints apply to NBYTES.
1102 */
1103static gcry_err_code_t
1104cipher_decrypt (gcry_cipher_hd_t c, byte *outbuf, const byte *inbuf,
1105 unsigned int nbytes)
1106{
1107 gcry_err_code_t rc = GPG_ERR_NO_ERROR;
1108
1109 switch( c->mode ) {
1110 case GCRY_CIPHER_MODE_ECB:
1111 if (!(nbytes%c->cipher->blocksize))
1112 do_ecb_decrypt(c, outbuf, inbuf, nbytes/c->cipher->blocksize );
1113 else
1114 rc = GPG_ERR_INV_ARG;
1115 break;
1116 case GCRY_CIPHER_MODE_CBC:
1117 if (!(nbytes%c->cipher->blocksize)
1118 || (nbytes > c->cipher->blocksize
1119 && (c->flags & GCRY_CIPHER_CBC_CTS)))
1120 do_cbc_decrypt(c, outbuf, inbuf, nbytes );
1121 else
1122 rc = GPG_ERR_INV_ARG;
1123 break;
1124 case GCRY_CIPHER_MODE_CFB:
1125 do_cfb_decrypt(c, outbuf, inbuf, nbytes );
1126 break;
1127 case GCRY_CIPHER_MODE_CTR:
1128 do_ctr_decrypt(c, outbuf, inbuf, nbytes );
1129 break;
1130 case GCRY_CIPHER_MODE_STREAM:
1131 c->cipher->stdecrypt ( &c->context.c,
1132 outbuf, (byte*)/*arggg*/inbuf, nbytes );
1133 break;
1134 case GCRY_CIPHER_MODE_NONE:
1135 if( inbuf != outbuf )
1136 memmove( outbuf, inbuf, nbytes );
1137 break;
1138 default:
1139 log_fatal ("cipher_decrypt: invalid mode %d\n", c->mode );
1140 rc = GPG_ERR_INV_CIPHER_MODE;
1141 break;
1142 }
1143 return rc;
1144}
1145
1146
1147gcry_error_t
1148gcry_cipher_decrypt (gcry_cipher_hd_t h, byte *out, size_t outsize,
1149 const byte *in, size_t inlen)
1150{
1151 gcry_err_code_t err = GPG_ERR_NO_ERROR;
1152
1153 if (! in)
1154 /* Caller requested in-place encryption. */
1155 /* Actullay cipher_encrypt() does not need to know about it, but
1156 * we may chnage this to get better performance. */
1157 err = cipher_decrypt (h, out, out, outsize);
1158 else if (outsize < inlen)
1159 err = GPG_ERR_TOO_SHORT;
1160 else if (((h->mode == GCRY_CIPHER_MODE_ECB)
1161 || ((h->mode == GCRY_CIPHER_MODE_CBC)
1162 && (! ((h->flags & GCRY_CIPHER_CBC_CTS)
1163 && (inlen > h->cipher->blocksize)))))
1164 && (inlen % h->cipher->blocksize) != 0)
1165 err = GPG_ERR_INV_ARG;
1166 else
1167 err = cipher_decrypt (h, out, in, inlen);
1168
1169 return gcry_error (err);
1170}
1171
1172
1173
1174/****************
1175 * Used for PGP's somewhat strange CFB mode. Only works if
1176 * the corresponding flag is set.
1177 */
1178static void
1179cipher_sync( gcry_cipher_hd_t c )
1180{
1181 if( (c->flags & GCRY_CIPHER_ENABLE_SYNC) && c->unused ) {
1182 memmove(c->iv + c->unused, c->iv, c->cipher->blocksize - c->unused );
1183 memcpy(c->iv, c->lastiv + c->cipher->blocksize - c->unused, c->unused);
1184 c->unused = 0;
1185 }
1186}
1187
1188
1189gcry_error_t
1190gcry_cipher_ctl( gcry_cipher_hd_t h, int cmd, void *buffer, size_t buflen)
1191{
1192 gcry_err_code_t rc = GPG_ERR_NO_ERROR;
1193
1194 switch (cmd)
1195 {
1196 case GCRYCTL_SET_KEY:
1197 rc = cipher_setkey( h, buffer, buflen );
1198 break;
1199 case GCRYCTL_SET_IV:
1200 cipher_setiv( h, buffer, buflen );
1201 break;
1202 case GCRYCTL_RESET:
1203 cipher_reset (h);
1204 break;
1205 case GCRYCTL_CFB_SYNC:
1206 cipher_sync( h );
1207 break;
1208 case GCRYCTL_SET_CBC_CTS:
1209 if (buflen)
1210 if (h->flags & GCRY_CIPHER_CBC_MAC)
1211 rc = GPG_ERR_INV_FLAG;
1212 else
1213 h->flags |= GCRY_CIPHER_CBC_CTS;
1214 else
1215 h->flags &= ~GCRY_CIPHER_CBC_CTS;
1216 break;
1217 case GCRYCTL_SET_CBC_MAC:
1218 if (buflen)
1219 if (h->flags & GCRY_CIPHER_CBC_CTS)
1220 rc = GPG_ERR_INV_FLAG;
1221 else
1222 h->flags |= GCRY_CIPHER_CBC_MAC;
1223 else
1224 h->flags &= ~GCRY_CIPHER_CBC_MAC;
1225 break;
1226 case GCRYCTL_DISABLE_ALGO:
1227 /* this one expects a NULL handle and buffer pointing to an
1228 * integer with the algo number.
1229 */
1230 if( h || !buffer || buflen != sizeof(int) )
1231 return gcry_error (GPG_ERR_CIPHER_ALGO);
1232 disable_cipher_algo( *(int*)buffer );
1233 break;
1234 case GCRYCTL_SET_CTR:
1235 if (buffer && buflen == h->cipher->blocksize)
1236 memcpy (h->ctr, buffer, h->cipher->blocksize);
1237 else if (buffer == NULL || buflen == 0)
1238 memset (h->ctr, 0, h->cipher->blocksize);
1239 else
1240 rc = GPG_ERR_INV_ARG;
1241 break;
1242
1243 default:
1244 rc = GPG_ERR_INV_OP;
1245 }
1246
1247 return gcry_error (rc);
1248}
1249
1250
1251/****************
1252 * Return information about the cipher handle.
1253 */
1254gcry_error_t
1255gcry_cipher_info( gcry_cipher_hd_t h, int cmd, void *buffer, size_t *nbytes)
1256{
1257 gcry_err_code_t err = GPG_ERR_NO_ERROR;
1258
1259 switch (cmd)
1260 {
1261 default:
1262 err = GPG_ERR_INV_OP;
1263 }
1264
1265 return gcry_error (err);
1266}
1267
1268/****************
1269 * Return information about the given cipher algorithm
1270 * WHAT select the kind of information returned:
1271 * GCRYCTL_GET_KEYLEN:
1272 *Return the length of the key, if the algorithm
1273 *supports multiple key length, the maximum supported value
1274 *is returnd. The length is return as number of octets.
1275 *buffer and nbytes must be zero.
1276 *The keylength is returned in _bytes_.
1277 * GCRYCTL_GET_BLKLEN:
1278 *Return the blocklength of the algorithm counted in octets.
1279 *buffer and nbytes must be zero.
1280 * GCRYCTL_TEST_ALGO:
1281 *Returns 0 when the specified algorithm is available for use.
1282 *buffer and nbytes must be zero.
1283 *
1284 * Note: Because this function is in most cases used to return an
1285 * integer value, we can make it easier for the caller to just look at
1286 * the return value. The caller will in all cases consult the value
1287 * and thereby detecting whether a error occured or not (i.e. while checking
1288 * the block size)
1289 */
1290gcry_error_t
1291gcry_cipher_algo_info (int algo, int what, void *buffer, size_t *nbytes)
1292{
1293 gcry_err_code_t err = GPG_ERR_NO_ERROR;
1294 unsigned int ui;
1295
1296 switch (what)
1297 {
1298 case GCRYCTL_GET_KEYLEN:
1299 if (buffer || (! nbytes))
1300 err = GPG_ERR_CIPHER_ALGO;
1301 else
1302 {
1303 ui = cipher_get_keylen (algo);
1304 if ((ui > 0) && (ui <= 512))
1305 *nbytes = (size_t) ui / 8;
1306 else
1307 /* The only reason is an invalid algo or a strange
1308 blocksize. */
1309 err = GPG_ERR_CIPHER_ALGO;
1310 }
1311 break;
1312
1313 case GCRYCTL_GET_BLKLEN:
1314 if (buffer || (! nbytes))
1315 err = GPG_ERR_CIPHER_ALGO;
1316 else
1317 {
1318 ui = cipher_get_blocksize (algo);
1319 if ((ui > 0) && (ui < 10000))
1320 *nbytes = ui;
1321 else
1322 /* The only reason is an invalid algo or a strange
1323 blocksize. */
1324 err = GPG_ERR_CIPHER_ALGO;
1325 }
1326 break;
1327
1328 case GCRYCTL_TEST_ALGO:
1329 if (buffer || nbytes)
1330 err = GPG_ERR_INV_ARG;
1331 else
1332 err = check_cipher_algo (algo);
1333 break;
1334
1335 default:
1336 err = GPG_ERR_INV_OP;
1337 }
1338
1339 return gcry_error (err);
1340}
1341
1342
1343size_t
1344gcry_cipher_get_algo_keylen (int algo)
1345{
1346 size_t n;
1347
1348 if (gcry_cipher_algo_info( algo, GCRYCTL_GET_KEYLEN, NULL, &n))
1349 n = 0;
1350 return n;
1351}
1352
1353
1354size_t
1355gcry_cipher_get_algo_blklen (int algo)
1356{
1357 size_t n;
1358
1359 if (gcry_cipher_algo_info( algo, GCRYCTL_GET_BLKLEN, NULL, &n))
1360 n = 0;
1361 return n;
1362}
1363
1364
1365gcry_err_code_t
1366_gcry_cipher_init (void)
1367{
1368 gcry_err_code_t err = GPG_ERR_NO_ERROR;
1369
1370 REGISTER_DEFAULT_CIPHERS;
1371
1372 return err;
1373}
1374
1375/* Get a list consisting of the IDs of the loaded cipher modules. If
1376 LIST is zero, write the number of loaded cipher modules to
1377 LIST_LENGTH and return. If LIST is non-zero, the first
1378 *LIST_LENGTH algorithm IDs are stored in LIST, which must be of
1379 according size. In case there are less cipher modules than
1380 *LIST_LENGTH, *LIST_LENGTH is updated to the correct number. */
1381gcry_error_t
1382gcry_cipher_list (int *list, int *list_length)
1383{
1384 gcry_err_code_t err = GPG_ERR_NO_ERROR;
1385
1386 ath_mutex_lock (&ciphers_registered_lock);
1387 err = _gcry_module_list (ciphers_registered, list, list_length);
1388 ath_mutex_unlock (&ciphers_registered_lock);
1389
1390 return err;
1391}
diff --git a/pwmanager/libcrypt/cipher/cipher.pro b/pwmanager/libcrypt/cipher/cipher.pro
new file mode 100644
index 0000000..9860136
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/cipher.pro
@@ -0,0 +1,70 @@
1######################################################################
2# Automatically generated by qmake (1.07a) Tue Oct 19 14:06:16 2004
3######################################################################
4
5TEMPLATE = lib
6INCLUDEPATH += . .. ../crypt
7
8CONFIG += staticlib
9OBJECTS_DIR = obj/$(PLATFORM)
10MOC_DIR = moc/$(PLATFORM)
11DESTDIR= ../$(PLATFORM)
12 TARGET = kpmicrocipher
13
14# Input
15HEADERS += bithelp.h rand-internal.h random.h rmd.h
16SOURCES += ac.c \
17 arcfour.c \
18 blowfish.c \
19 cast5.c \
20 cipher.c \
21 crc.c \
22 des.c \
23 dsa.c \
24 elgamal.c \
25 md.c \
26 md4.c \
27 md5.c \
28 primegen.c \
29 pubkey.c \
30 random.c \
31 rfc2268.c \
32 rijndael.c \
33 rmd160.c \
34 rndegd.c \
35 rndlinux.c \
36 rndunix.c \
37 rsa.c \
38 serpent.c \
39 sha1.c \
40 sha256.c \
41 sha512.c \
42 tiger.c \
43 twofish.c
44
45 # rndw32.c \
46
47
48HEADERS += ../crypt/ath.h \
49 ../crypt/cipher.h \
50 ../crypt/g10lib.h \
51 ../crypt/gcrypt-module.h \
52 ../crypt/gcrypt.h \
53 ../crypt/mpi.h \
54 ../crypt/secmem.h \
55 ../crypt/stdmem.h \
56 ../crypt/types.h
57SOURCES += ../crypt/ath.c \
58 ../crypt/global.c \
59 ../crypt/misc.c \
60 ../crypt/missing-string.c \
61 ../crypt/module.c \
62 ../crypt/secmem.c \
63 ../crypt/sexp.c \
64 ../crypt/stdmem.c
65
66
67
68
69#LIBS += -L../$(PLATFORM)
70#LIBS += -lgcrypt -lmpi -lerror \ No newline at end of file
diff --git a/pwmanager/libcrypt/cipher/crc.c b/pwmanager/libcrypt/cipher/crc.c
new file mode 100644
index 0000000..3d16a10
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/crc.c
@@ -0,0 +1,295 @@
1/* crc.c - Cyclic redundancy checks.
2 * Copyright (C) 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 *
20 */
21
22#include <config.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <assert.h>
27#include "g10lib.h"
28#include "memory.h"
29#include "cipher.h"
30
31#include "bithelp.h"
32
33/* Table of CRCs of all 8-bit messages. Generated by running code
34 from RFC 1952 modified to print out the table. */
35static u32 crc32_table[256] = {
36 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
37 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
38 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
39 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
40 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
41 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
42 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
43 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
44 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
45 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
46 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
47 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
48 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
49 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
50 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
51 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
52 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
53 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
54 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
55 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
56 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
57 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
58 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
59 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
60 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
61 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
62 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
63 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
64 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
65 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
66 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
67 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
68 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
69 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
70 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
71 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
72 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
73 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
74 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
75 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
76 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
77 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
78 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
79};
80
81/*
82 * The following function was extracted from RFC 1952 by Simon
83 * Josefsson, for the Shishi project, and modified to be compatible
84 * with the modified CRC-32 used by RFC 1510, and subsequently
85 * modified for GNU Libgcrypt to allow it to be used for calculating
86 * both unmodified CRC-32 and modified CRC-32 values. Original
87 * copyright and notice from the document follows:
88 *
89 * Copyright (c) 1996 L. Peter Deutsch
90 *
91 * Permission is granted to copy and distribute this document for
92 * any purpose and without charge, including translations into
93 * other languages and incorporation into compilations, provided
94 * that the copyright notice and this notice are preserved, and
95 * that any substantive changes or deletions from the original are
96 * clearly marked.
97 *
98 * The copyright on RFCs, and consequently the function below, are
99 * supposedly also retroactively claimed by the Internet Society
100 * (according to rfc-editor@rfc-editor.org), with the following
101 * copyright notice:
102 *
103 * Copyright (C) The Internet Society. All Rights Reserved.
104 *
105 * This document and translations of it may be copied and furnished
106 * to others, and derivative works that comment on or otherwise
107 * explain it or assist in its implementation may be prepared,
108 * copied, published and distributed, in whole or in part, without
109 * restriction of any kind, provided that the above copyright
110 * notice and this paragraph are included on all such copies and
111 * derivative works. However, this document itself may not be
112 * modified in any way, such as by removing the copyright notice or
113 * references to the Internet Society or other Internet
114 * organizations, except as needed for the purpose of developing
115 * Internet standards in which case the procedures for copyrights
116 * defined in the Internet Standards process must be followed, or
117 * as required to translate it into languages other than English.
118 *
119 * The limited permissions granted above are perpetual and will not be
120 * revoked by the Internet Society or its successors or assigns.
121 *
122 * This document and the information contained herein is provided
123 * on an "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET
124 * ENGINEERING TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR
125 * IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE
126 * OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY
127 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
128 * PARTICULAR PURPOSE.
129 *
130 */
131static u32
132update_crc32 (u32 crc, char *buf, size_t len)
133{
134 size_t n;
135
136 for (n = 0; n < len; n++)
137 crc = crc32_table[(crc ^ buf[n]) & 0xff] ^ (crc >> 8);
138
139 return crc;
140}
141
142typedef struct
143{
144 u32 CRC;
145 byte buf[4];
146}
147CRC_CONTEXT;
148
149/* CRC32 */
150
151static void
152crc32_init (void *context)
153{
154 CRC_CONTEXT *ctx = (CRC_CONTEXT *) context;
155 ctx->CRC = 0 ^ 0xffffffffL;
156}
157
158static void
159crc32_write (void *context, byte * inbuf, size_t inlen)
160{
161 CRC_CONTEXT *ctx = (CRC_CONTEXT *) context;
162 if (!inbuf)
163 return;
164 ctx->CRC = update_crc32 (ctx->CRC, inbuf, inlen);
165}
166
167static byte *
168crc32_read (void *context)
169{
170 CRC_CONTEXT *ctx = (CRC_CONTEXT *) context;
171 return ctx->buf;
172}
173
174static void
175crc32_final (void *context)
176{
177 CRC_CONTEXT *ctx = (CRC_CONTEXT *) context;
178 ctx->CRC ^= 0xffffffffL;
179 ctx->buf[0] = (ctx->CRC >> 24) & 0xFF;
180 ctx->buf[1] = (ctx->CRC >> 16) & 0xFF;
181 ctx->buf[2] = (ctx->CRC >> 8) & 0xFF;
182 ctx->buf[3] = (ctx->CRC ) & 0xFF;
183}
184
185/* CRC32 a'la RFC 1510 */
186static void
187crc32rfc1510_init (void *context)
188{
189 CRC_CONTEXT *ctx = (CRC_CONTEXT *) context;
190 ctx->CRC = 0;
191}
192
193static void
194crc32rfc1510_final (void *context)
195{
196 CRC_CONTEXT *ctx = (CRC_CONTEXT *) context;
197 ctx->buf[0] = (ctx->CRC >> 24) & 0xFF;
198 ctx->buf[1] = (ctx->CRC >> 16) & 0xFF;
199 ctx->buf[2] = (ctx->CRC >> 8) & 0xFF;
200 ctx->buf[3] = (ctx->CRC ) & 0xFF;
201}
202
203/* CRC24 a'la RFC 2440 */
204/*
205 * The following CRC 24 routines are adapted from RFC 2440, which has
206 * the following copyright notice:
207 *
208 * Copyright (C) The Internet Society (1998). All Rights Reserved.
209 *
210 * This document and translations of it may be copied and furnished
211 * to others, and derivative works that comment on or otherwise
212 * explain it or assist in its implementation may be prepared,
213 * copied, published and distributed, in whole or in part, without
214 * restriction of any kind, provided that the above copyright notice
215 * and this paragraph are included on all such copies and derivative
216 * works. However, this document itself may not be modified in any
217 * way, such as by removing the copyright notice or references to
218 * the Internet Society or other Internet organizations, except as
219 * needed for the purpose of developing Internet standards in which
220 * case the procedures for copyrights defined in the Internet
221 * Standards process must be followed, or as required to translate
222 * it into languages other than English.
223 *
224 * The limited permissions granted above are perpetual and will not be
225 * revoked by the Internet Society or its successors or assigns.
226 *
227 * This document and the information contained herein is provided on
228 * an "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET
229 * ENGINEERING TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR
230 * IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE
231 * OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY
232 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
233 * PURPOSE.
234 */
235
236#define CRC24_INIT 0xb704ceL
237#define CRC24_POLY 0x1864cfbL
238
239static void
240crc24rfc2440_init (void *context)
241{
242 CRC_CONTEXT *ctx = (CRC_CONTEXT *) context;
243 ctx->CRC = CRC24_INIT;
244}
245
246static void
247crc24rfc2440_write (void *context, byte * inbuf, size_t inlen)
248{
249 int i;
250 CRC_CONTEXT *ctx = (CRC_CONTEXT *) context;
251
252 if (!inbuf)
253 return;
254
255 while (inlen--) {
256 ctx->CRC ^= (*inbuf++) << 16;
257 for (i = 0; i < 8; i++) {
258 ctx->CRC <<= 1;
259 if (ctx->CRC & 0x1000000)
260 ctx->CRC ^= CRC24_POLY;
261 }
262 }
263}
264
265static void
266crc24rfc2440_final (void *context)
267{
268 CRC_CONTEXT *ctx = (CRC_CONTEXT *) context;
269 ctx->buf[0] = (ctx->CRC >> 16) & 0xFF;
270 ctx->buf[1] = (ctx->CRC >> 8) & 0xFF;
271 ctx->buf[2] = (ctx->CRC ) & 0xFF;
272}
273
274gcry_md_spec_t _gcry_digest_spec_crc32 =
275 {
276 "CRC32", NULL, 0, NULL, 4,
277 crc32_init, crc32_write, crc32_final, crc32_read,
278 sizeof (CRC_CONTEXT)
279 };
280
281gcry_md_spec_t _gcry_digest_spec_crc32_rfc1510 =
282 {
283 "CRC32RFC1510", NULL, 0, NULL, 4,
284 crc32rfc1510_init, crc32_write,
285 crc32rfc1510_final, crc32_read,
286 sizeof (CRC_CONTEXT)
287 };
288
289gcry_md_spec_t _gcry_digest_spec_crc24_rfc2440 =
290 {
291 "CRC24RFC2440", NULL, 0, NULL, 3,
292 crc24rfc2440_init, crc24rfc2440_write,
293 crc24rfc2440_final, crc32_read,
294 sizeof (CRC_CONTEXT)
295 };
diff --git a/pwmanager/libcrypt/cipher/des.c b/pwmanager/libcrypt/cipher/des.c
new file mode 100644
index 0000000..81b5337
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/des.c
@@ -0,0 +1,1111 @@
1/* des.c - DES and Triple-DES encryption/decryption Algorithm
2 * Copyright (C) 1998, 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 *
20 * For a description of triple encryption, see:
21 * Bruce Schneier: Applied Cryptography. Second Edition.
22 * John Wiley & Sons, 1996. ISBN 0-471-12845-7. Pages 358 ff.
23 * This implementation is according to the definition of DES in FIPS
24 * PUB 46-2 from December 1993.
25 */
26
27
28/*
29 * Written by Michael Roth <mroth@nessie.de>, September 1998
30 */
31
32
33/*
34 * U S A G E
35 * ===========
36 *
37 * For DES or Triple-DES encryption/decryption you must initialize a proper
38 * encryption context with a key.
39 *
40 * A DES key is 64bit wide but only 56bits of the key are used. The remaining
41 * bits are parity bits and they will _not_ checked in this implementation, but
42 * simply ignored.
43 *
44 * For Triple-DES you could use either two 64bit keys or three 64bit keys.
45 * The parity bits will _not_ checked, too.
46 *
47 * After initializing a context with a key you could use this context to
48 * encrypt or decrypt data in 64bit blocks in Electronic Codebook Mode.
49 *
50 * (In the examples below the slashes at the beginning and ending of comments
51 * are omited.)
52 *
53 * DES Example
54 * -----------
55 * unsigned char key[8];
56 * unsigned char plaintext[8];
57 * unsigned char ciphertext[8];
58 * unsigned char recoverd[8];
59 * des_ctx context;
60 *
61 * * Fill 'key' and 'plaintext' with some data *
62 * ....
63 *
64 * * Set up the DES encryption context *
65 * des_setkey(context, key);
66 *
67 * * Encrypt the plaintext *
68 * des_ecb_encrypt(context, plaintext, ciphertext);
69 *
70 * * To recover the orginal plaintext from ciphertext use: *
71 * des_ecb_decrypt(context, ciphertext, recoverd);
72 *
73 *
74 * Triple-DES Example
75 * ------------------
76 * unsigned char key1[8];
77 * unsigned char key2[8];
78 * unsigned char key3[8];
79 * unsigned char plaintext[8];
80 * unsigned char ciphertext[8];
81 * unsigned char recoverd[8];
82 * tripledes_ctx context;
83 *
84 * * If you would like to use two 64bit keys, fill 'key1' and'key2'
85 * then setup the encryption context: *
86 * tripledes_set2keys(context, key1, key2);
87 *
88 * * To use three 64bit keys with Triple-DES use: *
89 * tripledes_set3keys(context, key1, key2, key3);
90 *
91 * * Encrypting plaintext with Triple-DES *
92 * tripledes_ecb_encrypt(context, plaintext, ciphertext);
93 *
94 * * Decrypting ciphertext to recover the plaintext with Triple-DES *
95 * tripledes_ecb_decrypt(context, ciphertext, recoverd);
96 *
97 *
98 * Selftest
99 * --------
100 * char *error_msg;
101 *
102 * * To perform a selftest of this DES/Triple-DES implementation use the
103 * function selftest(). It will return an error string if their are
104 * some problems with this library. *
105 *
106 * if ( (error_msg = selftest()) )
107 * {
108 * fprintf(stderr, "An error in the DES/Tripple-DES implementation occured: %s\n", error_msg);
109 * abort();
110 * }
111 */
112
113
114#include <config.h>
115#include <stdio.h>
116 #include <string.h> /* memcpy, memcmp */
117#include "types.h" /* for byte and u32 typedefs */
118#include "g10lib.h"
119#include "cipher.h"
120
121#if defined(__GNUC__) && defined(__GNU_LIBRARY__)
122#define working_memcmp memcmp
123#else
124/*
125 * According to the SunOS man page, memcmp returns indeterminate sign
126 * depending on whether characters are signed or not.
127 */
128static int
129working_memcmp( const char *a, const char *b, size_t n )
130{
131 for( ; n; n--, a++, b++ )
132 if( *a != *b )
133 return (int)(*(byte*)a) - (int)(*(byte*)b);
134 return 0;
135}
136#endif
137
138/*
139 * Encryption/Decryption context of DES
140 */
141typedef struct _des_ctx
142 {
143 u32 encrypt_subkeys[32];
144 u32 decrypt_subkeys[32];
145 }
146des_ctx[1];
147
148/*
149 * Encryption/Decryption context of Triple-DES
150 */
151typedef struct _tripledes_ctx
152 {
153 u32 encrypt_subkeys[96];
154 u32 decrypt_subkeys[96];
155 }
156tripledes_ctx[1];
157
158static void des_key_schedule (const byte *, u32 *);
159static int des_setkey (struct _des_ctx *, const byte *);
160static int des_ecb_crypt (struct _des_ctx *, const byte *, byte *, int);
161static int tripledes_set2keys (struct _tripledes_ctx *,
162 const byte *, const byte *);
163static int tripledes_set3keys (struct _tripledes_ctx *,
164 const byte *, const byte *, const byte *);
165static int tripledes_ecb_crypt (struct _tripledes_ctx *,
166 const byte *, byte *, int);
167static int is_weak_key ( const byte *key );
168static const char *selftest (void);
169
170static int initialized;
171
172
173
174
175/*
176 * The s-box values are permuted according to the 'primitive function P'
177 * and are rotated one bit to the left.
178 */
179static u32 sbox1[64] =
180{
181 0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404, 0x00000004, 0x00010000,
182 0x00000400, 0x01010400, 0x01010404, 0x00000400, 0x01000404, 0x01010004, 0x01000000, 0x00000004,
183 0x00000404, 0x01000400, 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000, 0x01000404,
184 0x00010004, 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404, 0x00010404, 0x01000000,
185 0x00010000, 0x01010404, 0x00000004, 0x01010000, 0x01010400, 0x01000000, 0x01000000, 0x00000400,
186 0x01010004, 0x00010000, 0x00010400, 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404,
187 0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004, 0x00000404, 0x00010404, 0x01010400,
188 0x00000404, 0x01000400, 0x01000400, 0x00000000, 0x00010004, 0x00010400, 0x00000000, 0x01010004
189};
190
191static u32 sbox2[64] =
192{
193 0x80108020, 0x80008000, 0x00008000, 0x00108020, 0x00100000, 0x00000020, 0x80100020, 0x80008020,
194 0x80000020, 0x80108020, 0x80108000, 0x80000000, 0x80008000, 0x00100000, 0x00000020, 0x80100020,
195 0x00108000, 0x00100020, 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020, 0x80100000,
196 0x00100020, 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000, 0x80100000, 0x00008020,
197 0x00000000, 0x00108020, 0x80100020, 0x00100000, 0x80008020, 0x80100000, 0x80108000, 0x00008000,
198 0x80100000, 0x80008000, 0x00000020, 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000,
199 0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020, 0x80008020, 0x80000020, 0x00100020,
200 0x00108000, 0x00000000, 0x80008000, 0x00008020, 0x80000000, 0x80100020, 0x80108020, 0x00108000
201};
202
203static u32 sbox3[64] =
204{
205 0x00000208, 0x08020200, 0x00000000, 0x08020008, 0x08000200, 0x00000000, 0x00020208, 0x08000200,
206 0x00020008, 0x08000008, 0x08000008, 0x00020000, 0x08020208, 0x00020008, 0x08020000, 0x00000208,
207 0x08000000, 0x00000008, 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008, 0x00020208,
208 0x08000208, 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208, 0x00000200, 0x08000000,
209 0x08020200, 0x08000000, 0x00020008, 0x00000208, 0x00020000, 0x08020200, 0x08000200, 0x00000000,
210 0x00000200, 0x00020008, 0x08020208, 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008,
211 0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008, 0x00020208, 0x00020200, 0x08000008,
212 0x08020000, 0x08000208, 0x00000208, 0x08020000, 0x00020208, 0x00000008, 0x08020008, 0x00020200
213};
214
215static u32 sbox4[64] =
216{
217 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802080, 0x00800081, 0x00800001, 0x00002001,
218 0x00000000, 0x00802000, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00800080, 0x00800001,
219 0x00000001, 0x00002000, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001, 0x00002080,
220 0x00800081, 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080, 0x00802081, 0x00000081,
221 0x00800080, 0x00800001, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00000000, 0x00802000,
222 0x00002080, 0x00800080, 0x00800081, 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080,
223 0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001, 0x00002001, 0x00802080, 0x00800081,
224 0x00002001, 0x00002080, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002000, 0x00802080
225};
226
227static u32 sbox5[64] =
228{
229 0x00000100, 0x02080100, 0x02080000, 0x42000100, 0x00080000, 0x00000100, 0x40000000, 0x02080000,
230 0x40080100, 0x00080000, 0x02000100, 0x40080100, 0x42000100, 0x42080000, 0x00080100, 0x40000000,
231 0x02000000, 0x40080000, 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100, 0x02000100,
232 0x42080000, 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000, 0x42000000, 0x00080100,
233 0x00080000, 0x42000100, 0x00000100, 0x02000000, 0x40000000, 0x02080000, 0x42000100, 0x40080100,
234 0x02000100, 0x40000000, 0x42080000, 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000,
235 0x42080100, 0x00080100, 0x42000000, 0x42080100, 0x02080000, 0x00000000, 0x40080000, 0x42000000,
236 0x00080100, 0x02000100, 0x40000100, 0x00080000, 0x00000000, 0x40080000, 0x02080100, 0x40000100
237};
238
239static u32 sbox6[64] =
240{
241 0x20000010, 0x20400000, 0x00004000, 0x20404010, 0x20400000, 0x00000010, 0x20404010, 0x00400000,
242 0x20004000, 0x00404010, 0x00400000, 0x20000010, 0x00400010, 0x20004000, 0x20000000, 0x00004010,
243 0x00000000, 0x00400010, 0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010, 0x20400010,
244 0x20400010, 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000, 0x20404000, 0x20000000,
245 0x20004000, 0x00000010, 0x20400010, 0x00404000, 0x20404010, 0x00400000, 0x00004010, 0x20000010,
246 0x00400000, 0x20004000, 0x20000000, 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000,
247 0x00404010, 0x20404000, 0x00000000, 0x20400010, 0x00000010, 0x00004000, 0x20400000, 0x00404010,
248 0x00004000, 0x00400010, 0x20004010, 0x00000000, 0x20404000, 0x20000000, 0x00400010, 0x20004010
249};
250
251static u32 sbox7[64] =
252{
253 0x00200000, 0x04200002, 0x04000802, 0x00000000, 0x00000800, 0x04000802, 0x00200802, 0x04200800,
254 0x04200802, 0x00200000, 0x00000000, 0x04000002, 0x00000002, 0x04000000, 0x04200002, 0x00000802,
255 0x04000800, 0x00200802, 0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800, 0x00200002,
256 0x04200000, 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002, 0x04000000, 0x00200800,
257 0x04000000, 0x00200800, 0x00200000, 0x04000802, 0x04000802, 0x04200002, 0x04200002, 0x00000002,
258 0x00200002, 0x04000000, 0x04000800, 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800,
259 0x00000802, 0x04000002, 0x04200802, 0x04200000, 0x00200800, 0x00000000, 0x00000002, 0x04200802,
260 0x00000000, 0x00200802, 0x04200000, 0x00000800, 0x04000002, 0x04000800, 0x00000800, 0x00200002
261};
262
263static u32 sbox8[64] =
264{
265 0x10001040, 0x00001000, 0x00040000, 0x10041040, 0x10000000, 0x10001040, 0x00000040, 0x10000000,
266 0x00040040, 0x10040000, 0x10041040, 0x00041000, 0x10041000, 0x00041040, 0x00001000, 0x00000040,
267 0x10040000, 0x10000040, 0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040, 0x10041000,
268 0x00001040, 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000, 0x00041040, 0x00040000,
269 0x00041040, 0x00040000, 0x10041000, 0x00001000, 0x00000040, 0x10040040, 0x00001000, 0x00041040,
270 0x10001000, 0x00000040, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040,
271 0x00000000, 0x10041040, 0x00040040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0x00000000,
272 0x10041040, 0x00041000, 0x00041000, 0x00001040, 0x00001040, 0x00040040, 0x10000000, 0x10041000
273};
274
275
276/*
277 * These two tables are part of the 'permuted choice 1' function.
278 * In this implementation several speed improvements are done.
279 */
280static u32 leftkey_swap[16] =
281{
282 0x00000000, 0x00000001, 0x00000100, 0x00000101,
283 0x00010000, 0x00010001, 0x00010100, 0x00010101,
284 0x01000000, 0x01000001, 0x01000100, 0x01000101,
285 0x01010000, 0x01010001, 0x01010100, 0x01010101
286};
287
288static u32 rightkey_swap[16] =
289{
290 0x00000000, 0x01000000, 0x00010000, 0x01010000,
291 0x00000100, 0x01000100, 0x00010100, 0x01010100,
292 0x00000001, 0x01000001, 0x00010001, 0x01010001,
293 0x00000101, 0x01000101, 0x00010101, 0x01010101,
294};
295
296
297
298/*
299 * Numbers of left shifts per round for encryption subkeys.
300 * To calculate the decryption subkeys we just reverse the
301 * ordering of the calculated encryption subkeys. So their
302 * is no need for a decryption rotate tab.
303 */
304static byte encrypt_rotate_tab[16] =
305{
306 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
307};
308
309
310
311/*
312 * Table with weak DES keys sorted in ascending order.
313 * In DES their are 64 known keys wich are weak. They are weak
314 * because they produce only one, two or four different
315 * subkeys in the subkey scheduling process.
316 * The keys in this table have all their parity bits cleared.
317 */
318static byte weak_keys[64][8] =
319{
320 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, /*w*/
321 { 0x00, 0x00, 0x1e, 0x1e, 0x00, 0x00, 0x0e, 0x0e },
322 { 0x00, 0x00, 0xe0, 0xe0, 0x00, 0x00, 0xf0, 0xf0 },
323 { 0x00, 0x00, 0xfe, 0xfe, 0x00, 0x00, 0xfe, 0xfe },
324 { 0x00, 0x1e, 0x00, 0x1e, 0x00, 0x0e, 0x00, 0x0e }, /*sw*/
325 { 0x00, 0x1e, 0x1e, 0x00, 0x00, 0x0e, 0x0e, 0x00 },
326 { 0x00, 0x1e, 0xe0, 0xfe, 0x00, 0x0e, 0xf0, 0xfe },
327 { 0x00, 0x1e, 0xfe, 0xe0, 0x00, 0x0e, 0xfe, 0xf0 },
328 { 0x00, 0xe0, 0x00, 0xe0, 0x00, 0xf0, 0x00, 0xf0 }, /*sw*/
329 { 0x00, 0xe0, 0x1e, 0xfe, 0x00, 0xf0, 0x0e, 0xfe },
330 { 0x00, 0xe0, 0xe0, 0x00, 0x00, 0xf0, 0xf0, 0x00 },
331 { 0x00, 0xe0, 0xfe, 0x1e, 0x00, 0xf0, 0xfe, 0x0e },
332 { 0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe }, /*sw*/
333 { 0x00, 0xfe, 0x1e, 0xe0, 0x00, 0xfe, 0x0e, 0xf0 },
334 { 0x00, 0xfe, 0xe0, 0x1e, 0x00, 0xfe, 0xf0, 0x0e },
335 { 0x00, 0xfe, 0xfe, 0x00, 0x00, 0xfe, 0xfe, 0x00 },
336 { 0x1e, 0x00, 0x00, 0x1e, 0x0e, 0x00, 0x00, 0x0e },
337 { 0x1e, 0x00, 0x1e, 0x00, 0x0e, 0x00, 0x0e, 0x00 }, /*sw*/
338 { 0x1e, 0x00, 0xe0, 0xfe, 0x0e, 0x00, 0xf0, 0xfe },
339 { 0x1e, 0x00, 0xfe, 0xe0, 0x0e, 0x00, 0xfe, 0xf0 },
340 { 0x1e, 0x1e, 0x00, 0x00, 0x0e, 0x0e, 0x00, 0x00 },
341 { 0x1e, 0x1e, 0x1e, 0x1e, 0x0e, 0x0e, 0x0e, 0x0e }, /*w*/
342 { 0x1e, 0x1e, 0xe0, 0xe0, 0x0e, 0x0e, 0xf0, 0xf0 },
343 { 0x1e, 0x1e, 0xfe, 0xfe, 0x0e, 0x0e, 0xfe, 0xfe },
344 { 0x1e, 0xe0, 0x00, 0xfe, 0x0e, 0xf0, 0x00, 0xfe },
345 { 0x1e, 0xe0, 0x1e, 0xe0, 0x0e, 0xf0, 0x0e, 0xf0 }, /*sw*/
346 { 0x1e, 0xe0, 0xe0, 0x1e, 0x0e, 0xf0, 0xf0, 0x0e },
347 { 0x1e, 0xe0, 0xfe, 0x00, 0x0e, 0xf0, 0xfe, 0x00 },
348 { 0x1e, 0xfe, 0x00, 0xe0, 0x0e, 0xfe, 0x00, 0xf0 },
349 { 0x1e, 0xfe, 0x1e, 0xfe, 0x0e, 0xfe, 0x0e, 0xfe }, /*sw*/
350 { 0x1e, 0xfe, 0xe0, 0x00, 0x0e, 0xfe, 0xf0, 0x00 },
351 { 0x1e, 0xfe, 0xfe, 0x1e, 0x0e, 0xfe, 0xfe, 0x0e },
352 { 0xe0, 0x00, 0x00, 0xe0, 0xf0, 0x00, 0x00, 0xf0 },
353 { 0xe0, 0x00, 0x1e, 0xfe, 0xf0, 0x00, 0x0e, 0xfe },
354 { 0xe0, 0x00, 0xe0, 0x00, 0xf0, 0x00, 0xf0, 0x00 }, /*sw*/
355 { 0xe0, 0x00, 0xfe, 0x1e, 0xf0, 0x00, 0xfe, 0x0e },
356 { 0xe0, 0x1e, 0x00, 0xfe, 0xf0, 0x0e, 0x00, 0xfe },
357 { 0xe0, 0x1e, 0x1e, 0xe0, 0xf0, 0x0e, 0x0e, 0xf0 },
358 { 0xe0, 0x1e, 0xe0, 0x1e, 0xf0, 0x0e, 0xf0, 0x0e }, /*sw*/
359 { 0xe0, 0x1e, 0xfe, 0x00, 0xf0, 0x0e, 0xfe, 0x00 },
360 { 0xe0, 0xe0, 0x00, 0x00, 0xf0, 0xf0, 0x00, 0x00 },
361 { 0xe0, 0xe0, 0x1e, 0x1e, 0xf0, 0xf0, 0x0e, 0x0e },
362 { 0xe0, 0xe0, 0xe0, 0xe0, 0xf0, 0xf0, 0xf0, 0xf0 }, /*w*/
363 { 0xe0, 0xe0, 0xfe, 0xfe, 0xf0, 0xf0, 0xfe, 0xfe },
364 { 0xe0, 0xfe, 0x00, 0x1e, 0xf0, 0xfe, 0x00, 0x0e },
365 { 0xe0, 0xfe, 0x1e, 0x00, 0xf0, 0xfe, 0x0e, 0x00 },
366 { 0xe0, 0xfe, 0xe0, 0xfe, 0xf0, 0xfe, 0xf0, 0xfe }, /*sw*/
367 { 0xe0, 0xfe, 0xfe, 0xe0, 0xf0, 0xfe, 0xfe, 0xf0 },
368 { 0xfe, 0x00, 0x00, 0xfe, 0xfe, 0x00, 0x00, 0xfe },
369 { 0xfe, 0x00, 0x1e, 0xe0, 0xfe, 0x00, 0x0e, 0xf0 },
370 { 0xfe, 0x00, 0xe0, 0x1e, 0xfe, 0x00, 0xf0, 0x0e },
371 { 0xfe, 0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe, 0x00 }, /*sw*/
372 { 0xfe, 0x1e, 0x00, 0xe0, 0xfe, 0x0e, 0x00, 0xf0 },
373 { 0xfe, 0x1e, 0x1e, 0xfe, 0xfe, 0x0e, 0x0e, 0xfe },
374 { 0xfe, 0x1e, 0xe0, 0x00, 0xfe, 0x0e, 0xf0, 0x00 },
375 { 0xfe, 0x1e, 0xfe, 0x1e, 0xfe, 0x0e, 0xfe, 0x0e }, /*sw*/
376 { 0xfe, 0xe0, 0x00, 0x1e, 0xfe, 0xf0, 0x00, 0x0e },
377 { 0xfe, 0xe0, 0x1e, 0x00, 0xfe, 0xf0, 0x0e, 0x00 },
378 { 0xfe, 0xe0, 0xe0, 0xfe, 0xfe, 0xf0, 0xf0, 0xfe },
379 { 0xfe, 0xe0, 0xfe, 0xe0, 0xfe, 0xf0, 0xfe, 0xf0 }, /*sw*/
380 { 0xfe, 0xfe, 0x00, 0x00, 0xfe, 0xfe, 0x00, 0x00 },
381 { 0xfe, 0xfe, 0x1e, 0x1e, 0xfe, 0xfe, 0x0e, 0x0e },
382 { 0xfe, 0xfe, 0xe0, 0xe0, 0xfe, 0xfe, 0xf0, 0xf0 },
383 { 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe } /*w*/
384};
385static unsigned char weak_keys_chksum[20] = {
386 0xD0, 0xCF, 0x07, 0x38, 0x93, 0x70, 0x8A, 0x83, 0x7D, 0xD7,
387 0x8A, 0x36, 0x65, 0x29, 0x6C, 0x1F, 0x7C, 0x3F, 0xD3, 0x41
388};
389
390
391
392/*
393 * Macro to swap bits across two words.
394 */
395 #define DO_PERMUTATION(a, temp, b, offset, mask)\
396 temp = ((a>>offset) ^ b) & mask; \
397 b ^= temp; \
398 a ^= temp<<offset;
399
400
401/*
402 * This performs the 'initial permutation' of the data to be encrypted
403 * or decrypted. Additionally the resulting two words are rotated one bit
404 * to the left.
405 */
406 #define INITIAL_PERMUTATION(left, temp, right) \
407 DO_PERMUTATION(left, temp, right, 4, 0x0f0f0f0f)\
408 DO_PERMUTATION(left, temp, right, 16, 0x0000ffff)\
409 DO_PERMUTATION(right, temp, left, 2, 0x33333333)\
410 DO_PERMUTATION(right, temp, left, 8, 0x00ff00ff)\
411 right = (right << 1) | (right >> 31); \
412 temp = (left ^ right) & 0xaaaaaaaa; \
413 right ^= temp; \
414 left ^= temp; \
415 left = (left << 1) | (left >> 31);
416
417/*
418 * The 'inverse initial permutation'.
419 */
420 #define FINAL_PERMUTATION(left, temp, right) \
421 left = (left << 31) | (left >> 1); \
422 temp = (left ^ right) & 0xaaaaaaaa; \
423 left ^= temp; \
424 right ^= temp; \
425 right = (right << 31) | (right >> 1); \
426 DO_PERMUTATION(right, temp, left, 8, 0x00ff00ff)\
427 DO_PERMUTATION(right, temp, left, 2, 0x33333333)\
428 DO_PERMUTATION(left, temp, right, 16, 0x0000ffff)\
429 DO_PERMUTATION(left, temp, right, 4, 0x0f0f0f0f)
430
431
432/*
433 * A full DES round including 'expansion function', 'sbox substitution'
434 * and 'primitive function P' but without swapping the left and right word.
435 * Please note: The data in 'from' and 'to' is already rotated one bit to
436 * the left, done in the initial permutation.
437 */
438 #define DES_ROUND(from, to, work, subkey) \
439 work = from ^ *subkey++; \
440 to ^= sbox8[ work & 0x3f ]; \
441 to ^= sbox6[ (work>>8) & 0x3f ]; \
442 to ^= sbox4[ (work>>16) & 0x3f ]; \
443 to ^= sbox2[ (work>>24) & 0x3f ]; \
444 work = ((from << 28) | (from >> 4)) ^ *subkey++;\
445 to ^= sbox7[ work & 0x3f ]; \
446 to ^= sbox5[ (work>>8) & 0x3f ]; \
447 to ^= sbox3[ (work>>16) & 0x3f ]; \
448 to ^= sbox1[ (work>>24) & 0x3f ];
449
450/*
451 * Macros to convert 8 bytes from/to 32bit words.
452 */
453 #define READ_64BIT_DATA(data, left, right) \
454 left = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; \
455 right = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7];
456
457 #define WRITE_64BIT_DATA(data, left, right) \
458 data[0] = (left >> 24) &0xff; data[1] = (left >> 16) &0xff; \
459 data[2] = (left >> 8) &0xff; data[3] = left &0xff; \
460 data[4] = (right >> 24) &0xff; data[5] = (right >> 16) &0xff; \
461 data[6] = (right >> 8) &0xff; data[7] = right &0xff;
462
463/*
464 * Handy macros for encryption and decryption of data
465 */
466 #define des_ecb_encrypt(ctx, from, to) des_ecb_crypt(ctx, from, to, 0)
467 #define des_ecb_decrypt(ctx, from, to) des_ecb_crypt(ctx, from, to, 1)
468#define tripledes_ecb_encrypt(ctx, from, to) tripledes_ecb_crypt(ctx,from,to,0)
469#define tripledes_ecb_decrypt(ctx, from, to) tripledes_ecb_crypt(ctx,from,to,1)
470
471
472
473
474
475
476/*
477 * des_key_schedule(): Calculate 16 subkeys pairs (even/odd) for
478 * 16 encryption rounds.
479 * To calculate subkeys for decryption the caller
480 * have to reorder the generated subkeys.
481 *
482 * rawkey: 8 Bytes of key data
483 * subkey: Array of at least 32 u32s. Will be filled
484 * with calculated subkeys.
485 *
486 */
487static void
488des_key_schedule (const byte * rawkey, u32 * subkey)
489{
490 u32 left, right, work;
491 int round;
492
493 READ_64BIT_DATA (rawkey, left, right)
494
495 DO_PERMUTATION (right, work, left, 4, 0x0f0f0f0f)
496 DO_PERMUTATION (right, work, left, 0, 0x10101010)
497
498 left = ((leftkey_swap[(left >> 0) & 0xf] << 3)
499 | (leftkey_swap[(left >> 8) & 0xf] << 2)
500 | (leftkey_swap[(left >> 16) & 0xf] << 1)
501 | (leftkey_swap[(left >> 24) & 0xf])
502 | (leftkey_swap[(left >> 5) & 0xf] << 7)
503 | (leftkey_swap[(left >> 13) & 0xf] << 6)
504 | (leftkey_swap[(left >> 21) & 0xf] << 5)
505 | (leftkey_swap[(left >> 29) & 0xf] << 4));
506
507 left &= 0x0fffffff;
508
509 right = ((rightkey_swap[(right >> 1) & 0xf] << 3)
510 | (rightkey_swap[(right >> 9) & 0xf] << 2)
511 | (rightkey_swap[(right >> 17) & 0xf] << 1)
512 | (rightkey_swap[(right >> 25) & 0xf])
513 | (rightkey_swap[(right >> 4) & 0xf] << 7)
514 | (rightkey_swap[(right >> 12) & 0xf] << 6)
515 | (rightkey_swap[(right >> 20) & 0xf] << 5)
516 | (rightkey_swap[(right >> 28) & 0xf] << 4));
517
518 right &= 0x0fffffff;
519
520 for (round = 0; round < 16; ++round)
521 {
522 left = ((left << encrypt_rotate_tab[round])
523 | (left >> (28 - encrypt_rotate_tab[round]))) & 0x0fffffff;
524 right = ((right << encrypt_rotate_tab[round])
525 | (right >> (28 - encrypt_rotate_tab[round]))) & 0x0fffffff;
526
527 *subkey++ = (((left << 4) & 0x24000000)
528 | ((left << 28) & 0x10000000)
529 | ((left << 14) & 0x08000000)
530 | ((left << 18) & 0x02080000)
531 | ((left << 6) & 0x01000000)
532 | ((left << 9) & 0x00200000)
533 | ((left >> 1) & 0x00100000)
534 | ((left << 10) & 0x00040000)
535 | ((left << 2) & 0x00020000)
536 | ((left >> 10) & 0x00010000)
537 | ((right >> 13) & 0x00002000)
538 | ((right >> 4) & 0x00001000)
539 | ((right << 6) & 0x00000800)
540 | ((right >> 1) & 0x00000400)
541 | ((right >> 14) & 0x00000200)
542 | (right & 0x00000100)
543 | ((right >> 5) & 0x00000020)
544 | ((right >> 10) & 0x00000010)
545 | ((right >> 3) & 0x00000008)
546 | ((right >> 18) & 0x00000004)
547 | ((right >> 26) & 0x00000002)
548 | ((right >> 24) & 0x00000001));
549
550 *subkey++ = (((left << 15) & 0x20000000)
551 | ((left << 17) & 0x10000000)
552 | ((left << 10) & 0x08000000)
553 | ((left << 22) & 0x04000000)
554 | ((left >> 2) & 0x02000000)
555 | ((left << 1) & 0x01000000)
556 | ((left << 16) & 0x00200000)
557 | ((left << 11) & 0x00100000)
558 | ((left << 3) & 0x00080000)
559 | ((left >> 6) & 0x00040000)
560 | ((left << 15) & 0x00020000)
561 | ((left >> 4) & 0x00010000)
562 | ((right >> 2) & 0x00002000)
563 | ((right << 8) & 0x00001000)
564 | ((right >> 14) & 0x00000808)
565 | ((right >> 9) & 0x00000400)
566 | ((right) & 0x00000200)
567 | ((right << 7) & 0x00000100)
568 | ((right >> 7) & 0x00000020)
569 | ((right >> 3) & 0x00000011)
570 | ((right << 2) & 0x00000004)
571 | ((right >> 21) & 0x00000002));
572 }
573}
574
575
576/*
577 * Fill a DES context with subkeys calculated from a 64bit key.
578 * Does not check parity bits, but simply ignore them.
579 * Does not check for weak keys.
580 */
581static int
582des_setkey (struct _des_ctx *ctx, const byte * key)
583{
584 static const char *selftest_failed;
585 int i;
586
587 if (! initialized)
588 {
589 initialized = 1;
590 selftest_failed = selftest ();
591
592 if (selftest_failed)
593 log_error ("%s\n", selftest_failed);
594 }
595 if (selftest_failed)
596 return GPG_ERR_SELFTEST_FAILED;
597
598 des_key_schedule (key, ctx->encrypt_subkeys);
599 _gcry_burn_stack (32);
600
601 for(i=0; i<32; i+=2)
602 {
603 ctx->decrypt_subkeys[i]= ctx->encrypt_subkeys[30-i];
604 ctx->decrypt_subkeys[i+1] = ctx->encrypt_subkeys[31-i];
605 }
606
607 return 0;
608}
609
610
611
612/*
613 * Electronic Codebook Mode DES encryption/decryption of data according
614 * to 'mode'.
615 */
616static int
617des_ecb_crypt (struct _des_ctx *ctx, const byte * from, byte * to, int mode)
618{
619 u32 left, right, work;
620 u32 *keys;
621
622 keys = mode ? ctx->decrypt_subkeys : ctx->encrypt_subkeys;
623
624 READ_64BIT_DATA (from, left, right)
625 INITIAL_PERMUTATION (left, work, right)
626
627 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
628 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
629 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
630 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
631 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
632 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
633 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
634 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
635
636 FINAL_PERMUTATION (right, work, left)
637 WRITE_64BIT_DATA (to, right, left)
638
639 return 0;
640}
641
642
643
644/*
645 * Fill a Triple-DES context with subkeys calculated from two 64bit keys.
646 * Does not check the parity bits of the keys, but simply ignore them.
647 * Does not check for weak keys.
648 */
649static int
650tripledes_set2keys (struct _tripledes_ctx *ctx,
651 const byte * key1,
652 const byte * key2)
653{
654 int i;
655
656 des_key_schedule (key1, ctx->encrypt_subkeys);
657 des_key_schedule (key2, &(ctx->decrypt_subkeys[32]));
658 _gcry_burn_stack (32);
659
660 for(i=0; i<32; i+=2)
661 {
662 ctx->decrypt_subkeys[i] = ctx->encrypt_subkeys[30-i];
663 ctx->decrypt_subkeys[i+1] = ctx->encrypt_subkeys[31-i];
664
665 ctx->encrypt_subkeys[i+32] = ctx->decrypt_subkeys[62-i];
666 ctx->encrypt_subkeys[i+33] = ctx->decrypt_subkeys[63-i];
667
668 ctx->encrypt_subkeys[i+64] = ctx->encrypt_subkeys[i];
669 ctx->encrypt_subkeys[i+65] = ctx->encrypt_subkeys[i+1];
670
671 ctx->decrypt_subkeys[i+64] = ctx->decrypt_subkeys[i];
672 ctx->decrypt_subkeys[i+65] = ctx->decrypt_subkeys[i+1];
673 }
674
675 return 0;
676}
677
678
679
680/*
681 * Fill a Triple-DES context with subkeys calculated from three 64bit keys.
682 * Does not check the parity bits of the keys, but simply ignore them.
683 * Does not check for weak keys.
684 */
685static int
686tripledes_set3keys (struct _tripledes_ctx *ctx,
687 const byte * key1,
688 const byte * key2,
689 const byte * key3)
690{
691 static const char *selftest_failed;
692 int i;
693
694 if (! initialized)
695 {
696 initialized = 1;
697 selftest_failed = selftest ();
698
699 if (selftest_failed)
700 log_error ("%s\n", selftest_failed);
701 }
702 if (selftest_failed)
703 return GPG_ERR_SELFTEST_FAILED;
704
705 des_key_schedule (key1, ctx->encrypt_subkeys);
706 des_key_schedule (key2, &(ctx->decrypt_subkeys[32]));
707 des_key_schedule (key3, &(ctx->encrypt_subkeys[64]));
708 _gcry_burn_stack (32);
709
710 for(i=0; i<32; i+=2)
711 {
712 ctx->decrypt_subkeys[i] = ctx->encrypt_subkeys[94-i];
713 ctx->decrypt_subkeys[i+1] = ctx->encrypt_subkeys[95-i];
714
715 ctx->encrypt_subkeys[i+32] = ctx->decrypt_subkeys[62-i];
716 ctx->encrypt_subkeys[i+33] = ctx->decrypt_subkeys[63-i];
717
718 ctx->decrypt_subkeys[i+64] = ctx->encrypt_subkeys[30-i];
719 ctx->decrypt_subkeys[i+65] = ctx->encrypt_subkeys[31-i];
720 }
721
722 return 0;
723}
724
725
726
727/*
728 * Electronic Codebook Mode Triple-DES encryption/decryption of data
729 * according to 'mode'. Sometimes this mode is named 'EDE' mode
730 * (Encryption-Decryption-Encryption).
731 */
732static int
733tripledes_ecb_crypt (struct _tripledes_ctx *ctx, const byte * from,
734 byte * to, int mode)
735{
736 u32 left, right, work;
737 u32 *keys;
738
739 keys = mode ? ctx->decrypt_subkeys : ctx->encrypt_subkeys;
740
741 READ_64BIT_DATA (from, left, right)
742 INITIAL_PERMUTATION (left, work, right)
743
744 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
745 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
746 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
747 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
748 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
749 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
750 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
751 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
752
753 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
754 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
755 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
756 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
757 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
758 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
759 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
760 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
761
762 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
763 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
764 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
765 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
766 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
767 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
768 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
769 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
770
771 FINAL_PERMUTATION (right, work, left)
772 WRITE_64BIT_DATA (to, right, left)
773
774 return 0;
775}
776
777
778
779
780
781/*
782 * Check whether the 8 byte key is weak.
783 * Does not check the parity bits of the key but simple ignore them.
784 */
785static int
786is_weak_key ( const byte *key )
787{
788 byte work[8];
789 int i, left, right, middle, cmp_result;
790
791 /* clear parity bits */
792 for(i=0; i<8; ++i)
793 work[i] = key[i] & 0xfe;
794
795 /* binary search in the weak key table */
796 left = 0;
797 right = 63;
798 while(left <= right)
799 {
800 middle = (left + right) / 2;
801
802 if ( !(cmp_result=working_memcmp(work, weak_keys[middle], 8)) )
803 return -1;
804
805 if ( cmp_result > 0 )
806 left = middle + 1;
807 else
808 right = middle - 1;
809 }
810
811 return 0;
812}
813
814
815
816/*
817 * Performs a selftest of this DES/Triple-DES implementation.
818 * Returns an string with the error text on failure.
819 * Returns NULL if all is ok.
820 */
821static const char *
822selftest (void)
823{
824 /*
825 * Check if 'u32' is really 32 bits wide. This DES / 3DES implementation
826 * need this.
827 */
828 if (sizeof (u32) != 4)
829 return "Wrong word size for DES configured.";
830
831 /*
832 * DES Maintenance Test
833 */
834 {
835 int i;
836 byte key[8] =
837 {0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
838 byte input[8] =
839 {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
840 byte result[8] =
841 {0x24, 0x6e, 0x9d, 0xb9, 0xc5, 0x50, 0x38, 0x1a};
842 byte temp1[8], temp2[8], temp3[8];
843 des_ctx des;
844
845 for (i = 0; i < 64; ++i)
846 {
847 des_setkey (des, key);
848 des_ecb_encrypt (des, input, temp1);
849 des_ecb_encrypt (des, temp1, temp2);
850 des_setkey (des, temp2);
851 des_ecb_decrypt (des, temp1, temp3);
852 memcpy (key, temp3, 8);
853 memcpy (input, temp1, 8);
854 }
855 if (memcmp (temp3, result, 8))
856 return "DES maintenance test failed.";
857 }
858
859
860 /*
861 * Self made Triple-DES test(Does somebody know an official test?)
862 */
863 {
864 int i;
865 byte input[8] =
866 {0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
867 byte key1[8] =
868 {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
869 byte key2[8] =
870 {0x11, 0x22, 0x33, 0x44, 0xff, 0xaa, 0xcc, 0xdd};
871 byte result[8] =
872 {0x7b, 0x38, 0x3b, 0x23, 0xa2, 0x7d, 0x26, 0xd3};
873
874 tripledes_ctx des3;
875
876 for (i = 0; i < 16; ++i)
877 {
878 tripledes_set2keys (des3, key1, key2);
879 tripledes_ecb_encrypt (des3, input, key1);
880 tripledes_ecb_decrypt (des3, input, key2);
881 tripledes_set3keys (des3, key1, input, key2);
882 tripledes_ecb_encrypt (des3, input, input);
883 }
884 if (memcmp (input, result, 8))
885 return "Triple-DES test failed.";
886 }
887
888 /*
889 * More Triple-DES test. These are testvectors as used by SSLeay,
890 * thanks to Jeroen C. van Gelderen.
891 */
892 {
893 struct { byte key[24]; byte plain[8]; byte cipher[8]; } testdata[] = {
894 { { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
895 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
896 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },
897 { 0x95,0xF8,0xA5,0xE5,0xDD,0x31,0xD9,0x00 },
898 { 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }
899 },
900
901 { { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
902 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
903 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },
904 { 0x9D,0x64,0x55,0x5A,0x9A,0x10,0xB8,0x52, },
905 { 0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00 }
906 },
907 { { 0x38,0x49,0x67,0x4C,0x26,0x02,0x31,0x9E,
908 0x38,0x49,0x67,0x4C,0x26,0x02,0x31,0x9E,
909 0x38,0x49,0x67,0x4C,0x26,0x02,0x31,0x9E },
910 { 0x51,0x45,0x4B,0x58,0x2D,0xDF,0x44,0x0A },
911 { 0x71,0x78,0x87,0x6E,0x01,0xF1,0x9B,0x2A }
912 },
913 { { 0x04,0xB9,0x15,0xBA,0x43,0xFE,0xB5,0xB6,
914 0x04,0xB9,0x15,0xBA,0x43,0xFE,0xB5,0xB6,
915 0x04,0xB9,0x15,0xBA,0x43,0xFE,0xB5,0xB6 },
916 { 0x42,0xFD,0x44,0x30,0x59,0x57,0x7F,0xA2 },
917 { 0xAF,0x37,0xFB,0x42,0x1F,0x8C,0x40,0x95 }
918 },
919 { { 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,
920 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,
921 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF },
922 { 0x73,0x6F,0x6D,0x65,0x64,0x61,0x74,0x61 },
923 { 0x3D,0x12,0x4F,0xE2,0x19,0x8B,0xA3,0x18 }
924 },
925 { { 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,
926 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
927 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF },
928 { 0x73,0x6F,0x6D,0x65,0x64,0x61,0x74,0x61 },
929 { 0xFB,0xAB,0xA1,0xFF,0x9D,0x05,0xE9,0xB1 }
930 },
931 { { 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,
932 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
933 0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10 },
934 { 0x73,0x6F,0x6D,0x65,0x64,0x61,0x74,0x61 },
935 { 0x18,0xd7,0x48,0xe5,0x63,0x62,0x05,0x72 }
936 },
937 { { 0x03,0x52,0x02,0x07,0x67,0x20,0x82,0x17,
938 0x86,0x02,0x87,0x66,0x59,0x08,0x21,0x98,
939 0x64,0x05,0x6A,0xBD,0xFE,0xA9,0x34,0x57 },
940 { 0x73,0x71,0x75,0x69,0x67,0x67,0x6C,0x65 },
941 { 0xc0,0x7d,0x2a,0x0f,0xa5,0x66,0xfa,0x30 }
942 },
943 { { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
944 0x80,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
945 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x02 },
946 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
947 { 0xe6,0xe6,0xdd,0x5b,0x7e,0x72,0x29,0x74 }
948 },
949 { { 0x10,0x46,0x10,0x34,0x89,0x98,0x80,0x20,
950 0x91,0x07,0xD0,0x15,0x89,0x19,0x01,0x01,
951 0x19,0x07,0x92,0x10,0x98,0x1A,0x01,0x01 },
952 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
953 { 0xe1,0xef,0x62,0xc3,0x32,0xfe,0x82,0x5b }
954 }
955 };
956
957 byte result[8];
958 int i;
959 static charerror[80];
960 tripledes_ctxdes3;
961
962 for (i=0; i<sizeof(testdata)/sizeof(*testdata); ++i)
963 {
964 tripledes_set3keys (des3, testdata[i].key,
965 testdata[i].key + 8, testdata[i].key + 16);
966
967 tripledes_ecb_encrypt (des3, testdata[i].plain, result);
968 if (memcmp (testdata[i].cipher, result, 8))
969 {
970 sprintf (error, "Triple-DES SSLeay test pattern no. %d "
971 "failed on encryption.", i+1);
972 return error;
973 }
974
975 tripledes_ecb_decrypt (des3, testdata[i].cipher, result);
976 if (memcmp (testdata[i].plain, result, 8))
977 {
978 sprintf (error, "Triple-DES SSLeay test pattern no. %d "
979 "failed on decryption.", i+1);
980 return error;
981 }
982 }
983 }
984
985 /*
986 * Check the weak key detection. We simply assume that the table
987 * with weak keys is ok and check every key in the table if it is
988 * detected... (This test is a little bit stupid).
989 */
990 {
991 int i;
992 unsigned char *p;
993 gcry_md_hd_t h;
994
995 if (gcry_md_open (&h, GCRY_MD_SHA1, 0))
996 return "SHA1 not available";
997
998 for (i = 0; i < 64; ++i)
999 gcry_md_write (h, weak_keys[i], 8);
1000 p = gcry_md_read (h, GCRY_MD_SHA1);
1001 i = memcmp (p, weak_keys_chksum, 20);
1002 gcry_md_close (h);
1003 if (i)
1004 return "weak key table defect";
1005
1006 for (i = 0; i < 64; ++i)
1007 if (!is_weak_key(weak_keys[i]))
1008 return "DES weak key detection failed";
1009 }
1010
1011 return 0;
1012}
1013
1014
1015static gcry_err_code_t
1016do_tripledes_setkey ( void *context, const byte *key, unsigned keylen )
1017{
1018 struct _tripledes_ctx *ctx = (struct _tripledes_ctx *) context;
1019
1020 if( keylen != 24 )
1021 return GPG_ERR_INV_KEYLEN;
1022
1023 tripledes_set3keys ( ctx, key, key+8, key+16);
1024
1025 if( is_weak_key( key ) || is_weak_key( key+8 ) || is_weak_key( key+16 ) )
1026 {
1027 _gcry_burn_stack (64);
1028 return GPG_ERR_WEAK_KEY;
1029 }
1030 _gcry_burn_stack (64);
1031
1032 return GPG_ERR_NO_ERROR;
1033}
1034
1035
1036static void
1037do_tripledes_encrypt( void *context, byte *outbuf, const byte *inbuf )
1038{
1039 struct _tripledes_ctx *ctx = (struct _tripledes_ctx *) context;
1040
1041 tripledes_ecb_encrypt ( ctx, inbuf, outbuf );
1042 _gcry_burn_stack (32);
1043}
1044
1045static void
1046do_tripledes_decrypt( void *context, byte *outbuf, const byte *inbuf )
1047{
1048 struct _tripledes_ctx *ctx = (struct _tripledes_ctx *) context;
1049 tripledes_ecb_decrypt ( ctx, inbuf, outbuf );
1050 _gcry_burn_stack (32);
1051}
1052
1053static gcry_err_code_t
1054do_des_setkey (void *context, const byte *key, unsigned keylen)
1055{
1056 struct _des_ctx *ctx = (struct _des_ctx *) context;
1057
1058 if (keylen != 8)
1059 return GPG_ERR_INV_KEYLEN;
1060
1061 des_setkey (ctx, key);
1062
1063 if (is_weak_key (key)) {
1064 _gcry_burn_stack (64);
1065 return GPG_ERR_WEAK_KEY;
1066 }
1067 _gcry_burn_stack (64);
1068
1069 return GPG_ERR_NO_ERROR;
1070}
1071
1072
1073static void
1074do_des_encrypt( void *context, byte *outbuf, const byte *inbuf )
1075{
1076 struct _des_ctx *ctx = (struct _des_ctx *) context;
1077
1078 des_ecb_encrypt ( ctx, inbuf, outbuf );
1079 _gcry_burn_stack (32);
1080}
1081
1082static void
1083do_des_decrypt( void *context, byte *outbuf, const byte *inbuf )
1084{
1085 struct _des_ctx *ctx = (struct _des_ctx *) context;
1086
1087 des_ecb_decrypt ( ctx, inbuf, outbuf );
1088 _gcry_burn_stack (32);
1089}
1090
1091gcry_cipher_spec_t _gcry_cipher_spec_des =
1092 {
1093 "DES", NULL, NULL, 8, 64, sizeof (struct _des_ctx),
1094 do_des_setkey, do_des_encrypt, do_des_decrypt
1095 };
1096
1097static gcry_cipher_oid_spec_t oids_tripledes[] =
1098 {
1099 { "1.2.840.113549.3.7", GCRY_CIPHER_MODE_CBC },
1100 /* Teletrust specific OID for 3DES. */
1101 { "1.3.36.3.1.3.2.1", GCRY_CIPHER_MODE_CBC },
1102 /* pbeWithSHAAnd3_KeyTripleDES_CBC */
1103 { "1.2.840.113549.1.12.1.3", GCRY_CIPHER_MODE_CBC },
1104 { NULL }
1105 };
1106
1107gcry_cipher_spec_t _gcry_cipher_spec_tripledes =
1108 {
1109 "3DES", NULL, oids_tripledes, 8, 192, sizeof (struct _tripledes_ctx),
1110 do_tripledes_setkey, do_tripledes_encrypt, do_tripledes_decrypt
1111 };
diff --git a/pwmanager/libcrypt/cipher/dsa.c b/pwmanager/libcrypt/cipher/dsa.c
new file mode 100644
index 0000000..c3b4c82
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/dsa.c
@@ -0,0 +1,486 @@
1/* dsa.c - DSA signature scheme
2 * Copyright (C) 1998, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <config.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <assert.h>
26
27#include "g10lib.h"
28#include "mpi.h"
29#include "cipher.h"
30
31typedef struct
32{
33 gcry_mpi_t p; /* prime */
34 gcry_mpi_t q; /* group order */
35 gcry_mpi_t g; /* group generator */
36 gcry_mpi_t y; /* g^x mod p */
37} DSA_public_key;
38
39
40typedef struct
41{
42 gcry_mpi_t p; /* prime */
43 gcry_mpi_t q; /* group order */
44 gcry_mpi_t g; /* group generator */
45 gcry_mpi_t y; /* g^x mod p */
46 gcry_mpi_t x; /* secret exponent */
47} DSA_secret_key;
48
49
50static gcry_mpi_t gen_k (gcry_mpi_t q);
51static void test_keys (DSA_secret_key *sk, unsigned qbits);
52static int check_secret_key (DSA_secret_key *sk);
53static void generate (DSA_secret_key *sk, unsigned nbits,
54 gcry_mpi_t **ret_factors);
55static void sign (gcry_mpi_t r, gcry_mpi_t s, gcry_mpi_t input,
56 DSA_secret_key *skey);
57static int verify (gcry_mpi_t r, gcry_mpi_t s, gcry_mpi_t input,
58 DSA_public_key *pkey);
59
60static void (*progress_cb) (void *,const char *, int, int, int );
61static void *progress_cb_data;
62
63
64void
65_gcry_register_pk_dsa_progress (void (*cb) (void *, const char *,
66 int, int, int),
67 void *cb_data)
68{
69 progress_cb = cb;
70 progress_cb_data = cb_data;
71}
72
73
74static void
75progress (int c)
76{
77 if (progress_cb)
78 progress_cb (progress_cb_data, "pk_dsa", c, 0, 0);
79}
80
81
82/*
83 * Generate a random secret exponent k less than q.
84 */
85static gcry_mpi_t
86gen_k( gcry_mpi_t q )
87{
88 gcry_mpi_t k = mpi_alloc_secure( mpi_get_nlimbs(q) );
89 unsigned int nbits = mpi_get_nbits(q);
90 unsigned int nbytes = (nbits+7)/8;
91 char *rndbuf = NULL;
92
93 if ( DBG_CIPHER )
94 log_debug("choosing a random k ");
95 for (;;)
96 {
97 if( DBG_CIPHER )
98 progress('.');
99
100 if ( !rndbuf || nbits < 32 )
101 {
102 gcry_free(rndbuf);
103 rndbuf = gcry_random_bytes_secure( (nbits+7)/8, GCRY_STRONG_RANDOM );
104 }
105 else
106 { /* Change only some of the higher bits. We could improve
107 this by directly requesting more memory at the first call
108 to get_random_bytes() and use this the here maybe it is
109 easier to do this directly in random.c. */
110 char *pp = gcry_random_bytes_secure( 4, GCRY_STRONG_RANDOM );
111 memcpy( rndbuf,pp, 4 );
112 gcry_free(pp);
113 }
114 _gcry_mpi_set_buffer( k, rndbuf, nbytes, 0 );
115 if ( mpi_test_bit( k, nbits-1 ) )
116 mpi_set_highbit( k, nbits-1 );
117 else
118 {
119 mpi_set_highbit( k, nbits-1 );
120 mpi_clear_bit( k, nbits-1 );
121 }
122
123 if( !(mpi_cmp( k, q ) < 0) ) /* check: k < q */
124 {
125 if( DBG_CIPHER )
126 progress('+');
127 continue; /* no */
128 }
129 if( !(mpi_cmp_ui( k, 0 ) > 0) ) /* check: k > 0 */
130 {
131 if( DBG_CIPHER )
132 progress('-');
133 continue; /* no */
134 }
135 break;/* okay */
136 }
137 gcry_free(rndbuf);
138 if( DBG_CIPHER )
139 progress('\n');
140
141 return k;
142}
143
144
145static void
146test_keys( DSA_secret_key *sk, unsigned qbits )
147{
148 DSA_public_key pk;
149 gcry_mpi_t test = gcry_mpi_new ( qbits );
150 gcry_mpi_t out1_a = gcry_mpi_new ( qbits );
151 gcry_mpi_t out1_b = gcry_mpi_new ( qbits );
152
153 pk.p = sk->p;
154 pk.q = sk->q;
155 pk.g = sk->g;
156 pk.y = sk->y;
157 gcry_mpi_randomize( test, qbits, GCRY_WEAK_RANDOM );
158
159 sign( out1_a, out1_b, test, sk );
160 if( !verify( out1_a, out1_b, test, &pk ) )
161 log_fatal("DSA:: sign, verify failed\n");
162
163 gcry_mpi_release ( test );
164 gcry_mpi_release ( out1_a );
165 gcry_mpi_release ( out1_b );
166}
167
168
169
170/*
171 Generate a DSA key pair with a key of size NBITS.
172 Returns: 2 structures filled with all needed values
173 and an array with the n-1 factors of (p-1)
174 */
175static void
176generate( DSA_secret_key *sk, unsigned nbits, gcry_mpi_t **ret_factors )
177{
178 gcry_mpi_t p; /* the prime */
179 gcry_mpi_t q; /* the 160 bit prime factor */
180 gcry_mpi_t g; /* the generator */
181 gcry_mpi_t y; /* g^x mod p */
182 gcry_mpi_t x; /* the secret exponent */
183 gcry_mpi_t h, e; /* helper */
184 unsigned qbits;
185 byte *rndbuf;
186
187 assert( nbits >= 512 && nbits <= 1024 );
188
189 qbits = 160;
190 p = _gcry_generate_elg_prime( 1, nbits, qbits, NULL, ret_factors );
191 /* get q out of factors */
192 q = mpi_copy((*ret_factors)[0]);
193 if( mpi_get_nbits(q) != qbits )
194 BUG();
195
196 /* Find a generator g (h and e are helpers).
197 e = (p-1)/q */
198 e = mpi_alloc( mpi_get_nlimbs(p) );
199 mpi_sub_ui( e, p, 1 );
200 mpi_fdiv_q( e, e, q );
201 g = mpi_alloc( mpi_get_nlimbs(p) );
202 h = mpi_alloc_set_ui( 1 ); /* we start with 2 */
203 do
204 {
205 mpi_add_ui( h, h, 1 );
206 /* g = h^e mod p */
207 gcry_mpi_powm( g, h, e, p );
208 }
209 while( !mpi_cmp_ui( g, 1 ) ); /* continue until g != 1 */
210
211 /* Select a random number which has these properties:
212 * 0 < x < q-1
213 * This must be a very good random number because this
214 * is the secret part. */
215 if( DBG_CIPHER )
216 log_debug("choosing a random x ");
217 assert( qbits >= 160 );
218 x = mpi_alloc_secure( mpi_get_nlimbs(q) );
219 mpi_sub_ui( h, q, 1 ); /* put q-1 into h */
220 rndbuf = NULL;
221 do
222 {
223 if( DBG_CIPHER )
224 progress('.');
225 if( !rndbuf )
226 rndbuf = gcry_random_bytes_secure( (qbits+7)/8,
227 GCRY_VERY_STRONG_RANDOM );
228 else
229 { /* Change only some of the higher bits (= 2 bytes)*/
230 char *r = gcry_random_bytes_secure (2, GCRY_VERY_STRONG_RANDOM);
231 memcpy(rndbuf, r, 2 );
232 gcry_free(r);
233 }
234
235 _gcry_mpi_set_buffer( x, rndbuf, (qbits+7)/8, 0 );
236 mpi_clear_highbit( x, qbits+1 );
237 }
238 while ( !( mpi_cmp_ui( x, 0 )>0 && mpi_cmp( x, h )<0 ) );
239 gcry_free(rndbuf);
240 mpi_free( e );
241 mpi_free( h );
242
243 /* y = g^x mod p */
244 y = mpi_alloc( mpi_get_nlimbs(p) );
245 gcry_mpi_powm( y, g, x, p );
246
247 if( DBG_CIPHER )
248 {
249 progress('\n');
250 log_mpidump("dsa p= ", p );
251 log_mpidump("dsa q= ", q );
252 log_mpidump("dsa g= ", g );
253 log_mpidump("dsa y= ", y );
254 log_mpidump("dsa x= ", x );
255 }
256
257 /* Copy the stuff to the key structures. */
258 sk->p = p;
259 sk->q = q;
260 sk->g = g;
261 sk->y = y;
262 sk->x = x;
263
264 /* Now we can test our keys (this should never fail!). */
265 test_keys( sk, qbits );
266}
267
268
269
270/*
271 Test whether the secret key is valid.
272 Returns: if this is a valid key.
273 */
274static int
275check_secret_key( DSA_secret_key *sk )
276{
277 int rc;
278 gcry_mpi_t y = mpi_alloc( mpi_get_nlimbs(sk->y) );
279
280 gcry_mpi_powm( y, sk->g, sk->x, sk->p );
281 rc = !mpi_cmp( y, sk->y );
282 mpi_free( y );
283 return rc;
284}
285
286
287
288/*
289 Make a DSA signature from HASH and put it into r and s.
290 */
291static void
292sign(gcry_mpi_t r, gcry_mpi_t s, gcry_mpi_t hash, DSA_secret_key *skey )
293{
294 gcry_mpi_t k;
295 gcry_mpi_t kinv;
296 gcry_mpi_t tmp;
297
298 /* Select a random k with 0 < k < q */
299 k = gen_k( skey->q );
300
301 /* r = (a^k mod p) mod q */
302 gcry_mpi_powm( r, skey->g, k, skey->p );
303 mpi_fdiv_r( r, r, skey->q );
304
305 /* kinv = k^(-1) mod q */
306 kinv = mpi_alloc( mpi_get_nlimbs(k) );
307 mpi_invm(kinv, k, skey->q );
308
309 /* s = (kinv * ( hash + x * r)) mod q */
310 tmp = mpi_alloc( mpi_get_nlimbs(skey->p) );
311 mpi_mul( tmp, skey->x, r );
312 mpi_add( tmp, tmp, hash );
313 mpi_mulm( s , kinv, tmp, skey->q );
314
315 mpi_free(k);
316 mpi_free(kinv);
317 mpi_free(tmp);
318}
319
320
321/*
322 Returns true if the signature composed from R and S is valid.
323 */
324static int
325verify (gcry_mpi_t r, gcry_mpi_t s, gcry_mpi_t hash, DSA_public_key *pkey )
326{
327 int rc;
328 gcry_mpi_t w, u1, u2, v;
329 gcry_mpi_t base[3];
330 gcry_mpi_t ex[3];
331
332 if( !(mpi_cmp_ui( r, 0 ) > 0 && mpi_cmp( r, pkey->q ) < 0) )
333 return 0; /* assertion0 < r < q failed */
334 if( !(mpi_cmp_ui( s, 0 ) > 0 && mpi_cmp( s, pkey->q ) < 0) )
335 return 0; /* assertion0 < s < q failed */
336
337 w = mpi_alloc( mpi_get_nlimbs(pkey->q) );
338 u1 = mpi_alloc( mpi_get_nlimbs(pkey->q) );
339 u2 = mpi_alloc( mpi_get_nlimbs(pkey->q) );
340 v = mpi_alloc( mpi_get_nlimbs(pkey->p) );
341
342 /* w = s^(-1) mod q */
343 mpi_invm( w, s, pkey->q );
344
345 /* u1 = (hash * w) mod q */
346 mpi_mulm( u1, hash, w, pkey->q );
347
348 /* u2 = r * w mod q */
349 mpi_mulm( u2, r, w, pkey->q );
350
351 /* v = g^u1 * y^u2 mod p mod q */
352 base[0] = pkey->g; ex[0] = u1;
353 base[1] = pkey->y; ex[1] = u2;
354 base[2] = NULL; ex[2] = NULL;
355 mpi_mulpowm( v, base, ex, pkey->p );
356 mpi_fdiv_r( v, v, pkey->q );
357
358 rc = !mpi_cmp( v, r );
359
360 mpi_free(w);
361 mpi_free(u1);
362 mpi_free(u2);
363 mpi_free(v);
364
365 return rc;
366}
367
368
369/*********************************************
370 ************** interface ******************
371 *********************************************/
372
373gcry_err_code_t
374_gcry_dsa_generate (int algo, unsigned nbits, unsigned long dummy,
375 gcry_mpi_t *skey, gcry_mpi_t **retfactors)
376{
377 DSA_secret_key sk;
378
379 generate (&sk, nbits, retfactors);
380 skey[0] = sk.p;
381 skey[1] = sk.q;
382 skey[2] = sk.g;
383 skey[3] = sk.y;
384 skey[4] = sk.x;
385
386 return GPG_ERR_NO_ERROR;
387}
388
389
390gcry_err_code_t
391_gcry_dsa_check_secret_key (int algo, gcry_mpi_t *skey)
392{
393 gcry_err_code_t err = GPG_ERR_NO_ERROR;
394 DSA_secret_key sk;
395
396 if ((! skey[0]) || (! skey[1]) || (! skey[2]) || (! skey[3]) || (! skey[4]))
397 err = GPG_ERR_BAD_MPI;
398 else
399 {
400 sk.p = skey[0];
401 sk.q = skey[1];
402 sk.g = skey[2];
403 sk.y = skey[3];
404 sk.x = skey[4];
405 if (! check_secret_key (&sk))
406 err = GPG_ERR_BAD_SECKEY;
407 }
408
409 return err;
410}
411
412
413gcry_err_code_t
414_gcry_dsa_sign (int algo, gcry_mpi_t *resarr, gcry_mpi_t data, gcry_mpi_t *skey)
415{
416 gcry_err_code_t err = GPG_ERR_NO_ERROR;
417 DSA_secret_key sk;
418
419 if ((! data)
420 || (! skey[0]) || (! skey[1]) || (! skey[2])
421 || (! skey[3]) || (! skey[4]))
422 err = GPG_ERR_BAD_MPI;
423 else
424 {
425 sk.p = skey[0];
426 sk.q = skey[1];
427 sk.g = skey[2];
428 sk.y = skey[3];
429 sk.x = skey[4];
430 resarr[0] = mpi_alloc (mpi_get_nlimbs (sk.p));
431 resarr[1] = mpi_alloc (mpi_get_nlimbs (sk.p));
432 sign (resarr[0], resarr[1], data, &sk);
433 }
434 return err;
435}
436
437gcry_err_code_t
438_gcry_dsa_verify (int algo, gcry_mpi_t hash, gcry_mpi_t *data, gcry_mpi_t *pkey,
439 int (*cmp) (void *, gcry_mpi_t), void *opaquev)
440{
441 gcry_err_code_t err = GPG_ERR_NO_ERROR;
442 DSA_public_key pk;
443
444 if ((! data[0]) || (! data[1]) || (! hash)
445 || (! pkey[0]) || (! pkey[1]) || (! pkey[2]) || (! pkey[3]))
446 err = GPG_ERR_BAD_MPI;
447 else
448 {
449 pk.p = pkey[0];
450 pk.q = pkey[1];
451 pk.g = pkey[2];
452 pk.y = pkey[3];
453 if (! verify (data[0], data[1], hash, &pk))
454 err = GPG_ERR_BAD_SIGNATURE;
455 }
456 return err;
457}
458
459
460unsigned int
461_gcry_dsa_get_nbits (int algo, gcry_mpi_t *pkey)
462{
463 return mpi_get_nbits (pkey[0]);
464}
465
466static char *dsa_names[] =
467 {
468 "dsa",
469 "openpgp-dsa",
470 NULL,
471 };
472
473gcry_pk_spec_t _gcry_pubkey_spec_dsa =
474 {
475 "DSA", dsa_names,
476 "pqgy", "pqgyx", "", "rs", "pqgy",
477 GCRY_PK_USAGE_SIGN,
478 _gcry_dsa_generate,
479 _gcry_dsa_check_secret_key,
480 NULL,
481 NULL,
482 _gcry_dsa_sign,
483 _gcry_dsa_verify,
484 _gcry_dsa_get_nbits,
485 };
486
diff --git a/pwmanager/libcrypt/cipher/elgamal.c b/pwmanager/libcrypt/cipher/elgamal.c
new file mode 100644
index 0000000..e62b1e5
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/elgamal.c
@@ -0,0 +1,682 @@
1/* Elgamal.c - ElGamal Public Key encryption
2 * Copyright (C) 1998, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 *
20 * For a description of the algorithm, see:
21 * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1996.
22 * ISBN 0-471-11709-9. Pages 476 ff.
23 */
24
25#include <config.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include "g10lib.h"
30#include "mpi.h"
31#include "cipher.h"
32
33typedef struct
34{
35 gcry_mpi_t p; /* prime */
36 gcry_mpi_t g; /* group generator */
37 gcry_mpi_t y; /* g^x mod p */
38} ELG_public_key;
39
40
41typedef struct
42{
43 gcry_mpi_t p; /* prime */
44 gcry_mpi_t g; /* group generator */
45 gcry_mpi_t y; /* g^x mod p */
46 gcry_mpi_t x; /* secret exponent */
47} ELG_secret_key;
48
49
50static void test_keys (ELG_secret_key *sk, unsigned nbits);
51static gcry_mpi_t gen_k (gcry_mpi_t p, int small_k);
52static void generate (ELG_secret_key *sk, unsigned nbits, gcry_mpi_t **factors);
53static int check_secret_key (ELG_secret_key *sk);
54static void do_encrypt (gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input,
55 ELG_public_key *pkey);
56static void decrypt (gcry_mpi_t output, gcry_mpi_t a, gcry_mpi_t b,
57 ELG_secret_key *skey);
58static void sign (gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input,
59 ELG_secret_key *skey);
60static int verify (gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input,
61 ELG_public_key *pkey);
62
63
64static void (*progress_cb) (void *, const char *, int, int, int);
65static void *progress_cb_data;
66
67void
68_gcry_register_pk_elg_progress (void (*cb) (void *, const char *,
69 int, int, int),
70 void *cb_data)
71{
72 progress_cb = cb;
73 progress_cb_data = cb_data;
74}
75
76
77static void
78progress (int c)
79{
80 if (progress_cb)
81 progress_cb (progress_cb_data, "pk_elg", c, 0, 0);
82}
83
84
85/****************
86 * Michael Wiener's table on subgroup sizes to match field sizes
87 * (floating around somewhere - Fixme: need a reference)
88 */
89static unsigned int
90wiener_map( unsigned int n )
91{
92 static struct { unsigned int p_n, q_n; } t[] =
93 { /* p q attack cost */
94 { 512, 119 },/* 9 x 10^17 */
95 { 768, 145 },/* 6 x 10^21 */
96 { 1024, 165 },/* 7 x 10^24 */
97 { 1280, 183 },/* 3 x 10^27 */
98 { 1536, 198 },/* 7 x 10^29 */
99 { 1792, 212 },/* 9 x 10^31 */
100 { 2048, 225 },/* 8 x 10^33 */
101 { 2304, 237 },/* 5 x 10^35 */
102 { 2560, 249 },/* 3 x 10^37 */
103 { 2816, 259 },/* 1 x 10^39 */
104 { 3072, 269 },/* 3 x 10^40 */
105 { 3328, 279 },/* 8 x 10^41 */
106 { 3584, 288 },/* 2 x 10^43 */
107 { 3840, 296 },/* 4 x 10^44 */
108 { 4096, 305 },/* 7 x 10^45 */
109 { 4352, 313 },/* 1 x 10^47 */
110 { 4608, 320 },/* 2 x 10^48 */
111 { 4864, 328 },/* 2 x 10^49 */
112 { 5120, 335 },/* 3 x 10^50 */
113 { 0, 0 }
114 };
115 int i;
116
117 for(i=0; t[i].p_n; i++ )
118 {
119 if( n <= t[i].p_n )
120 return t[i].q_n;
121 }
122 /* Not in table - use an arbitrary high number. */
123 return n / 8 + 200;
124}
125
126static void
127test_keys( ELG_secret_key *sk, unsigned nbits )
128{
129 ELG_public_key pk;
130 gcry_mpi_t test = gcry_mpi_new ( 0 );
131 gcry_mpi_t out1_a = gcry_mpi_new ( nbits );
132 gcry_mpi_t out1_b = gcry_mpi_new ( nbits );
133 gcry_mpi_t out2 = gcry_mpi_new ( nbits );
134
135 pk.p = sk->p;
136 pk.g = sk->g;
137 pk.y = sk->y;
138
139 gcry_mpi_randomize( test, nbits, GCRY_WEAK_RANDOM );
140
141 do_encrypt( out1_a, out1_b, test, &pk );
142 decrypt( out2, out1_a, out1_b, sk );
143 if( mpi_cmp( test, out2 ) )
144 log_fatal("ElGamal operation: encrypt, decrypt failed\n");
145
146 sign( out1_a, out1_b, test, sk );
147 if( !verify( out1_a, out1_b, test, &pk ) )
148 log_fatal("ElGamal operation: sign, verify failed\n");
149
150 gcry_mpi_release ( test );
151 gcry_mpi_release ( out1_a );
152 gcry_mpi_release ( out1_b );
153 gcry_mpi_release ( out2 );
154}
155
156
157/****************
158 * Generate a random secret exponent k from prime p, so that k is
159 * relatively prime to p-1. With SMALL_K set, k will be selected for
160 * better encryption performance - this must never be used signing!
161 */
162static gcry_mpi_t
163gen_k( gcry_mpi_t p, int small_k )
164{
165 gcry_mpi_t k = mpi_alloc_secure( 0 );
166 gcry_mpi_t temp = mpi_alloc( mpi_get_nlimbs(p) );
167 gcry_mpi_t p_1 = mpi_copy(p);
168 unsigned int orig_nbits = mpi_get_nbits(p);
169 unsigned int nbits, nbytes;
170 char *rndbuf = NULL;
171
172 if (small_k)
173 {
174 /* Using a k much lesser than p is sufficient for encryption and
175 * it greatly improves the encryption performance. We use
176 * Wiener's table and add a large safety margin. */
177 nbits = wiener_map( orig_nbits ) * 3 / 2;
178 if( nbits >= orig_nbits )
179 BUG();
180 }
181 else
182 nbits = orig_nbits;
183
184
185 nbytes = (nbits+7)/8;
186 if( DBG_CIPHER )
187 log_debug("choosing a random k ");
188 mpi_sub_ui( p_1, p, 1);
189 for(;;)
190 {
191 if( !rndbuf || nbits < 32 )
192 {
193 gcry_free(rndbuf);
194 rndbuf = gcry_random_bytes_secure( nbytes, GCRY_STRONG_RANDOM );
195 }
196 else
197 {
198 /* Change only some of the higher bits. We could improve
199 this by directly requesting more memory at the first call
200 to get_random_bytes() and use this the here maybe it is
201 easier to do this directly in random.c Anyway, it is
202 highly inlikely that we will ever reach this code. */
203 char *pp = gcry_random_bytes_secure( 4, GCRY_STRONG_RANDOM );
204 memcpy( rndbuf, pp, 4 );
205 gcry_free(pp);
206 }
207 _gcry_mpi_set_buffer( k, rndbuf, nbytes, 0 );
208
209 for(;;)
210 {
211 if( !(mpi_cmp( k, p_1 ) < 0) ) /* check: k < (p-1) */
212 {
213 if( DBG_CIPHER )
214 progress('+');
215 break; /* no */
216 }
217 if( !(mpi_cmp_ui( k, 0 ) > 0) ) /* check: k > 0 */
218 {
219 if( DBG_CIPHER )
220 progress('-');
221 break; /* no */
222 }
223 if (gcry_mpi_gcd( temp, k, p_1 ))
224 goto found; /* okay, k is relative prime to (p-1) */
225 mpi_add_ui( k, k, 1 );
226 if( DBG_CIPHER )
227 progress('.');
228 }
229 }
230 found:
231 gcry_free(rndbuf);
232 if( DBG_CIPHER )
233 progress('\n');
234 mpi_free(p_1);
235 mpi_free(temp);
236
237 return k;
238}
239
240/****************
241 * Generate a key pair with a key of size NBITS
242 * Returns: 2 structures filles with all needed values
243 * and an array with n-1 factors of (p-1)
244 */
245static void
246generate ( ELG_secret_key *sk, unsigned int nbits, gcry_mpi_t **ret_factors )
247{
248 gcry_mpi_t p; /* the prime */
249 gcry_mpi_t p_min1;
250 gcry_mpi_t g;
251 gcry_mpi_t x; /* the secret exponent */
252 gcry_mpi_t y;
253 gcry_mpi_t temp;
254 unsigned int qbits;
255 unsigned int xbits;
256 byte *rndbuf;
257
258 p_min1 = gcry_mpi_new ( nbits );
259 temp = gcry_mpi_new( nbits );
260 qbits = wiener_map( nbits );
261 if( qbits & 1 ) /* better have a even one */
262 qbits++;
263 g = mpi_alloc(1);
264 p = _gcry_generate_elg_prime( 0, nbits, qbits, g, ret_factors );
265 mpi_sub_ui(p_min1, p, 1);
266
267
268 /* Select a random number which has these properties:
269 * 0 < x < p-1
270 * This must be a very good random number because this is the
271 * secret part. The prime is public and may be shared anyway,
272 * so a random generator level of 1 is used for the prime.
273 *
274 * I don't see a reason to have a x of about the same size
275 * as the p. It should be sufficient to have one about the size
276 * of q or the later used k plus a large safety margin. Decryption
277 * will be much faster with such an x.
278 */
279 xbits = qbits * 3 / 2;
280 if( xbits >= nbits )
281 BUG();
282 x = gcry_mpi_snew ( xbits );
283 if( DBG_CIPHER )
284 log_debug("choosing a random x of size %u", xbits );
285 rndbuf = NULL;
286 do
287 {
288 if( DBG_CIPHER )
289 progress('.');
290 if( rndbuf )
291 { /* Change only some of the higher bits */
292 if( xbits < 16 ) /* should never happen ... */
293 {
294 gcry_free(rndbuf);
295 rndbuf = gcry_random_bytes_secure( (xbits+7)/8,
296 GCRY_VERY_STRONG_RANDOM );
297 }
298 else
299 {
300 char *r = gcry_random_bytes_secure( 2,
301 GCRY_VERY_STRONG_RANDOM );
302 memcpy(rndbuf, r, 2 );
303 gcry_free(r);
304 }
305 }
306 else
307 {
308 rndbuf = gcry_random_bytes_secure( (xbits+7)/8,
309 GCRY_VERY_STRONG_RANDOM );
310 }
311 _gcry_mpi_set_buffer( x, rndbuf, (xbits+7)/8, 0 );
312 mpi_clear_highbit( x, xbits+1 );
313 }
314 while( !( mpi_cmp_ui( x, 0 )>0 && mpi_cmp( x, p_min1 )<0 ) );
315 gcry_free(rndbuf);
316
317 y = gcry_mpi_new (nbits);
318 gcry_mpi_powm( y, g, x, p );
319
320 if( DBG_CIPHER )
321 {
322 progress('\n');
323 log_mpidump("elg p= ", p );
324 log_mpidump("elg g= ", g );
325 log_mpidump("elg y= ", y );
326 log_mpidump("elg x= ", x );
327 }
328
329 /* Copy the stuff to the key structures */
330 sk->p = p;
331 sk->g = g;
332 sk->y = y;
333 sk->x = x;
334
335 /* Now we can test our keys (this should never fail!) */
336 test_keys( sk, nbits - 64 );
337
338 gcry_mpi_release ( p_min1 );
339 gcry_mpi_release ( temp );
340}
341
342
343/****************
344 * Test whether the secret key is valid.
345 * Returns: if this is a valid key.
346 */
347static int
348check_secret_key( ELG_secret_key *sk )
349{
350 int rc;
351 gcry_mpi_t y = mpi_alloc( mpi_get_nlimbs(sk->y) );
352
353 gcry_mpi_powm( y, sk->g, sk->x, sk->p );
354 rc = !mpi_cmp( y, sk->y );
355 mpi_free( y );
356 return rc;
357}
358
359
360static void
361do_encrypt(gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input, ELG_public_key *pkey )
362{
363 gcry_mpi_t k;
364
365 /* Note: maybe we should change the interface, so that it
366 * is possible to check that input is < p and return an
367 * error code.
368 */
369
370 k = gen_k( pkey->p, 1 );
371 gcry_mpi_powm( a, pkey->g, k, pkey->p );
372 /* b = (y^k * input) mod p
373 * = ((y^k mod p) * (input mod p)) mod p
374 * and because input is < p
375 * = ((y^k mod p) * input) mod p
376 */
377 gcry_mpi_powm( b, pkey->y, k, pkey->p );
378 gcry_mpi_mulm( b, b, input, pkey->p );
379#if 0
380 if( DBG_CIPHER )
381 {
382 log_mpidump("elg encrypted y= ", pkey->y);
383 log_mpidump("elg encrypted p= ", pkey->p);
384 log_mpidump("elg encrypted k= ", k);
385 log_mpidump("elg encrypted M= ", input);
386 log_mpidump("elg encrypted a= ", a);
387 log_mpidump("elg encrypted b= ", b);
388 }
389#endif
390 mpi_free(k);
391}
392
393
394
395
396static void
397decrypt(gcry_mpi_t output, gcry_mpi_t a, gcry_mpi_t b, ELG_secret_key *skey )
398{
399 gcry_mpi_t t1 = mpi_alloc_secure( mpi_get_nlimbs( skey->p ) );
400
401 /* output = b/(a^x) mod p */
402 gcry_mpi_powm( t1, a, skey->x, skey->p );
403 mpi_invm( t1, t1, skey->p );
404 mpi_mulm( output, b, t1, skey->p );
405#if 0
406 if( DBG_CIPHER )
407 {
408 log_mpidump("elg decrypted x= ", skey->x);
409 log_mpidump("elg decrypted p= ", skey->p);
410 log_mpidump("elg decrypted a= ", a);
411 log_mpidump("elg decrypted b= ", b);
412 log_mpidump("elg decrypted M= ", output);
413 }
414#endif
415 mpi_free(t1);
416}
417
418
419/****************
420 * Make an Elgamal signature out of INPUT
421 */
422
423static void
424sign(gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input, ELG_secret_key *skey )
425{
426 gcry_mpi_t k;
427 gcry_mpi_t t = mpi_alloc( mpi_get_nlimbs(a) );
428 gcry_mpi_t inv = mpi_alloc( mpi_get_nlimbs(a) );
429 gcry_mpi_t p_1 = mpi_copy(skey->p);
430
431 /*
432 * b = (t * inv) mod (p-1)
433 * b = (t * inv(k,(p-1),(p-1)) mod (p-1)
434 * b = (((M-x*a) mod (p-1)) * inv(k,(p-1),(p-1))) mod (p-1)
435 *
436 */
437 mpi_sub_ui(p_1, p_1, 1);
438 k = gen_k( skey->p, 0 /* no small K ! */ );
439 gcry_mpi_powm( a, skey->g, k, skey->p );
440 mpi_mul(t, skey->x, a );
441 mpi_subm(t, input, t, p_1 );
442 mpi_invm(inv, k, p_1 );
443 mpi_mulm(b, t, inv, p_1 );
444
445#if 0
446 if( DBG_CIPHER )
447 {
448 log_mpidump("elg sign p= ", skey->p);
449 log_mpidump("elg sign g= ", skey->g);
450 log_mpidump("elg sign y= ", skey->y);
451 log_mpidump("elg sign x= ", skey->x);
452 log_mpidump("elg sign k= ", k);
453 log_mpidump("elg sign M= ", input);
454 log_mpidump("elg sign a= ", a);
455 log_mpidump("elg sign b= ", b);
456 }
457#endif
458 mpi_free(k);
459 mpi_free(t);
460 mpi_free(inv);
461 mpi_free(p_1);
462}
463
464
465/****************
466 * Returns true if the signature composed of A and B is valid.
467 */
468static int
469verify(gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input, ELG_public_key *pkey )
470{
471 int rc;
472 gcry_mpi_t t1;
473 gcry_mpi_t t2;
474 gcry_mpi_t base[4];
475 gcry_mpi_t ex[4];
476
477 if( !(mpi_cmp_ui( a, 0 ) > 0 && mpi_cmp( a, pkey->p ) < 0) )
478 return 0; /* assertion0 < a < p failed */
479
480 t1 = mpi_alloc( mpi_get_nlimbs(a) );
481 t2 = mpi_alloc( mpi_get_nlimbs(a) );
482
483#if 0
484 /* t1 = (y^a mod p) * (a^b mod p) mod p */
485 gcry_mpi_powm( t1, pkey->y, a, pkey->p );
486 gcry_mpi_powm( t2, a, b, pkey->p );
487 mpi_mulm( t1, t1, t2, pkey->p );
488
489 /* t2 = g ^ input mod p */
490 gcry_mpi_powm( t2, pkey->g, input, pkey->p );
491
492 rc = !mpi_cmp( t1, t2 );
493#elif 0
494 /* t1 = (y^a mod p) * (a^b mod p) mod p */
495 base[0] = pkey->y; ex[0] = a;
496 base[1] = a; ex[1] = b;
497 base[2] = NULL; ex[2] = NULL;
498 mpi_mulpowm( t1, base, ex, pkey->p );
499
500 /* t2 = g ^ input mod p */
501 gcry_mpi_powm( t2, pkey->g, input, pkey->p );
502
503 rc = !mpi_cmp( t1, t2 );
504#else
505 /* t1 = g ^ - input * y ^ a * a ^ b mod p */
506 mpi_invm(t2, pkey->g, pkey->p );
507 base[0] = t2 ; ex[0] = input;
508 base[1] = pkey->y; ex[1] = a;
509 base[2] = a; ex[2] = b;
510 base[3] = NULL; ex[3] = NULL;
511 mpi_mulpowm( t1, base, ex, pkey->p );
512 rc = !mpi_cmp_ui( t1, 1 );
513
514#endif
515
516 mpi_free(t1);
517 mpi_free(t2);
518 return rc;
519}
520
521/*********************************************
522 ************** interface ******************
523 *********************************************/
524
525gcry_err_code_t
526_gcry_elg_generate (int algo, unsigned nbits, unsigned long dummy,
527 gcry_mpi_t *skey, gcry_mpi_t **retfactors)
528{
529 ELG_secret_key sk;
530
531 generate (&sk, nbits, retfactors);
532 skey[0] = sk.p;
533 skey[1] = sk.g;
534 skey[2] = sk.y;
535 skey[3] = sk.x;
536
537 return GPG_ERR_NO_ERROR;
538}
539
540
541gcry_err_code_t
542_gcry_elg_check_secret_key (int algo, gcry_mpi_t *skey)
543{
544 gcry_err_code_t err = GPG_ERR_NO_ERROR;
545 ELG_secret_key sk;
546
547 if ((! skey[0]) || (! skey[1]) || (! skey[2]) || (! skey[3]))
548 err = GPG_ERR_BAD_MPI;
549 else
550 {
551 sk.p = skey[0];
552 sk.g = skey[1];
553 sk.y = skey[2];
554 sk.x = skey[3];
555
556 if (! check_secret_key (&sk))
557 err = GPG_ERR_BAD_SECKEY;
558 }
559
560 return err;
561}
562
563
564gcry_err_code_t
565_gcry_elg_encrypt (int algo, gcry_mpi_t *resarr,
566 gcry_mpi_t data, gcry_mpi_t *pkey, int flags)
567{
568 gcry_err_code_t err = GPG_ERR_NO_ERROR;
569 ELG_public_key pk;
570
571 if ((! data) || (! pkey[0]) || (! pkey[1]) || (! pkey[2]))
572 err = GPG_ERR_BAD_MPI;
573 else
574 {
575 pk.p = pkey[0];
576 pk.g = pkey[1];
577 pk.y = pkey[2];
578 resarr[0] = mpi_alloc (mpi_get_nlimbs (pk.p));
579 resarr[1] = mpi_alloc (mpi_get_nlimbs (pk.p));
580 do_encrypt (resarr[0], resarr[1], data, &pk);
581 }
582 return err;
583}
584
585
586gcry_err_code_t
587_gcry_elg_decrypt (int algo, gcry_mpi_t *result,
588 gcry_mpi_t *data, gcry_mpi_t *skey, int flags)
589{
590 gcry_err_code_t err = GPG_ERR_NO_ERROR;
591 ELG_secret_key sk;
592
593 if ((! data[0]) || (! data[1])
594 || (! skey[0]) || (! skey[1]) || (! skey[2]) || (! skey[3]))
595 err = GPG_ERR_BAD_MPI;
596 else
597 {
598 sk.p = skey[0];
599 sk.g = skey[1];
600 sk.y = skey[2];
601 sk.x = skey[3];
602 *result = mpi_alloc_secure (mpi_get_nlimbs (sk.p));
603 decrypt (*result, data[0], data[1], &sk);
604 }
605 return err;
606}
607
608
609gcry_err_code_t
610_gcry_elg_sign (int algo, gcry_mpi_t *resarr, gcry_mpi_t data, gcry_mpi_t *skey)
611{
612 gcry_err_code_t err = GPG_ERR_NO_ERROR;
613 ELG_secret_key sk;
614
615 if ((! data)
616 || (! skey[0]) || (! skey[1]) || (! skey[2]) || (! skey[3]))
617 err = GPG_ERR_BAD_MPI;
618 else
619 {
620 sk.p = skey[0];
621 sk.g = skey[1];
622 sk.y = skey[2];
623 sk.x = skey[3];
624 resarr[0] = mpi_alloc (mpi_get_nlimbs (sk.p));
625 resarr[1] = mpi_alloc (mpi_get_nlimbs (sk.p));
626 sign (resarr[0], resarr[1], data, &sk);
627 }
628
629 return err;
630}
631
632gcry_err_code_t
633_gcry_elg_verify (int algo, gcry_mpi_t hash, gcry_mpi_t *data, gcry_mpi_t *pkey,
634 int (*cmp) (void *, gcry_mpi_t), void *opaquev)
635{
636 gcry_err_code_t err = GPG_ERR_NO_ERROR;
637 ELG_public_key pk;
638
639 if ((! data[0]) || (! data[1]) || (! hash)
640 || (! pkey[0]) || (! pkey[1]) || (! pkey[2]))
641 err = GPG_ERR_BAD_MPI;
642 else
643 {
644 pk.p = pkey[0];
645 pk.g = pkey[1];
646 pk.y = pkey[2];
647 if (! verify (data[0], data[1], hash, &pk))
648 err = GPG_ERR_BAD_SIGNATURE;
649 }
650
651 return err;
652}
653
654
655unsigned int
656_gcry_elg_get_nbits (int algo, gcry_mpi_t *pkey)
657{
658 return mpi_get_nbits (pkey[0]);
659}
660
661static char *elg_names[] =
662 {
663 "elg",
664 "openpgp-elg",
665 "openpgp-elg-sig",
666 NULL,
667 };
668
669
670gcry_pk_spec_t _gcry_pubkey_spec_elg =
671 {
672 "ELG", elg_names,
673 "pgy", "pgyx", "ab", "rs", "pgy",
674 GCRY_PK_USAGE_SIGN | GCRY_PK_USAGE_ENCR,
675 _gcry_elg_generate,
676 _gcry_elg_check_secret_key,
677 _gcry_elg_encrypt,
678 _gcry_elg_decrypt,
679 _gcry_elg_sign,
680 _gcry_elg_verify,
681 _gcry_elg_get_nbits,
682 };
diff --git a/pwmanager/libcrypt/cipher/md.c b/pwmanager/libcrypt/cipher/md.c
new file mode 100644
index 0000000..06b46e6
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/md.c
@@ -0,0 +1,1253 @@
1/* md.c - message digest dispatcher
2 * Copyright (C) 1998, 1999, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <config.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <errno.h>
26#include <assert.h>
27
28#include "g10lib.h"
29#include "cipher.h"
30#include "ath.h"
31
32#include "rmd.h"
33
34static struct digest_table_entry
35{
36 gcry_md_spec_t *digest;
37 unsigned int algorithm;
38} digest_table[] =
39 {
40#if USE_CRC
41 { &_gcry_digest_spec_crc32, GCRY_MD_CRC32 },
42 { &_gcry_digest_spec_crc32_rfc1510, GCRY_MD_CRC32_RFC1510 },
43 { &_gcry_digest_spec_crc24_rfc2440, GCRY_MD_CRC24_RFC2440 },
44#endif
45#if USE_MD4
46 { &_gcry_digest_spec_md4, GCRY_MD_MD4 },
47#endif
48#if USE_MD5
49 { &_gcry_digest_spec_md5, GCRY_MD_MD5 },
50#endif
51#if USE_RMD160
52 { &_gcry_digest_spec_rmd160, GCRY_MD_RMD160 },
53#endif
54#if USE_SHA1
55 { &_gcry_digest_spec_sha1, GCRY_MD_SHA1 },
56#endif
57#if USE_SHA256
58 { &_gcry_digest_spec_sha256, GCRY_MD_SHA256 },
59#endif
60#if USE_SHA512
61 { &_gcry_digest_spec_sha512, GCRY_MD_SHA512 },
62 { &_gcry_digest_spec_sha384, GCRY_MD_SHA384 },
63#endif
64#if USE_TIGER
65 { &_gcry_digest_spec_tiger, GCRY_MD_TIGER },
66#endif
67 { NULL },
68 };
69
70/* List of registered digests. */
71static gcry_module_t digests_registered;
72
73/* This is the lock protecting DIGESTS_REGISTERED. */
74static ath_mutex_t digests_registered_lock = ATH_MUTEX_INITIALIZER;
75
76/* Flag to check wether the default ciphers have already been
77 registered. */
78static int default_digests_registered;
79
80typedef struct gcry_md_list
81{
82 gcry_md_spec_t *digest;
83 gcry_module_t module;
84 struct gcry_md_list *next;
85 size_t actual_struct_size; /* Allocated size of this structure. */
86 PROPERLY_ALIGNED_TYPE context;
87} GcryDigestEntry;
88
89/* this structure is put right after the gcry_md_hd_t buffer, so that
90 * only one memory block is needed. */
91struct gcry_md_context
92{
93 int magic;
94 size_t actual_handle_size; /* Allocated size of this handle. */
95 int secure;
96 FILE *debug;
97 int finalized;
98 GcryDigestEntry *list;
99 byte *macpads;
100};
101
102
103#define CTX_MAGIC_NORMAL 0x11071961
104#define CTX_MAGIC_SECURE 0x16917011
105
106/* Convenient macro for registering the default digests. */
107#define REGISTER_DEFAULT_DIGESTS \
108 do \
109 { \
110 ath_mutex_lock (&digests_registered_lock); \
111 if (! default_digests_registered) \
112 { \
113 gcry_md_register_default (); \
114 default_digests_registered = 1; \
115 } \
116 ath_mutex_unlock (&digests_registered_lock); \
117 } \
118 while (0)
119
120
121static const char * digest_algo_to_string( int algo );
122static gcry_err_code_t check_digest_algo (int algo);
123static gcry_err_code_t md_open (gcry_md_hd_t *h, int algo,
124 int secure, int hmac);
125static gcry_err_code_t md_enable (gcry_md_hd_t hd, int algo);
126static gcry_err_code_t md_copy (gcry_md_hd_t a, gcry_md_hd_t *b);
127static void md_close (gcry_md_hd_t a);
128static void md_write (gcry_md_hd_t a, byte *inbuf, size_t inlen);
129static void md_final(gcry_md_hd_t a);
130static byte *md_read( gcry_md_hd_t a, int algo );
131static int md_get_algo( gcry_md_hd_t a );
132static int md_digest_length( int algo );
133static const byte *md_asn_oid( int algo, size_t *asnlen, size_t *mdlen );
134static void md_start_debug( gcry_md_hd_t a, char *suffix );
135static void md_stop_debug( gcry_md_hd_t a );
136
137
138
139
140/* Internal function. Register all the ciphers included in
141 CIPHER_TABLE. Returns zero on success or an error code. */
142static void
143gcry_md_register_default (void)
144{
145 gcry_err_code_t err = 0;
146 int i;
147
148 for (i = 0; (! err) && digest_table[i].digest; i++)
149 err = _gcry_module_add (&digests_registered,
150 digest_table[i].algorithm,
151 (void *) digest_table[i].digest,
152 NULL);
153
154 if (err)
155 BUG ();
156}
157
158/* Internal callback function. */
159static int
160gcry_md_lookup_func_name (void *spec, void *data)
161{
162 gcry_md_spec_t *digest = (gcry_md_spec_t *) spec;
163 char *name = (char *) data;
164
165 return (! stricmp (digest->name, name));
166}
167
168/* Internal callback function. Used via _gcry_module_lookup. */
169static int
170gcry_md_lookup_func_oid (void *spec, void *data)
171{
172 gcry_md_spec_t *digest = (gcry_md_spec_t *) spec;
173 char *oid = (char *) data;
174 gcry_md_oid_spec_t *oid_specs = digest->oids;
175 int ret = 0, i;
176
177 if (oid_specs)
178 {
179 for (i = 0; oid_specs[i].oidstring && (! ret); i++)
180 if (! stricmp (oid, oid_specs[i].oidstring))
181 ret = 1;
182 }
183
184 return ret;
185}
186
187/* Internal function. Lookup a digest entry by it's name. */
188static gcry_module_t
189gcry_md_lookup_name (const char *name)
190{
191 gcry_module_t digest;
192
193 digest = _gcry_module_lookup (digests_registered, (void *) name,
194 gcry_md_lookup_func_name);
195
196 return digest;
197}
198
199/* Internal function. Lookup a cipher entry by it's oid. */
200static gcry_module_t
201gcry_md_lookup_oid (const char *oid)
202{
203 gcry_module_t digest;
204
205 digest = _gcry_module_lookup (digests_registered, (void *) oid,
206 gcry_md_lookup_func_oid);
207
208 return digest;
209}
210
211/* Register a new digest module whose specification can be found in
212 DIGEST. On success, a new algorithm ID is stored in ALGORITHM_ID
213 and a pointer representhing this module is stored in MODULE. */
214gcry_error_t
215gcry_md_register (gcry_md_spec_t *digest,
216 unsigned int *algorithm_id,
217 gcry_module_t *module)
218{
219 gcry_err_code_t err = 0;
220 gcry_module_t mod;
221
222 ath_mutex_lock (&digests_registered_lock);
223 err = _gcry_module_add (&digests_registered, 0,
224 (void *) digest, &mod);
225 ath_mutex_unlock (&digests_registered_lock);
226
227 if (! err)
228 {
229 *module = mod;
230 *algorithm_id = mod->mod_id;
231 }
232
233 return gcry_error (err);
234}
235
236/* Unregister the digest identified by ID, which must have been
237 registered with gcry_digest_register. */
238void
239gcry_md_unregister (gcry_module_t module)
240{
241 ath_mutex_lock (&digests_registered_lock);
242 _gcry_module_release (module);
243 ath_mutex_unlock (&digests_registered_lock);
244}
245
246
247static int
248search_oid (const char *oid, int *algorithm, gcry_md_oid_spec_t *oid_spec)
249{
250 gcry_module_t module;
251 int ret = 0;
252
253 if (oid && ((! strncmp (oid, "oid.", 4))
254 || (! strncmp (oid, "OID.", 4))))
255 oid += 4;
256
257 module = gcry_md_lookup_oid (oid);
258 if (module)
259 {
260 gcry_md_spec_t *digest = module->spec;
261 int i;
262
263 for (i = 0; digest->oids[i].oidstring && !ret; i++)
264 if (! stricmp (oid, digest->oids[i].oidstring))
265 {
266 if (algorithm)
267 *algorithm = module->mod_id;
268 if (oid_spec)
269 *oid_spec = digest->oids[i];
270 ret = 1;
271 }
272 _gcry_module_release (module);
273 }
274
275 return ret;
276}
277
278/****************
279 * Map a string to the digest algo
280 */
281int
282gcry_md_map_name (const char *string)
283{
284 gcry_module_t digest;
285 int ret, algorithm = 0;
286
287 if (! string)
288 return 0;
289
290 REGISTER_DEFAULT_DIGESTS;
291
292 /* If the string starts with a digit (optionally prefixed with
293 either "OID." or "oid."), we first look into our table of ASN.1
294 object identifiers to figure out the algorithm */
295
296 ath_mutex_lock (&digests_registered_lock);
297
298 ret = search_oid (string, &algorithm, NULL);
299 if (! ret)
300 {
301 /* Not found, search for an acording diget name. */
302 digest = gcry_md_lookup_name (string);
303 if (digest)
304 {
305 algorithm = digest->mod_id;
306 _gcry_module_release (digest);
307 }
308 }
309 ath_mutex_unlock (&digests_registered_lock);
310
311 return algorithm;
312}
313
314
315/****************
316 * Map a digest algo to a string
317 */
318static const char *
319digest_algo_to_string (int algorithm)
320{
321 const char *name = NULL;
322 gcry_module_t digest;
323
324 REGISTER_DEFAULT_DIGESTS;
325
326 ath_mutex_lock (&digests_registered_lock);
327 digest = _gcry_module_lookup_id (digests_registered, algorithm);
328 if (digest)
329 {
330 name = ((gcry_md_spec_t *) digest->spec)->name;
331 _gcry_module_release (digest);
332 }
333 ath_mutex_unlock (&digests_registered_lock);
334
335 return name;
336}
337
338/****************
339 * This function simply returns the name of the algorithm or some constant
340 * string when there is no algo. It will never return NULL.
341 * Usethe macro gcry_md_test_algo() to check whether the algorithm
342 * is valid.
343 */
344const char *
345gcry_md_algo_name (int algorithm)
346{
347 const char *s = digest_algo_to_string (algorithm);
348 return s ? s : "?";
349}
350
351
352static gcry_err_code_t
353check_digest_algo (int algorithm)
354{
355 gcry_err_code_t rc = 0;
356 gcry_module_t digest;
357
358 REGISTER_DEFAULT_DIGESTS;
359
360 ath_mutex_lock (&digests_registered_lock);
361 digest = _gcry_module_lookup_id (digests_registered, algorithm);
362 if (digest)
363 _gcry_module_release (digest);
364 else
365 rc = GPG_ERR_DIGEST_ALGO;
366 ath_mutex_unlock (&digests_registered_lock);
367
368 return rc;
369}
370
371
372
373/****************
374 * Open a message digest handle for use with algorithm ALGO.
375 * More algorithms may be added by md_enable(). The initial algorithm
376 * may be 0.
377 */
378static gcry_err_code_t
379md_open (gcry_md_hd_t *h, int algo, int secure, int hmac)
380{
381 gcry_err_code_t err = GPG_ERR_NO_ERROR;
382 int bufsize = secure ? 512 : 1024;
383 struct gcry_md_context *ctx;
384 gcry_md_hd_t hd;
385 size_t n;
386
387 /* Allocate a memory area to hold the caller visible buffer with it's
388 * control information and the data required by this module. Set the
389 * context pointer at the beginning to this area.
390 * We have to use this strange scheme because we want to hide the
391 * internal data but have a variable sized buffer.
392 *
393 *+---+------+---........------+-------------+
394 * !ctx! bctl ! buffer ! private !
395 *+---+------+---........------+-------------+
396 * ! ^
397 * !---------------------------!
398 *
399 * We have to make sure that private is well aligned.
400 */
401 n = sizeof (struct gcry_md_handle) + bufsize;
402 n = ((n + sizeof (PROPERLY_ALIGNED_TYPE) - 1)
403 / sizeof (PROPERLY_ALIGNED_TYPE)) * sizeof (PROPERLY_ALIGNED_TYPE);
404
405 /* Allocate and set the Context pointer to the private data */
406 if (secure)
407 hd = gcry_malloc_secure (n + sizeof (struct gcry_md_context));
408 else
409 hd = gcry_malloc (n + sizeof (struct gcry_md_context));
410
411 if (! hd)
412 err = gpg_err_code_from_errno (errno);
413
414 if (! err)
415 {
416 hd->ctx = ctx = (struct gcry_md_context *) ((char *) hd + n);
417 /* Setup the globally visible data (bctl in the diagram).*/
418 hd->bufsize = n - sizeof (struct gcry_md_handle) + 1;
419 hd->bufpos = 0;
420
421 /* Initialize the private data. */
422 memset (hd->ctx, 0, sizeof *hd->ctx);
423 ctx->magic = secure ? CTX_MAGIC_SECURE : CTX_MAGIC_NORMAL;
424 ctx->actual_handle_size = n + sizeof (struct gcry_md_context);
425 ctx->secure = secure;
426
427 if (hmac)
428 {
429 ctx->macpads = gcry_malloc_secure (128);
430 if (! ctx->macpads)
431 {
432 md_close (hd);
433 err = gpg_err_code_from_errno (errno);
434 }
435 }
436 }
437
438 if (! err)
439 {
440 /* FIXME: should we really do that? - yes [-wk] */
441 _gcry_fast_random_poll ();
442
443 if (algo)
444 {
445 err = md_enable (hd, algo);
446 if (err)
447 md_close (hd);
448 }
449 }
450
451 if (! err)
452 *h = hd;
453
454 return err;
455}
456
457/* Create a message digest object for algorithm ALGO. FLAGS may be
458 given as an bitwise OR of the gcry_md_flags values. ALGO may be
459 given as 0 if the algorithms to be used are later set using
460 gcry_md_enable. H is guaranteed to be a valid handle or NULL on
461 error. */
462gcry_error_t
463gcry_md_open (gcry_md_hd_t *h, int algo, unsigned int flags)
464{
465 gcry_err_code_t err = GPG_ERR_NO_ERROR;
466 gcry_md_hd_t hd;
467
468 if ((flags & ~(GCRY_MD_FLAG_SECURE | GCRY_MD_FLAG_HMAC)))
469 err = GPG_ERR_INV_ARG;
470 else
471 {
472 err = md_open (&hd, algo, (flags & GCRY_MD_FLAG_SECURE),
473 (flags & GCRY_MD_FLAG_HMAC));
474 }
475
476 *h = err? NULL : hd;
477 return gcry_error (err);
478}
479
480
481
482static gcry_err_code_t
483md_enable (gcry_md_hd_t hd, int algorithm)
484{
485 struct gcry_md_context *h = hd->ctx;
486 gcry_md_spec_t *digest = NULL;
487 GcryDigestEntry *entry;
488 gcry_module_t module;
489 gcry_err_code_t err = 0;
490
491 for (entry = h->list; entry; entry = entry->next)
492 if (entry->module->mod_id == algorithm)
493 return err; /* already enabled */
494
495 REGISTER_DEFAULT_DIGESTS;
496
497 ath_mutex_lock (&digests_registered_lock);
498 module = _gcry_module_lookup_id (digests_registered, algorithm);
499 ath_mutex_unlock (&digests_registered_lock);
500 if (! module)
501 {
502 log_debug ("md_enable: algorithm %d not available\n", algorithm);
503 err = GPG_ERR_DIGEST_ALGO;
504 }
505 else
506 digest = (gcry_md_spec_t *) module->spec;
507
508 if (! err)
509 {
510 size_t size = (sizeof (*entry)
511 + digest->contextsize
512 - sizeof (entry->context));
513
514 /* And allocate a new list entry. */
515 if (h->secure)
516 entry = gcry_malloc_secure (size);
517 else
518 entry = gcry_malloc (size);
519
520 if (! entry)
521 err = gpg_err_code_from_errno (errno);
522 else
523 {
524 entry->digest = digest;
525 entry->module = module;
526 entry->next = h->list;
527 entry->actual_struct_size = size;
528 h->list = entry;
529
530 /* And init this instance. */
531 entry->digest->init (&entry->context.c);
532 }
533 }
534
535 if (err)
536 {
537 if (module)
538 {
539 ath_mutex_lock (&digests_registered_lock);
540 _gcry_module_release (module);
541 ath_mutex_unlock (&digests_registered_lock);
542 }
543 }
544
545 return err;
546}
547
548
549gcry_error_t
550gcry_md_enable (gcry_md_hd_t hd, int algorithm)
551{
552 gcry_err_code_t err = md_enable (hd, algorithm);
553 return gcry_error (err);
554}
555
556static gcry_err_code_t
557md_copy (gcry_md_hd_t ahd, gcry_md_hd_t *b_hd)
558{
559 gcry_err_code_t err = GPG_ERR_NO_ERROR;
560 struct gcry_md_context *a = ahd->ctx;
561 struct gcry_md_context *b;
562 GcryDigestEntry *ar, *br;
563 gcry_md_hd_t bhd;
564 size_t n;
565
566 if (ahd->bufpos)
567 md_write (ahd, NULL, 0);
568
569 n = (char *) ahd->ctx - (char *) ahd;
570 if (a->secure)
571 bhd = gcry_malloc_secure (n + sizeof (struct gcry_md_context));
572 else
573 bhd = gcry_malloc (n + sizeof (struct gcry_md_context));
574
575 if (! bhd)
576 err = gpg_err_code_from_errno (errno);
577
578 if (! err)
579 {
580 bhd->ctx = b = (struct gcry_md_context *) ((char *) bhd + n);
581 /* No need to copy the buffer due to the write above. */
582 assert (ahd->bufsize == (n - sizeof (struct gcry_md_handle) + 1));
583 bhd->bufsize = ahd->bufsize;
584 bhd->bufpos = 0;
585 assert (! ahd->bufpos);
586 memcpy (b, a, sizeof *a);
587 b->list = NULL;
588 b->debug = NULL;
589 if (a->macpads)
590 {
591 b->macpads = gcry_malloc_secure (128);
592 if (! b->macpads)
593 {
594 md_close (bhd);
595 err = gpg_err_code_from_errno (errno);
596 }
597 else
598 memcpy (b->macpads, a->macpads, 128);
599 }
600 }
601
602 /* Copy the complete list of algorithms. The copied list is
603 reversed, but that doesn't matter. */
604 if (! err)
605 for (ar = a->list; ar; ar = ar->next)
606 {
607 if (a->secure)
608 br = gcry_xmalloc_secure (sizeof *br
609 + ar->digest->contextsize
610 - sizeof(ar->context));
611 else
612 br = gcry_xmalloc (sizeof *br
613 + ar->digest->contextsize
614 - sizeof (ar->context));
615 memcpy (br, ar,
616 sizeof (*br) + ar->digest->contextsize - sizeof (ar->context));
617 br->next = b->list;
618 b->list = br;
619
620 /* Add a reference to the module. */
621 ath_mutex_lock (&digests_registered_lock);
622 _gcry_module_use (br->module);
623 ath_mutex_unlock (&digests_registered_lock);
624 }
625
626 if (a->debug)
627 md_start_debug (bhd, "unknown");
628
629 if (! err)
630 *b_hd = bhd;
631
632 return err;
633}
634
635gcry_error_t
636gcry_md_copy (gcry_md_hd_t *handle, gcry_md_hd_t hd)
637{
638 gcry_err_code_t err = md_copy (hd, handle);
639 if (err)
640 *handle = NULL;
641 return gcry_error (err);
642}
643
644/*
645 * Reset all contexts and discard any buffered stuff. This may be used
646 * instead of a md_close(); md_open().
647 */
648void
649gcry_md_reset (gcry_md_hd_t a)
650{
651 GcryDigestEntry *r;
652
653 a->bufpos = a->ctx->finalized = 0;
654
655 for (r = a->ctx->list; r; r = r->next)
656 {
657 memset (r->context.c, 0, r->digest->contextsize);
658 (*r->digest->init) (&r->context.c);
659 }
660 if (a->ctx->macpads)
661 md_write (a, a->ctx->macpads, 64); /* inner pad */
662}
663
664static void
665md_close (gcry_md_hd_t a)
666{
667 GcryDigestEntry *r, *r2;
668
669 if (! a)
670 return;
671 if (a->ctx->debug)
672 md_stop_debug (a);
673 for (r = a->ctx->list; r; r = r2)
674 {
675 r2 = r->next;
676 ath_mutex_lock (&digests_registered_lock);
677 _gcry_module_release (r->module);
678 ath_mutex_unlock (&digests_registered_lock);
679 wipememory (r, r->actual_struct_size);
680 gcry_free (r);
681 }
682
683 if (a->ctx->macpads)
684 {
685 wipememory (a->ctx->macpads, 128);
686 gcry_free(a->ctx->macpads);
687 }
688
689 wipememory (a, a->ctx->actual_handle_size);
690 gcry_free(a);
691}
692
693void
694gcry_md_close (gcry_md_hd_t hd)
695{
696 md_close (hd);
697}
698
699static void
700md_write (gcry_md_hd_t a, byte *inbuf, size_t inlen)
701{
702 GcryDigestEntry *r;
703
704 if (a->ctx->debug)
705 {
706 if (a->bufpos && fwrite (a->buf, a->bufpos, 1, a->ctx->debug) != 1)
707 BUG();
708 if (inlen && fwrite (inbuf, inlen, 1, a->ctx->debug) != 1)
709 BUG();
710 }
711
712 for (r = a->ctx->list; r; r = r->next)
713 {
714 if (a->bufpos)
715 (*r->digest->write) (&r->context.c, a->buf, a->bufpos);
716 (*r->digest->write) (&r->context.c, inbuf, inlen);
717 }
718 a->bufpos = 0;
719}
720
721void
722gcry_md_write (gcry_md_hd_t hd, const void *inbuf, size_t inlen)
723{
724 md_write (hd, (unsigned char *) inbuf, inlen);
725}
726
727static void
728md_final (gcry_md_hd_t a)
729{
730 GcryDigestEntry *r;
731
732 if (a->ctx->finalized)
733 return;
734
735 if (a->bufpos)
736 md_write (a, NULL, 0);
737
738 for (r = a->ctx->list; r; r = r->next)
739 (*r->digest->final) (&r->context.c);
740
741 a->ctx->finalized = 1;
742
743 if (a->ctx->macpads)
744 {
745 /* Finish the hmac. */
746 int algo = md_get_algo (a);
747 byte *p = md_read (a, algo);
748 size_t dlen = md_digest_length (algo);
749 gcry_md_hd_t om;
750 gcry_err_code_t err = md_open (&om, algo, a->ctx->secure, 0);
751
752 if (err)
753 _gcry_fatal_error (err, NULL);
754 md_write (om, a->ctx->macpads+64, 64);
755 md_write (om, p, dlen);
756 md_final (om);
757 /* Replace our digest with the mac (they have the same size). */
758 memcpy (p, md_read (om, algo), dlen);
759 md_close (om);
760 }
761}
762
763static gcry_err_code_t
764prepare_macpads( gcry_md_hd_t hd, const byte *key, size_t keylen)
765{
766 int i;
767 int algo = md_get_algo( hd );
768 byte *helpkey = NULL;
769 byte *ipad, *opad;
770
771 if ( !algo )
772 return GPG_ERR_DIGEST_ALGO; /* i.e. no algo enabled */
773
774 if ( keylen > 64 )
775 {
776 helpkey = gcry_malloc_secure ( md_digest_length( algo ) );
777 if ( !helpkey )
778 return gpg_err_code_from_errno (errno);
779 gcry_md_hash_buffer ( algo, helpkey, key, keylen );
780 key = helpkey;
781 keylen = md_digest_length( algo );
782 assert ( keylen <= 64 );
783 }
784
785 memset ( hd->ctx->macpads, 0, 128 );
786 ipad = hd->ctx->macpads;
787 opad = hd->ctx->macpads+64;
788 memcpy ( ipad, key, keylen );
789 memcpy ( opad, key, keylen );
790 for (i=0; i < 64; i++ )
791 {
792 ipad[i] ^= 0x36;
793 opad[i] ^= 0x5c;
794 }
795 gcry_free( helpkey );
796
797 return GPG_ERR_NO_ERROR;
798}
799
800gcry_error_t
801gcry_md_ctl (gcry_md_hd_t hd, int cmd, byte *buffer, size_t buflen)
802{
803 gcry_err_code_t rc = 0;
804
805 switch (cmd)
806 {
807 case GCRYCTL_FINALIZE:
808 md_final (hd);
809 break;
810 case GCRYCTL_SET_KEY:
811 rc = gcry_err_code (gcry_md_setkey (hd, buffer, buflen));
812 break;
813 case GCRYCTL_START_DUMP:
814 md_start_debug (hd, buffer);
815 break;
816 case GCRYCTL_STOP_DUMP:
817 md_stop_debug( hd );
818 break;
819 default:
820 rc = GPG_ERR_INV_OP;
821 }
822 return gcry_error (rc);
823}
824
825gcry_error_t
826gcry_md_setkey (gcry_md_hd_t hd, const void *key, size_t keylen)
827{
828 gcry_err_code_t rc = GPG_ERR_NO_ERROR;
829
830 if (! hd->ctx->macpads)
831 rc = GPG_ERR_CONFLICT;
832 else
833 {
834 rc = prepare_macpads (hd, key, keylen);
835 if (! rc)
836 gcry_md_reset (hd);
837 }
838
839 return gcry_error (rc);
840}
841
842
843/****************
844 * if ALGO is null get the digest for the used algo (which should be only one)
845 */
846static byte *
847md_read( gcry_md_hd_t a, int algo )
848{
849 GcryDigestEntry *r = a->ctx->list;
850
851 if (! algo)
852 {
853 /* return the first algorithm */
854 if (r && r->next)
855 log_debug("more than algorithm in md_read(0)\n");
856 return r->digest->read( &r->context.c );
857 }
858 else
859 {
860 for (r = a->ctx->list; r; r = r->next)
861 if (r->module->mod_id == algo)
862 return r->digest->read (&r->context.c);
863 }
864 BUG();
865 return NULL;
866}
867
868/*
869 * Read out the complete digest, this function implictly finalizes
870 * the hash.
871 */
872byte *
873gcry_md_read (gcry_md_hd_t hd, int algo)
874{
875 gcry_md_ctl (hd, GCRYCTL_FINALIZE, NULL, 0);
876 return md_read (hd, algo);
877}
878
879/****************
880 * This function combines md_final and md_read but keeps the context
881 * intact. This function can be used to calculate intermediate
882 * digests. The digest is copied into buffer and the digestlength is
883 * returned. If buffer is NULL only the needed size for buffer is returned.
884 * buflen gives the max size of buffer. If the buffer is too shourt to
885 * hold the complete digest, the buffer is filled with as many bytes are
886 * possible and this value is returned.
887 */
888#if 0
889static int
890md_digest( gcry_md_hd_t a, int algo, byte *buffer, int buflen )
891{
892 struct md_digest_list_s *r = NULL;
893 char *context;
894 char *digest;
895
896 if( a->bufpos )
897 md_write( a, NULL, 0 );
898
899 if( !algo ) { /* return digest for the first algorithm */
900 if( (r=a->ctx->list) && r->next )
901 log_debug("more than algorithm in md_digest(0)\n");
902 }
903 else {
904 for(r=a->ctx->list; r; r = r->next )
905 if( r->algo == algo )
906 break;
907 }
908 if( !r )
909 BUG();
910
911 if( !buffer )
912 return r->mdlen;
913
914 /* I don't want to change the interface, so I simply work on a copy
915 * of the context (extra overhead - should be fixed)*/
916 context = a->ctx->secure ? gcry_xmalloc_secure( r->contextsize )
917 : gcry_xmalloc( r->contextsize );
918 memcpy( context, r->context.c, r->contextsize );
919 (*r->digest->final)( context );
920 digest = (*r->digest->read)( context );
921
922 if( buflen > r->mdlen )
923 buflen = r->mdlen;
924 memcpy( buffer, digest, buflen );
925
926 gcry_free(context);
927 return buflen;
928}
929#endif
930
931/*
932 * Read out an intermediate digest. Not yet fucntional.
933 */
934gcry_err_code_t
935gcry_md_get (gcry_md_hd_t hd, int algo, byte *buffer, int buflen)
936{
937 /*md_digest ... */
938 return GPG_ERR_INTERNAL;
939}
940
941
942/*
943 * Shortcut function to hash a buffer with a given algo. The only
944 * guaranteed supported algorithms are RIPE-MD160 and SHA-1. The
945 * supplied digest buffer must be large enough to store the resulting
946 * hash. No error is returned, the function will abort on an invalid
947 * algo. DISABLED_ALGOS are ignored here. */
948void
949gcry_md_hash_buffer (int algo, void *digest,
950 const void *buffer, size_t length)
951{
952 if (algo == GCRY_MD_SHA1)
953 _gcry_sha1_hash_buffer (digest, buffer, length);
954 else if (algo == GCRY_MD_RMD160)
955 _gcry_rmd160_hash_buffer (digest, buffer, length);
956 else
957 {
958 /* For the others we do not have a fast function, so we use the
959 normal functions. */
960 gcry_md_hd_t h;
961 gpg_err_code_t err = md_open (&h, algo, 0, 0);
962 if (err)
963 log_bug ("gcry_md_open failed for algo %d: %s",
964 algo, gpg_strerror (gcry_error(err)));
965 md_write (h, (byte *) buffer, length);
966 md_final (h);
967 memcpy (digest, md_read (h, algo), md_digest_length (algo));
968 md_close (h);
969 }
970}
971
972static int
973md_get_algo (gcry_md_hd_t a)
974{
975 GcryDigestEntry *r = a->ctx->list;
976
977 if (r && r->next)
978 log_error("WARNING: more than algorithm in md_get_algo()\n");
979 return r ? r->module->mod_id : 0;
980}
981
982int
983gcry_md_get_algo (gcry_md_hd_t hd)
984{
985 return md_get_algo (hd);
986}
987
988
989/****************
990 * Return the length of the digest
991 */
992static int
993md_digest_length (int algorithm)
994{
995 gcry_module_t digest;
996 int mdlen = 0;
997
998 REGISTER_DEFAULT_DIGESTS;
999
1000 ath_mutex_lock (&digests_registered_lock);
1001 digest = _gcry_module_lookup_id (digests_registered, algorithm);
1002 if (digest)
1003 {
1004 mdlen = ((gcry_md_spec_t *) digest->spec)->mdlen;
1005 _gcry_module_release (digest);
1006 }
1007 ath_mutex_unlock (&digests_registered_lock);
1008
1009 return mdlen;
1010}
1011
1012/****************
1013 * Return the length of the digest in bytes.
1014 * This function will return 0 in case of errors.
1015 */
1016unsigned int
1017gcry_md_get_algo_dlen (int algorithm)
1018{
1019 return md_digest_length (algorithm);
1020}
1021
1022
1023/* Hmmm: add a mode to enumerate the OIDs
1024 *to make g10/sig-check.c more portable */
1025static const byte *
1026md_asn_oid (int algorithm, size_t *asnlen, size_t *mdlen)
1027{
1028 const byte *asnoid = NULL;
1029 gcry_module_t digest;
1030
1031 REGISTER_DEFAULT_DIGESTS;
1032
1033 ath_mutex_lock (&digests_registered_lock);
1034 digest = _gcry_module_lookup_id (digests_registered, algorithm);
1035 if (digest)
1036 {
1037 if (asnlen)
1038 *asnlen = ((gcry_md_spec_t *) digest->spec)->asnlen;
1039 if (mdlen)
1040 *mdlen = ((gcry_md_spec_t *) digest->spec)->mdlen;
1041 asnoid = ((gcry_md_spec_t *) digest->spec)->asnoid;
1042 _gcry_module_release (digest);
1043 }
1044 else
1045 log_bug ("no ASN.1 OID for md algo %d\n", algorithm);
1046 ath_mutex_unlock (&digests_registered_lock);
1047
1048 return asnoid;
1049}
1050
1051
1052
1053/****************
1054 * Return information about the given cipher algorithm
1055 * WHAT select the kind of information returned:
1056 * GCRYCTL_TEST_ALGO:
1057 *Returns 0 when the specified algorithm is available for use.
1058 *buffer and nbytes must be zero.
1059 * GCRYCTL_GET_ASNOID:
1060 *Return the ASNOID of the algorithm in buffer. if buffer is NULL, only
1061 *the required length is returned.
1062 *
1063 * Note: Because this function is in most cases used to return an
1064 * integer value, we can make it easier for the caller to just look at
1065 * the return value. The caller will in all cases consult the value
1066 * and thereby detecting whether a error occured or not (i.e. while checking
1067 * the block size)
1068 */
1069gcry_error_t
1070gcry_md_algo_info (int algo, int what, void *buffer, size_t *nbytes)
1071{
1072 gcry_err_code_t err = GPG_ERR_NO_ERROR;
1073
1074 switch (what)
1075 {
1076 case GCRYCTL_TEST_ALGO:
1077 if (buffer || nbytes)
1078 err = GPG_ERR_INV_ARG;
1079 else
1080 err = check_digest_algo (algo);
1081 break;
1082
1083 case GCRYCTL_GET_ASNOID:
1084 {
1085 const char unsigned *asn;
1086 size_t asnlen;
1087
1088 asn = md_asn_oid (algo, &asnlen, NULL);
1089 if (buffer && (*nbytes >= asnlen))
1090 {
1091 memcpy (buffer, asn, asnlen);
1092 *nbytes = asnlen;
1093 }
1094 else if ((! buffer) && nbytes)
1095 *nbytes = asnlen;
1096 else
1097 {
1098 if (buffer)
1099 err = GPG_ERR_TOO_SHORT;
1100 else
1101 err = GPG_ERR_INV_ARG;
1102 }
1103 break;
1104 }
1105
1106 default:
1107 err = GPG_ERR_INV_OP;
1108 }
1109
1110 return gcry_error (err);
1111}
1112
1113
1114static void
1115md_start_debug( gcry_md_hd_t md, char *suffix )
1116{
1117 static int idx=0;
1118 char buf[50];
1119
1120 if( md->ctx->debug ) {
1121 log_debug("Oops: md debug already started\n");
1122 return;
1123 }
1124 idx++;
1125 sprintf(buf, "dbgmd-%05d.%.10s", idx, suffix );
1126 md->ctx->debug = fopen(buf, "w");
1127 if( !md->ctx->debug )
1128 log_debug("md debug: can't open %s\n", buf );
1129}
1130
1131static void
1132md_stop_debug( gcry_md_hd_t md )
1133{
1134 if( md->ctx->debug ) {
1135 if( md->bufpos )
1136 md_write( md, NULL, 0 );
1137 fclose(md->ctx->debug);
1138 md->ctx->debug = NULL;
1139 }
1140#ifdef HAVE_U64_TYPEDEF
1141 { /* a kludge to pull in the __muldi3 for Solaris */
1142 volatile u32 a = (u32)(ulong)md;
1143 volatile u64 b = 42;
1144 volatile u64 c;
1145 c = a * b;
1146 }
1147#endif
1148}
1149
1150
1151
1152/*
1153 * Return information about the digest handle.
1154 * GCRYCTL_IS_SECURE:
1155 *Returns 1 when the handle works on secured memory
1156 *otherwise 0 is returned. There is no error return.
1157 * GCRYCTL_IS_ALGO_ENABLED:
1158 * Returns 1 if the algo is enanled for that handle.
1159 * The algo must be passed as the address of an int.
1160 */
1161gcry_error_t
1162gcry_md_info (gcry_md_hd_t h, int cmd, void *buffer, size_t *nbytes)
1163{
1164 gcry_err_code_t err = GPG_ERR_NO_ERROR;
1165
1166 switch (cmd)
1167 {
1168 case GCRYCTL_IS_SECURE:
1169 *nbytes = h->ctx->secure;
1170 break;
1171
1172 case GCRYCTL_IS_ALGO_ENABLED:
1173 {
1174 GcryDigestEntry *r;
1175 int algo;
1176
1177 if ( !buffer || (nbytes && (*nbytes != sizeof (int))))
1178 err = GPG_ERR_INV_ARG;
1179 else
1180 {
1181 algo = *(int*)buffer;
1182
1183 *nbytes = 0;
1184 for(r=h->ctx->list; r; r = r->next ) {
1185 if (r->module->mod_id == algo)
1186 {
1187 *nbytes = 1;
1188 break;
1189 }
1190 }
1191 }
1192 break;
1193 }
1194
1195 default:
1196 err = GPG_ERR_INV_OP;
1197 }
1198
1199 return gcry_error (err);
1200}
1201
1202gcry_err_code_t
1203_gcry_md_init (void)
1204{
1205 gcry_err_code_t err = GPG_ERR_NO_ERROR;
1206
1207 REGISTER_DEFAULT_DIGESTS;
1208
1209 return err;
1210}
1211
1212
1213int
1214gcry_md_is_secure (gcry_md_hd_t a)
1215{
1216 size_t value;
1217
1218 if (gcry_md_info (a, GCRYCTL_IS_SECURE, NULL, &value))
1219 value = 1; /* It seems to be better to assume secure memory on
1220 error. */
1221 return value;
1222}
1223
1224
1225int
1226gcry_md_is_enabled (gcry_md_hd_t a, int algo)
1227{
1228 size_t value;
1229
1230 value = sizeof algo;
1231 if (gcry_md_info (a, GCRYCTL_IS_ALGO_ENABLED, &algo, &value))
1232 value = 0;
1233 return value;
1234}
1235
1236/* Get a list consisting of the IDs of the loaded message digest
1237 modules. If LIST is zero, write the number of loaded message
1238 digest modules to LIST_LENGTH and return. If LIST is non-zero, the
1239 first *LIST_LENGTH algorithm IDs are stored in LIST, which must be
1240 of according size. In case there are less message digest modules
1241 than *LIST_LENGTH, *LIST_LENGTH is updated to the correct
1242 number. */
1243gcry_error_t
1244gcry_md_list (int *list, int *list_length)
1245{
1246 gcry_err_code_t err = GPG_ERR_NO_ERROR;
1247
1248 ath_mutex_lock (&digests_registered_lock);
1249 err = _gcry_module_list (digests_registered, list, list_length);
1250 ath_mutex_unlock (&digests_registered_lock);
1251
1252 return err;
1253}
diff --git a/pwmanager/libcrypt/cipher/md4.c b/pwmanager/libcrypt/cipher/md4.c
new file mode 100644
index 0000000..0d0a559
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/md4.c
@@ -0,0 +1,327 @@
1/* md4.c - MD4 Message-Digest Algorithm
2 * Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 *
20 * Based on md5.c in libgcrypt, but rewritten to compute md4 checksums
21 * using a public domain md4 implementation with the following comments:
22 *
23 * Modified by Wei Dai from Andrew M. Kuchling's md4.c
24 * The original code and all modifications are in the public domain.
25 *
26 * This is the original introductory comment:
27 *
28 * md4.c : MD4 hash algorithm.
29 *
30 * Part of the Python Cryptography Toolkit, version 1.1
31 *
32 * Distribute and use freely; there are no restrictions on further
33 * dissemination and usage except those imposed by the laws of your
34 * country of residence.
35 *
36 */
37
38/* MD4 test suite:
39 * MD4 ("") = 31d6cfe0d16ae931b73c59d7e0c089c0
40 * MD4 ("a") = bde52cb31de33e46245e05fbdbd6fb24
41 * MD4 ("abc") = a448017aaf21d8525fc10ae87aa6729d
42 * MD4 ("message digest") = d9130a8164549fe818874806e1c7014b
43 * MD4 ("abcdefghijklmnopqrstuvwxyz") = d79e1c308aa5bbcdeea8ed63df412da9
44 * MD4 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
45 * 043f8582f241db351ce627e153e7f0e4
46 * MD4 ("123456789012345678901234567890123456789012345678901234567890123456
47 * 78901234567890") = e33b4ddc9c38f2199c3e7b164fcc0536
48 */
49
50#include <config.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include <assert.h>
55#include "g10lib.h"
56#include "memory.h"
57#include "cipher.h"
58
59#include "bithelp.h"
60
61
62typedef struct {
63 u32 A,B,C,D; /* chaining variables */
64 u32 nblocks;
65 byte buf[64];
66 int count;
67} MD4_CONTEXT;
68
69
70static void
71md4_init( void *context )
72{
73 MD4_CONTEXT *ctx = context;
74
75 ctx->A = 0x67452301;
76 ctx->B = 0xefcdab89;
77 ctx->C = 0x98badcfe;
78 ctx->D = 0x10325476;
79
80 ctx->nblocks = 0;
81 ctx->count = 0;
82}
83
84#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
85#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
86#define H(x, y, z) ((x) ^ (y) ^ (z))
87
88
89/****************
90 * transform 64 bytes
91 */
92static void
93transform( MD4_CONTEXT *ctx, byte *data )
94{
95 u32 in[16];
96 register u32 A = ctx->A;
97 register u32 B = ctx->B;
98 register u32 C = ctx->C;
99 register u32 D = ctx->D;
100
101#ifdef WORDS_BIGENDIAN
102 {
103 int i;
104 byte *p2, *p1;
105 for(i=0, p1=data, p2=(byte*)in; i < 16; i++, p2 += 4 )
106 {
107 p2[3] = *p1++;
108 p2[2] = *p1++;
109 p2[1] = *p1++;
110 p2[0] = *p1++;
111 }
112 }
113#else
114 memcpy (in, data, 64);
115#endif
116
117 /* Round 1. */
118#define function(a,b,c,d,k,s) a=rol(a+F(b,c,d)+in[k],s);
119 function(A,B,C,D, 0, 3);
120 function(D,A,B,C, 1, 7);
121 function(C,D,A,B, 2,11);
122 function(B,C,D,A, 3,19);
123 function(A,B,C,D, 4, 3);
124 function(D,A,B,C, 5, 7);
125 function(C,D,A,B, 6,11);
126 function(B,C,D,A, 7,19);
127 function(A,B,C,D, 8, 3);
128 function(D,A,B,C, 9, 7);
129 function(C,D,A,B,10,11);
130 function(B,C,D,A,11,19);
131 function(A,B,C,D,12, 3);
132 function(D,A,B,C,13, 7);
133 function(C,D,A,B,14,11);
134 function(B,C,D,A,15,19);
135
136#undef function
137
138 /* Round 2. */
139#define function(a,b,c,d,k,s) a=rol(a+G(b,c,d)+in[k]+0x5a827999,s);
140
141 function(A,B,C,D, 0, 3);
142 function(D,A,B,C, 4, 5);
143 function(C,D,A,B, 8, 9);
144 function(B,C,D,A,12,13);
145 function(A,B,C,D, 1, 3);
146 function(D,A,B,C, 5, 5);
147 function(C,D,A,B, 9, 9);
148 function(B,C,D,A,13,13);
149 function(A,B,C,D, 2, 3);
150 function(D,A,B,C, 6, 5);
151 function(C,D,A,B,10, 9);
152 function(B,C,D,A,14,13);
153 function(A,B,C,D, 3, 3);
154 function(D,A,B,C, 7, 5);
155 function(C,D,A,B,11, 9);
156 function(B,C,D,A,15,13);
157
158#undef function
159
160 /* Round 3. */
161#define function(a,b,c,d,k,s) a=rol(a+H(b,c,d)+in[k]+0x6ed9eba1,s);
162
163 function(A,B,C,D, 0, 3);
164 function(D,A,B,C, 8, 9);
165 function(C,D,A,B, 4,11);
166 function(B,C,D,A,12,15);
167 function(A,B,C,D, 2, 3);
168 function(D,A,B,C,10, 9);
169 function(C,D,A,B, 6,11);
170 function(B,C,D,A,14,15);
171 function(A,B,C,D, 1, 3);
172 function(D,A,B,C, 9, 9);
173 function(C,D,A,B, 5,11);
174 function(B,C,D,A,13,15);
175 function(A,B,C,D, 3, 3);
176 function(D,A,B,C,11, 9);
177 function(C,D,A,B, 7,11);
178 function(B,C,D,A,15,15);
179
180
181 /* Put checksum in context given as argument. */
182 ctx->A += A;
183 ctx->B += B;
184 ctx->C += C;
185 ctx->D += D;
186}
187
188
189
190/* The routine updates the message-digest context to
191 * account for the presence of each of the characters inBuf[0..inLen-1]
192 * in the message whose digest is being computed.
193 */
194static void
195md4_write( void *context, byte *inbuf, size_t inlen)
196{
197 MD4_CONTEXT *hd = context;
198
199 if( hd->count == 64 ) /* flush the buffer */
200 {
201 transform( hd, hd->buf );
202 _gcry_burn_stack (80+6*sizeof(void*));
203 hd->count = 0;
204 hd->nblocks++;
205 }
206 if( !inbuf )
207 return;
208
209 if( hd->count )
210 {
211 for( ; inlen && hd->count < 64; inlen-- )
212 hd->buf[hd->count++] = *inbuf++;
213 md4_write( hd, NULL, 0 );
214 if( !inlen )
215 return;
216 }
217 _gcry_burn_stack (80+6*sizeof(void*));
218
219 while( inlen >= 64 )
220 {
221 transform( hd, inbuf );
222 hd->count = 0;
223 hd->nblocks++;
224 inlen -= 64;
225 inbuf += 64;
226 }
227 for( ; inlen && hd->count < 64; inlen-- )
228 hd->buf[hd->count++] = *inbuf++;
229}
230
231
232
233/* The routine final terminates the message-digest computation and
234 * ends with the desired message digest in mdContext->digest[0...15].
235 * The handle is prepared for a new MD4 cycle.
236 * Returns 16 bytes representing the digest.
237 */
238
239static void
240md4_final( void *context )
241{
242 MD4_CONTEXT *hd = context;
243 u32 t, msb, lsb;
244 byte *p;
245
246 md4_write(hd, NULL, 0); /* flush */;
247
248 t = hd->nblocks;
249 /* multiply by 64 to make a byte count */
250 lsb = t << 6;
251 msb = t >> 26;
252 /* add the count */
253 t = lsb;
254 if( (lsb += hd->count) < t )
255 msb++;
256 /* multiply by 8 to make a bit count */
257 t = lsb;
258 lsb <<= 3;
259 msb <<= 3;
260 msb |= t >> 29;
261
262 if( hd->count < 56 ) /* enough room */
263 {
264 hd->buf[hd->count++] = 0x80; /* pad */
265 while( hd->count < 56 )
266 hd->buf[hd->count++] = 0; /* pad */
267 }
268 else /* need one extra block */
269 {
270 hd->buf[hd->count++] = 0x80; /* pad character */
271 while( hd->count < 64 )
272 hd->buf[hd->count++] = 0;
273 md4_write(hd, NULL, 0); /* flush */;
274 memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
275 }
276 /* append the 64 bit count */
277 hd->buf[56] = lsb ;
278 hd->buf[57] = lsb >> 8;
279 hd->buf[58] = lsb >> 16;
280 hd->buf[59] = lsb >> 24;
281 hd->buf[60] = msb ;
282 hd->buf[61] = msb >> 8;
283 hd->buf[62] = msb >> 16;
284 hd->buf[63] = msb >> 24;
285 transform( hd, hd->buf );
286 _gcry_burn_stack (80+6*sizeof(void*));
287
288 p = hd->buf;
289#ifdef WORDS_BIGENDIAN
290#define X(a) do { *p++ = hd->a ; *p++ = hd->a >> 8; \
291 *p++ = hd->a >> 16; *p++ = hd->a >> 24; } while(0)
292#else /* little endian */
293#define X(a) do { *(u32*)p = (*hd).a ; p += 4; } while(0)
294#endif
295 X(A);
296 X(B);
297 X(C);
298 X(D);
299#undef X
300
301}
302
303static byte *
304md4_read (void *context)
305{
306 MD4_CONTEXT *hd = context;
307 return hd->buf;
308}
309
310static byte asn[18] = /* Object ID is 1.2.840.113549.2.4 */
311 { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86,0x48,
312 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 };
313
314static gcry_md_oid_spec_t oid_spec_md4[] =
315 {
316 /* iso.member-body.us.rsadsi.digestAlgorithm.md4 */
317 { "1.2.840.113549.2.4" },
318 { NULL },
319 };
320
321gcry_md_spec_t _gcry_digest_spec_md4 =
322 {
323 "MD4", asn, DIM (asn), oid_spec_md4,16,
324 md4_init, md4_write, md4_final, md4_read,
325 sizeof (MD4_CONTEXT)
326 };
327
diff --git a/pwmanager/libcrypt/cipher/md5.c b/pwmanager/libcrypt/cipher/md5.c
new file mode 100644
index 0000000..ec10b4d
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/md5.c
@@ -0,0 +1,354 @@
1/* md5.c - MD5 Message-Digest Algorithm
2 * Copyright (C) 1995,1996,1998,1999,2001,2002,
3 * 2003 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 *
21 * According to the definition of MD5 in RFC 1321 from April 1992.
22 * NOTE: This is *not* the same file as the one from glibc.
23 * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
24 * heavily modified for GnuPG by Werner Koch <wk@gnupg.org>
25 */
26
27/* Test values:
28 * "" D4 1D 8C D9 8F 00 B2 04 E9 80 09 98 EC F8 42 7E
29 * "a" 0C C1 75 B9 C0 F1 B6 A8 31 C3 99 E2 69 77 26 61
30 * "abc 90 01 50 98 3C D2 4F B0 D6 96 3F 7D 28 E1 7F 72
31 * "message digest" F9 6B 69 7D 7C B7 93 8D 52 5A 2F 31 AA F1 61 D0
32 */
33
34#include <config.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <assert.h>
39#include "g10lib.h"
40#include "memory.h"
41#include "cipher.h"
42
43#include "bithelp.h"
44
45
46typedef struct {
47 u32 A,B,C,D; /* chaining variables */
48 u32 nblocks;
49 byte buf[64];
50 int count;
51} MD5_CONTEXT;
52
53
54static void
55md5_init( void *context )
56{
57 MD5_CONTEXT *ctx = context;
58
59 ctx->A = 0x67452301;
60 ctx->B = 0xefcdab89;
61 ctx->C = 0x98badcfe;
62 ctx->D = 0x10325476;
63
64 ctx->nblocks = 0;
65 ctx->count = 0;
66}
67
68
69/* These are the four functions used in the four steps of the MD5 algorithm
70 and defined in the RFC 1321. The first function is a little bit optimized
71 (as found in Colin Plumbs public domain implementation). */
72/* #define FF(b, c, d) ((b & c) | (~b & d)) */
73#define FF(b, c, d) (d ^ (b & (c ^ d)))
74#define FG(b, c, d) FF (d, b, c)
75#define FH(b, c, d) (b ^ c ^ d)
76#define FI(b, c, d) (c ^ (b | ~d))
77
78
79/****************
80 * transform n*64 bytes
81 */
82static void
83transform( MD5_CONTEXT *ctx, byte *data )
84{
85 u32 correct_words[16];
86 register u32 A = ctx->A;
87 register u32 B = ctx->B;
88 register u32 C = ctx->C;
89 register u32 D = ctx->D;
90 u32 *cwp = correct_words;
91
92#ifdef WORDS_BIGENDIAN
93 {
94 int i;
95 byte *p2, *p1;
96 for(i=0, p1=data, p2=(byte*)correct_words; i < 16; i++, p2 += 4 )
97 {
98 p2[3] = *p1++;
99 p2[2] = *p1++;
100 p2[1] = *p1++;
101 p2[0] = *p1++;
102 }
103 }
104#else
105 memcpy( correct_words, data, 64 );
106#endif
107
108
109#define OP(a, b, c, d, s, T) \
110 do \
111 { \
112 a += FF (b, c, d) + (*cwp++) + T; \
113 a = rol(a, s); \
114 a += b; \
115 } \
116 while (0)
117
118 /* Before we start, one word about the strange constants.
119 They are defined in RFC 1321 as
120
121 T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
122 */
123
124 /* Round 1. */
125 OP (A, B, C, D, 7, 0xd76aa478);
126 OP (D, A, B, C, 12, 0xe8c7b756);
127 OP (C, D, A, B, 17, 0x242070db);
128 OP (B, C, D, A, 22, 0xc1bdceee);
129 OP (A, B, C, D, 7, 0xf57c0faf);
130 OP (D, A, B, C, 12, 0x4787c62a);
131 OP (C, D, A, B, 17, 0xa8304613);
132 OP (B, C, D, A, 22, 0xfd469501);
133 OP (A, B, C, D, 7, 0x698098d8);
134 OP (D, A, B, C, 12, 0x8b44f7af);
135 OP (C, D, A, B, 17, 0xffff5bb1);
136 OP (B, C, D, A, 22, 0x895cd7be);
137 OP (A, B, C, D, 7, 0x6b901122);
138 OP (D, A, B, C, 12, 0xfd987193);
139 OP (C, D, A, B, 17, 0xa679438e);
140 OP (B, C, D, A, 22, 0x49b40821);
141
142#undef OP
143#define OP(f, a, b, c, d, k, s, T) \
144 do \
145 { \
146 a += f (b, c, d) + correct_words[k] + T; \
147 a = rol(a, s); \
148 a += b; \
149 } \
150 while (0)
151
152 /* Round 2. */
153 OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
154 OP (FG, D, A, B, C, 6, 9, 0xc040b340);
155 OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
156 OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
157 OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
158 OP (FG, D, A, B, C, 10, 9, 0x02441453);
159 OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
160 OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
161 OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
162 OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
163 OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
164 OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
165 OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
166 OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
167 OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
168 OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
169
170 /* Round 3. */
171 OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
172 OP (FH, D, A, B, C, 8, 11, 0x8771f681);
173 OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
174 OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
175 OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
176 OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
177 OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
178 OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
179 OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
180 OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
181 OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
182 OP (FH, B, C, D, A, 6, 23, 0x04881d05);
183 OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
184 OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
185 OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
186 OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
187
188 /* Round 4. */
189 OP (FI, A, B, C, D, 0, 6, 0xf4292244);
190 OP (FI, D, A, B, C, 7, 10, 0x432aff97);
191 OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
192 OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
193 OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
194 OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
195 OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
196 OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
197 OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
198 OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
199 OP (FI, C, D, A, B, 6, 15, 0xa3014314);
200 OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
201 OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
202 OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
203 OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
204 OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
205
206 /* Put checksum in context given as argument. */
207 ctx->A += A;
208 ctx->B += B;
209 ctx->C += C;
210 ctx->D += D;
211}
212
213
214
215/* The routine updates the message-digest context to
216 * account for the presence of each of the characters inBuf[0..inLen-1]
217 * in the message whose digest is being computed.
218 */
219static void
220md5_write( void *context, byte *inbuf, size_t inlen)
221{
222 MD5_CONTEXT *hd = context;
223
224 if( hd->count == 64 ) /* flush the buffer */
225 {
226 transform( hd, hd->buf );
227 _gcry_burn_stack (80+6*sizeof(void*));
228 hd->count = 0;
229 hd->nblocks++;
230 }
231 if( !inbuf )
232 return;
233
234 if( hd->count )
235 {
236 for( ; inlen && hd->count < 64; inlen-- )
237 hd->buf[hd->count++] = *inbuf++;
238 md5_write( hd, NULL, 0 );
239 if( !inlen )
240 return;
241 }
242 _gcry_burn_stack (80+6*sizeof(void*));
243
244 while( inlen >= 64 )
245 {
246 transform( hd, inbuf );
247 hd->count = 0;
248 hd->nblocks++;
249 inlen -= 64;
250 inbuf += 64;
251 }
252 for( ; inlen && hd->count < 64; inlen-- )
253 hd->buf[hd->count++] = *inbuf++;
254
255}
256
257
258
259/* The routine final terminates the message-digest computation and
260 * ends with the desired message digest in mdContext->digest[0...15].
261 * The handle is prepared for a new MD5 cycle.
262 * Returns 16 bytes representing the digest.
263 */
264
265static void
266md5_final( void *context)
267{
268 MD5_CONTEXT *hd = context;
269 u32 t, msb, lsb;
270 byte *p;
271
272 md5_write(hd, NULL, 0); /* flush */;
273
274 t = hd->nblocks;
275 /* multiply by 64 to make a byte count */
276 lsb = t << 6;
277 msb = t >> 26;
278 /* add the count */
279 t = lsb;
280 if( (lsb += hd->count) < t )
281 msb++;
282 /* multiply by 8 to make a bit count */
283 t = lsb;
284 lsb <<= 3;
285 msb <<= 3;
286 msb |= t >> 29;
287
288 if( hd->count < 56 ) /* enough room */
289 {
290 hd->buf[hd->count++] = 0x80; /* pad */
291 while( hd->count < 56 )
292 hd->buf[hd->count++] = 0; /* pad */
293 }
294 else /* need one extra block */
295 {
296 hd->buf[hd->count++] = 0x80; /* pad character */
297 while( hd->count < 64 )
298 hd->buf[hd->count++] = 0;
299 md5_write(hd, NULL, 0); /* flush */;
300 memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
301 }
302 /* append the 64 bit count */
303 hd->buf[56] = lsb ;
304 hd->buf[57] = lsb >> 8;
305 hd->buf[58] = lsb >> 16;
306 hd->buf[59] = lsb >> 24;
307 hd->buf[60] = msb ;
308 hd->buf[61] = msb >> 8;
309 hd->buf[62] = msb >> 16;
310 hd->buf[63] = msb >> 24;
311 transform( hd, hd->buf );
312 _gcry_burn_stack (80+6*sizeof(void*));
313
314 p = hd->buf;
315#ifdef WORDS_BIGENDIAN
316#define X(a) do { *p++ = hd->a ; *p++ = hd->a >> 8; \
317 *p++ = hd->a >> 16; *p++ = hd->a >> 24; } while(0)
318#else /* little endian */
319#define X(a) do { *(u32*)p = (*hd).a ; p += 4; } while(0)
320#endif
321 X(A);
322 X(B);
323 X(C);
324 X(D);
325#undef X
326
327}
328
329static byte *
330md5_read( void *context )
331{
332 MD5_CONTEXT *hd = (MD5_CONTEXT *) context;
333 return hd->buf;
334}
335
336static byte asn[18] = /* Object ID is 1.2.840.113549.2.5 */
337 { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86,0x48,
338 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 };
339
340static gcry_md_oid_spec_t oid_spec_md5[] =
341 {
342 /* iso.member-body.us.rsadsi.pkcs.pkcs-1.4 (md5WithRSAEncryption) */
343 { "1.2.840.113549.1.1.4" },
344 /* RSADSI digestAlgorithm MD5 */
345 { "1.2.840.113549.2.5" },
346 { NULL },
347 };
348
349gcry_md_spec_t _gcry_digest_spec_md5 =
350 {
351 "MD5", asn, DIM (asn), oid_spec_md5, 16,
352 md5_init, md5_write, md5_final, md5_read,
353 sizeof (MD5_CONTEXT)
354 };
diff --git a/pwmanager/libcrypt/cipher/primegen.c b/pwmanager/libcrypt/cipher/primegen.c
new file mode 100644
index 0000000..afd435e
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/primegen.c
@@ -0,0 +1,1028 @@
1/* primegen.c - prime number generator
2 * Copyright (C) 1998, 2000, 2001, 2002, 2003
3 * 2004 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser general Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 *
21 * ***********************************************************************
22 * The algorithm used to generate practically save primes is due to
23 * Lim and Lee as described in the CRYPTO '97 proceedings (ISBN3540633847)
24 * page 260.
25 */
26
27#include <config.h>
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <assert.h>
33#include <errno.h>
34
35#include "g10lib.h"
36#include "mpi.h"
37#include "cipher.h"
38
39static gcry_mpi_t gen_prime (unsigned int nbits, int secret, int randomlevel,
40 int (*extra_check)(void *, gcry_mpi_t),
41 void *extra_check_arg);
42static int check_prime( gcry_mpi_t prime, gcry_mpi_t val_2,
43 gcry_prime_check_func_t cb_func, void *cb_arg );
44static int is_prime( gcry_mpi_t n, int steps, int *count );
45static void m_out_of_n( char *array, int m, int n );
46
47static void (*progress_cb) (void *,const char*,int,int, int );
48static void *progress_cb_data;
49
50/* Note: 2 is not included because it can be tested more easily by
51 looking at bit 0. The last entry in this list is marked by a zero */
52static ushort small_prime_numbers[] = {
53 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
54 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
55 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
56 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
57 211, 223, 227, 229, 233, 239, 241, 251, 257, 263,
58 269, 271, 277, 281, 283, 293, 307, 311, 313, 317,
59 331, 337, 347, 349, 353, 359, 367, 373, 379, 383,
60 389, 397, 401, 409, 419, 421, 431, 433, 439, 443,
61 449, 457, 461, 463, 467, 479, 487, 491, 499, 503,
62 509, 521, 523, 541, 547, 557, 563, 569, 571, 577,
63 587, 593, 599, 601, 607, 613, 617, 619, 631, 641,
64 643, 647, 653, 659, 661, 673, 677, 683, 691, 701,
65 709, 719, 727, 733, 739, 743, 751, 757, 761, 769,
66 773, 787, 797, 809, 811, 821, 823, 827, 829, 839,
67 853, 857, 859, 863, 877, 881, 883, 887, 907, 911,
68 919, 929, 937, 941, 947, 953, 967, 971, 977, 983,
69 991, 997, 1009, 1013, 1019, 1021, 1031, 1033,
70 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091,
71 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151,
72 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213,
73 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277,
74 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307,
75 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399,
76 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451,
77 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493,
78 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559,
79 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609,
80 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667,
81 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733,
82 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789,
83 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871,
84 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931,
85 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997,
86 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053,
87 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111,
88 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161,
89 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243,
90 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297,
91 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357,
92 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411,
93 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473,
94 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551,
95 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633,
96 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687,
97 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729,
98 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791,
99 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851,
100 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917,
101 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999,
102 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061,
103 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137,
104 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209,
105 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271,
106 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331,
107 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391,
108 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467,
109 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533,
110 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583,
111 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643,
112 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709,
113 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779,
114 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851,
115 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917,
116 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989,
117 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049,
118 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111,
119 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177,
120 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243,
121 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297,
122 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391,
123 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457,
124 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519,
125 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597,
126 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657,
127 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729,
128 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799,
129 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889,
130 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951,
131 4957, 4967, 4969, 4973, 4987, 4993, 4999,
132 0
133};
134static int no_of_small_prime_numbers = DIM (small_prime_numbers) - 1;
135
136void
137_gcry_register_primegen_progress ( void (*cb)(void *,const char*,int,int,int),
138 void *cb_data )
139{
140 progress_cb = cb;
141 progress_cb_data = cb_data;
142}
143
144
145static void
146progress( int c )
147{
148 if ( progress_cb )
149 progress_cb ( progress_cb_data, "primegen", c, 0, 0 );
150}
151
152
153/****************
154 * Generate a prime number (stored in secure memory)
155 */
156gcry_mpi_t
157_gcry_generate_secret_prime (unsigned int nbits,
158 int (*extra_check)(void*, gcry_mpi_t),
159 void *extra_check_arg)
160{
161 gcry_mpi_t prime;
162
163 prime = gen_prime( nbits, 1, 2, extra_check, extra_check_arg);
164 progress('\n');
165 return prime;
166}
167
168gcry_mpi_t
169_gcry_generate_public_prime( unsigned int nbits,
170 int (*extra_check)(void*, gcry_mpi_t),
171 void *extra_check_arg)
172{
173 gcry_mpi_t prime;
174
175 prime = gen_prime( nbits, 0, 2, extra_check, extra_check_arg );
176 progress('\n');
177 return prime;
178}
179
180
181/****************
182 * We do not need to use the strongest RNG because we gain no extra
183 * security from it - The prime number is public and we could also
184 * offer the factors for those who are willing to check that it is
185 * indeed a strong prime. With ALL_FACTORS set to true all afcors of
186 * prime-1 are returned in FACTORS.
187 *
188 * mode 0: Standard
189 *1: Make sure that at least one factor is of size qbits.
190 */
191static gcry_err_code_t
192prime_generate_internal (int mode,
193 gcry_mpi_t *prime_generated, unsigned int pbits,
194 unsigned int qbits, gcry_mpi_t g,
195 gcry_mpi_t **ret_factors,
196 gcry_random_level_t randomlevel, unsigned int flags,
197 int all_factors,
198 gcry_prime_check_func_t cb_func, void *cb_arg)
199{
200 gcry_err_code_t err = 0;
201 gcry_mpi_t *factors_new = NULL; /* Factors to return to the
202 caller. */
203 gcry_mpi_t *factors = NULL;/* Current factors. */
204 gcry_mpi_t *pool = NULL;/* Pool of primes. */
205 unsigned char *perms = NULL;/* Permutations of POOL. */
206 gcry_mpi_t q_factor = NULL;/* Used if QBITS is non-zero. */
207 unsigned int fbits = 0;/* Length of prime factors. */
208 unsigned int n = 0; /* Number of factors. */
209 unsigned int m = 0; /* Number of primes in pool. */
210 gcry_mpi_t q = NULL; /* First prime factor. */
211 gcry_mpi_t prime = NULL;/* Prime candidate. */
212 unsigned int nprime = 0;/* Bits of PRIME. */
213 unsigned int req_qbits; /* The original QBITS value. */
214 gcry_mpi_t val_2; /* For check_prime(). */
215 unsigned int is_secret = (flags & GCRY_PRIME_FLAG_SECRET);
216 unsigned int count1 = 0, count2 = 0;
217 unsigned int i = 0, j = 0;
218
219 if (pbits < 48)
220 return GPG_ERR_INV_ARG;
221
222 /* If QBITS is not given, assume a reasonable value. */
223 if (!qbits)
224 qbits = pbits / 3;
225
226 req_qbits = qbits;
227
228 /* Find number of needed prime factors. */
229 for (n = 1; (pbits - qbits - 1) / n >= qbits; n++)
230 ;
231 n--;
232
233 val_2 = mpi_alloc_set_ui (2);
234
235 if ((! n) || ((mode == 1) && (n < 2)))
236 {
237 err = GPG_ERR_INV_ARG;
238 goto leave;
239 }
240
241 if (mode == 1)
242 {
243 n--;
244 fbits = (pbits - 2 * req_qbits -1) / n;
245 qbits = pbits - req_qbits - n * fbits;
246 }
247 else
248 {
249 fbits = (pbits - req_qbits -1) / n;
250 qbits = pbits - n * fbits;
251 }
252
253 if (DBG_CIPHER)
254 log_debug ("gen prime: pbits=%u qbits=%u fbits=%u/%u n=%d\n",
255 pbits, req_qbits, qbits, fbits, n);
256
257 prime = gcry_mpi_new (pbits);
258
259 /* Generate first prime factor. */
260 q = gen_prime (qbits, is_secret, randomlevel, NULL, NULL);
261
262 if (mode == 1)
263 q_factor = gen_prime (req_qbits, is_secret, randomlevel, NULL, NULL);
264
265 /* Allocate an array to hold the factors + 2 for later usage. */
266 factors = gcry_calloc (n + 2, sizeof (*factors));
267 if (!factors)
268 {
269 err = gpg_err_code_from_errno (errno);
270 goto leave;
271 }
272
273 /* Make a pool of 3n+5 primes (this is an arbitrary value). */
274 m = n * 3 + 5;
275 if (mode == 1) /* Need some more (for e.g. DSA). */
276 m += 5;
277 if (m < 25)
278 m = 25;
279 pool = gcry_calloc (m , sizeof (*pool));
280 if (! pool)
281 {
282 err = gpg_err_code_from_errno (errno);
283 goto leave;
284 }
285
286 /* Permutate over the pool of primes. */
287 do
288 {
289 next_try:
290 if (! perms)
291 {
292 /* Allocate new primes. */
293 for(i = 0; i < m; i++)
294 {
295 mpi_free (pool[i]);
296 pool[i] = NULL;
297 }
298
299 /* Init m_out_of_n(). */
300 perms = gcry_calloc (1, m);
301 if (! perms)
302 {
303 err = gpg_err_code_from_errno (errno);
304 goto leave;
305 }
306 for(i = 0; i < n; i++)
307 {
308 perms[i] = 1;
309 pool[i] = gen_prime (fbits, is_secret,
310 randomlevel, NULL, NULL);
311 factors[i] = pool[i];
312 }
313 }
314 else
315 {
316 m_out_of_n (perms, n, m);
317 for (i = j = 0; (i < m) && (j < n); i++)
318 if (perms[i])
319 {
320 if(! pool[i])
321 pool[i] = gen_prime (fbits, 0, 1, NULL, NULL);
322 factors[j++] = pool[i];
323 }
324 if (i == n)
325 {
326 gcry_free (perms);
327 perms = NULL;
328 progress ('!');
329 goto next_try;/* Allocate new primes. */
330 }
331 }
332
333 /* Generate next prime candidate:
334 p = 2 * q [ * q_factor] * factor_0 * factor_1 * ... * factor_n + 1.
335 */
336 mpi_set (prime, q);
337 mpi_mul_ui (prime, prime, 2);
338 if (mode == 1)
339 mpi_mul (prime, prime, q_factor);
340 for(i = 0; i < n; i++)
341 mpi_mul (prime, prime, factors[i]);
342 mpi_add_ui (prime, prime, 1);
343 nprime = mpi_get_nbits (prime);
344
345 if (nprime < pbits)
346 {
347 if (++count1 > 20)
348 {
349 count1 = 0;
350 qbits++;
351 progress('>');
352 mpi_free (q);
353 q = gen_prime (qbits, 0, 0, NULL, NULL);
354 goto next_try;
355 }
356 }
357 else
358 count1 = 0;
359
360 if (nprime > pbits)
361 {
362 if (++count2 > 20)
363 {
364 count2 = 0;
365 qbits--;
366 progress('<');
367 mpi_free (q);
368 q = gen_prime (qbits, 0, 0, NULL, NULL);
369 goto next_try;
370 }
371 }
372 else
373 count2 = 0;
374 }
375 while (! ((nprime == pbits) && check_prime (prime, val_2, cb_func, cb_arg)));
376
377 if (DBG_CIPHER)
378 {
379 progress ('\n');
380 log_mpidump ("prime : ", prime);
381 log_mpidump ("factor q: ", q);
382 if (mode == 1)
383 log_mpidump ("factor q0: ", q_factor);
384 for (i = 0; i < n; i++)
385 log_mpidump ("factor pi: ", factors[i]);
386 log_debug ("bit sizes: prime=%u, q=%u",
387 mpi_get_nbits (prime), mpi_get_nbits (q));
388 if (mode == 1)
389 log_debug (", q0=%u", mpi_get_nbits (q_factor));
390 for (i = 0; i < n; i++)
391 log_debug (", p%d=%u", i, mpi_get_nbits (factors[i]));
392 progress('\n');
393 }
394
395 if (ret_factors)
396 {
397 /* Caller wants the factors. */
398 factors_new = gcry_calloc (n + 4, sizeof (*factors_new));
399 if (! factors_new)
400 {
401 err = gpg_err_code_from_errno (errno);
402 goto leave;
403 }
404
405 if (all_factors)
406 {
407 i = 0;
408 factors_new[i++] = gcry_mpi_set_ui (NULL, 2);
409 factors_new[i++] = mpi_copy (q);
410 if (mode == 1)
411 factors_new[i++] = mpi_copy (q_factor);
412 for(j=0; j < n; j++)
413 factors_new[i++] = mpi_copy (factors[j]);
414 }
415 else
416 {
417 i = 0;
418 if (mode == 1)
419 {
420 factors_new[i++] = mpi_copy (q_factor);
421 for (; i <= n; i++)
422 factors_new[i] = mpi_copy (factors[i]);
423 }
424 else
425 for (; i < n; i++ )
426 factors_new[i] = mpi_copy (factors[i]);
427 }
428 }
429
430 if (g)
431 {
432 /* Create a generator (start with 3). */
433 gcry_mpi_t tmp = mpi_alloc (mpi_get_nlimbs (prime));
434 gcry_mpi_t b = mpi_alloc (mpi_get_nlimbs (prime));
435 gcry_mpi_t pmin1 = mpi_alloc (mpi_get_nlimbs (prime));
436
437 if (mode == 1)
438 err = GPG_ERR_NOT_IMPLEMENTED;
439 else
440 {
441 factors[n] = q;
442 factors[n + 1] = mpi_alloc_set_ui (2);
443 mpi_sub_ui (pmin1, prime, 1);
444 mpi_set_ui (g, 2);
445 do
446 {
447 mpi_add_ui (g, g, 1);
448 if (DBG_CIPHER)
449 {
450 log_debug ("checking g:");
451 gcry_mpi_dump (g);
452 log_printf ("\n");
453 }
454 else
455 progress('^');
456 for (i = 0; i < n + 2; i++)
457 {
458 mpi_fdiv_q (tmp, pmin1, factors[i]);
459 /* No mpi_pow(), but it is okay to use this with mod
460 prime. */
461 gcry_mpi_powm (b, g, tmp, prime);
462 if (! mpi_cmp_ui (b, 1))
463 break;
464 }
465 if (DBG_CIPHER)
466 progress('\n');
467 }
468 while (i < n + 2);
469
470 mpi_free (factors[n+1]);
471 mpi_free (tmp);
472 mpi_free (b);
473 mpi_free (pmin1);
474 }
475 }
476
477 if (! DBG_CIPHER)
478 progress ('\n');
479
480
481 leave:
482 if (pool)
483 {
484 for(i = 0; i < m; i++)
485 mpi_free (pool[i]);
486 gcry_free (pool);
487 }
488 if (factors)
489 gcry_free (factors); /* Factors are shallow copies. */
490 if (perms)
491 gcry_free (perms);
492
493 mpi_free (val_2);
494 mpi_free (q);
495 mpi_free (q_factor);
496
497 if (! err)
498 {
499 *prime_generated = prime;
500 if (ret_factors)
501 *ret_factors = factors_new;
502 }
503 else
504 {
505 if (factors_new)
506 {
507 for (i = 0; factors_new[i]; i++)
508 mpi_free (factors_new[i]);
509 gcry_free (factors_new);
510 }
511 mpi_free (prime);
512 }
513
514 return err;
515}
516
517gcry_mpi_t
518_gcry_generate_elg_prime (int mode, unsigned pbits, unsigned qbits,
519 gcry_mpi_t g, gcry_mpi_t **ret_factors)
520{
521 gcry_err_code_t err = GPG_ERR_NO_ERROR;
522 gcry_mpi_t prime = NULL;
523
524 err = prime_generate_internal (mode, &prime, pbits, qbits, g,
525 ret_factors, GCRY_WEAK_RANDOM, 0, 0,
526 NULL, NULL);
527
528 return prime;
529}
530
531static gcry_mpi_t
532gen_prime (unsigned int nbits, int secret, int randomlevel,
533 int (*extra_check)(void *, gcry_mpi_t), void *extra_check_arg)
534{
535 gcry_mpi_t prime, ptest, pminus1, val_2, val_3, result;
536 int i;
537 unsigned x, step;
538 unsigned count1, count2;
539 int *mods;
540
541/* if ( DBG_CIPHER ) */
542/* log_debug ("generate a prime of %u bits ", nbits ); */
543
544 if (nbits < 16)
545 log_fatal ("can't generate a prime with less than %d bits\n", 16);
546
547 mods = gcry_xmalloc( no_of_small_prime_numbers * sizeof *mods );
548 /* Make nbits fit into gcry_mpi_t implementation. */
549 val_2 = mpi_alloc_set_ui( 2 );
550 val_3 = mpi_alloc_set_ui( 3);
551 prime = secret? gcry_mpi_snew ( nbits ): gcry_mpi_new ( nbits );
552 result = mpi_alloc_like( prime );
553 pminus1= mpi_alloc_like( prime );
554 ptest = mpi_alloc_like( prime );
555 count1 = count2 = 0;
556 for (;;)
557 { /* try forvever */
558 int dotcount=0;
559
560 /* generate a random number */
561 gcry_mpi_randomize( prime, nbits, randomlevel );
562
563 /* Set high order bit to 1, set low order bit to 1. If we are
564 generating a secret prime we are most probably doing that
565 for RSA, to make sure that the modulus does have the
566 requested key size we set the 2 high order bits. */
567 mpi_set_highbit (prime, nbits-1);
568 if (secret)
569 mpi_set_bit (prime, nbits-2);
570 mpi_set_bit(prime, 0);
571
572 /* Calculate all remainders. */
573 for (i=0; (x = small_prime_numbers[i]); i++ )
574 mods[i] = mpi_fdiv_r_ui(NULL, prime, x);
575
576 /* Now try some primes starting with prime. */
577 for(step=0; step < 20000; step += 2 )
578 {
579 /* Check against all the small primes we have in mods. */
580 count1++;
581 for (i=0; (x = small_prime_numbers[i]); i++ )
582 {
583 while ( mods[i] + step >= x )
584 mods[i] -= x;
585 if ( !(mods[i] + step) )
586 break;
587 }
588 if ( x )
589 continue; /* Found a multiple of an already known prime. */
590
591 mpi_add_ui( ptest, prime, step );
592
593 /* Do a fast Fermat test now. */
594 count2++;
595 mpi_sub_ui( pminus1, ptest, 1);
596 gcry_mpi_powm( result, val_2, pminus1, ptest );
597 if ( !mpi_cmp_ui( result, 1 ) )
598 {
599 /* Not composite, perform stronger tests */
600 if (is_prime(ptest, 5, &count2 ))
601 {
602 if (!mpi_test_bit( ptest, nbits-1-secret ))
603 {
604 progress('\n');
605 log_debug ("overflow in prime generation\n");
606 break; /* Stop loop, continue with a new prime. */
607 }
608
609 if (extra_check && extra_check (extra_check_arg, ptest))
610 {
611 /* The extra check told us that this prime is
612 not of the caller's taste. */
613 progress ('/');
614 }
615 else
616 {
617 /* Got it. */
618 mpi_free(val_2);
619 mpi_free(val_3);
620 mpi_free(result);
621 mpi_free(pminus1);
622 mpi_free(prime);
623 gcry_free(mods);
624 return ptest;
625 }
626 }
627 }
628 if (++dotcount == 10 )
629 {
630 progress('.');
631 dotcount = 0;
632 }
633 }
634 progress(':'); /* restart with a new random value */
635 }
636}
637
638/****************
639 * Returns: true if this may be a prime
640 */
641static int
642check_prime( gcry_mpi_t prime, gcry_mpi_t val_2,
643 gcry_prime_check_func_t cb_func, void *cb_arg)
644{
645 int i;
646 unsigned int x;
647 int count=0;
648
649 /* Check against small primes. */
650 for (i=0; (x = small_prime_numbers[i]); i++ )
651 {
652 if ( mpi_divisible_ui( prime, x ) )
653 return 0;
654 }
655
656 /* A quick Fermat test. */
657 {
658 gcry_mpi_t result = mpi_alloc_like( prime );
659 gcry_mpi_t pminus1 = mpi_alloc_like( prime );
660 mpi_sub_ui( pminus1, prime, 1);
661 gcry_mpi_powm( result, val_2, pminus1, prime );
662 mpi_free( pminus1 );
663 if ( mpi_cmp_ui( result, 1 ) )
664 {
665 /* Is composite. */
666 mpi_free( result );
667 progress('.');
668 return 0;
669 }
670 mpi_free( result );
671 }
672
673 if (!cb_func || cb_func (cb_arg, GCRY_PRIME_CHECK_AT_MAYBE_PRIME, prime))
674 {
675 /* Perform stronger tests. */
676 if ( is_prime( prime, 5, &count ) )
677 {
678 if (!cb_func
679 || cb_func (cb_arg, GCRY_PRIME_CHECK_AT_GOT_PRIME, prime))
680 return 1; /* Probably a prime. */
681 }
682 }
683 progress('.');
684 return 0;
685}
686
687
688/*
689 * Return true if n is probably a prime
690 */
691static int
692is_prime (gcry_mpi_t n, int steps, int *count)
693{
694 gcry_mpi_t x = mpi_alloc( mpi_get_nlimbs( n ) );
695 gcry_mpi_t y = mpi_alloc( mpi_get_nlimbs( n ) );
696 gcry_mpi_t z = mpi_alloc( mpi_get_nlimbs( n ) );
697 gcry_mpi_t nminus1 = mpi_alloc( mpi_get_nlimbs( n ) );
698 gcry_mpi_t a2 = mpi_alloc_set_ui( 2 );
699 gcry_mpi_t q;
700 unsigned i, j, k;
701 int rc = 0;
702 unsigned nbits = mpi_get_nbits( n );
703
704 mpi_sub_ui( nminus1, n, 1 );
705
706 /* Find q and k, so that n = 1 + 2^k * q . */
707 q = mpi_copy ( nminus1 );
708 k = mpi_trailing_zeros ( q );
709 mpi_tdiv_q_2exp (q, q, k);
710
711 for (i=0 ; i < steps; i++ )
712 {
713 ++*count;
714 if( !i )
715 {
716 mpi_set_ui( x, 2 );
717 }
718 else
719 {
720 gcry_mpi_randomize( x, nbits, GCRY_WEAK_RANDOM );
721
722 /* Make sure that the number is smaller than the prime and
723 keep the randomness of the high bit. */
724 if ( mpi_test_bit ( x, nbits-2) )
725 {
726 mpi_set_highbit ( x, nbits-2); /* Clear all higher bits. */
727 }
728 else
729 {
730 mpi_set_highbit( x, nbits-2 );
731 mpi_clear_bit( x, nbits-2 );
732 }
733 assert ( mpi_cmp( x, nminus1 ) < 0 && mpi_cmp_ui( x, 1 ) > 0 );
734 }
735 gcry_mpi_powm ( y, x, q, n);
736 if ( mpi_cmp_ui(y, 1) && mpi_cmp( y, nminus1 ) )
737 {
738 for ( j=1; j < k && mpi_cmp( y, nminus1 ); j++ )
739 {
740 gcry_mpi_powm(y, y, a2, n);
741 if( !mpi_cmp_ui( y, 1 ) )
742 goto leave; /* Not a prime. */
743 }
744 if (mpi_cmp( y, nminus1 ) )
745 goto leave; /* Not a prime. */
746 }
747 progress('+');
748 }
749 rc = 1; /* May be a prime. */
750
751 leave:
752 mpi_free( x );
753 mpi_free( y );
754 mpi_free( z );
755 mpi_free( nminus1 );
756 mpi_free( q );
757 mpi_free( a2 );
758
759 return rc;
760}
761
762
763static void
764m_out_of_n ( char *array, int m, int n )
765{
766 int i=0, i1=0, j=0, jp=0, j1=0, k1=0, k2=0;
767
768 if( !m || m >= n )
769 return;
770
771 if( m == 1 )
772 {
773 /* Special case. */
774 for (i=0; i < n; i++ )
775 {
776 if( array[i] )
777 {
778 array[i++] = 0;
779 if( i >= n )
780 i = 0;
781 array[i] = 1;
782 return;
783 }
784 }
785 BUG();
786 }
787
788 for (j=1; j < n; j++ )
789 {
790 if ( array[n-1] == array[n-j-1])
791 continue;
792 j1 = j;
793 break;
794 }
795
796 if ( (m & 1) )
797 {
798 /* M is odd. */
799 if( array[n-1] )
800 {
801 if( j1 & 1 )
802 {
803 k1 = n - j1;
804 k2 = k1+2;
805 if( k2 > n )
806 k2 = n;
807 goto leave;
808 }
809 goto scan;
810 }
811 k2 = n - j1 - 1;
812 if( k2 == 0 )
813 {
814 k1 = i;
815 k2 = n - j1;
816 }
817 else if( array[k2] && array[k2-1] )
818 k1 = n;
819 else
820 k1 = k2 + 1;
821 }
822 else
823 {
824 /* M is even. */
825 if( !array[n-1] )
826 {
827 k1 = n - j1;
828 k2 = k1 + 1;
829 goto leave;
830 }
831
832 if( !(j1 & 1) )
833 {
834 k1 = n - j1;
835 k2 = k1+2;
836 if( k2 > n )
837 k2 = n;
838 goto leave;
839 }
840 scan:
841 jp = n - j1 - 1;
842 for (i=1; i <= jp; i++ )
843 {
844 i1 = jp + 2 - i;
845 if( array[i1-1] )
846 {
847 if( array[i1-2] )
848 {
849 k1 = i1 - 1;
850 k2 = n - j1;
851 }
852 else
853 {
854 k1 = i1 - 1;
855 k2 = n + 1 - j1;
856 }
857 goto leave;
858 }
859 }
860 k1 = 1;
861 k2 = n + 1 - m;
862 }
863 leave:
864 array[k1-1] = !array[k1-1];
865 array[k2-1] = !array[k2-1];
866}
867
868
869/* Generate a new prime number of PRIME_BITS bits and store it in
870 PRIME. If FACTOR_BITS is non-zero, one of the prime factors of
871 (prime - 1) / 2 must be FACTOR_BITS bits long. If FACTORS is
872 non-zero, allocate a new, NULL-terminated array holding the prime
873 factors and store it in FACTORS. FLAGS might be used to influence
874 the prime number generation process. */
875gcry_error_t
876gcry_prime_generate (gcry_mpi_t *prime, unsigned int prime_bits,
877 unsigned int factor_bits, gcry_mpi_t **factors,
878 gcry_prime_check_func_t cb_func, void *cb_arg,
879 gcry_random_level_t random_level,
880 unsigned int flags)
881{
882 gcry_err_code_t err = GPG_ERR_NO_ERROR;
883 gcry_mpi_t *factors_generated = NULL;
884 gcry_mpi_t prime_generated = NULL;
885 unsigned int mode = 0;
886
887 if (!prime)
888 return gpg_error (GPG_ERR_INV_ARG);
889 *prime = NULL;
890
891 if (flags & GCRY_PRIME_FLAG_SPECIAL_FACTOR)
892 mode = 1;
893
894 /* Generate. */
895 err = prime_generate_internal (mode, &prime_generated, prime_bits,
896 factor_bits, NULL,
897 factors? &factors_generated : NULL,
898 random_level, flags, 1,
899 cb_func, cb_arg);
900
901 if (! err)
902 if (cb_func)
903 {
904 /* Additional check. */
905 if ( !cb_func (cb_arg, GCRY_PRIME_CHECK_AT_FINISH, prime_generated))
906 {
907 /* Failed, deallocate resources. */
908 unsigned int i;
909
910 mpi_free (prime_generated);
911 if (factors)
912 {
913 for (i = 0; factors_generated[i]; i++)
914 mpi_free (factors_generated[i]);
915 gcry_free (factors_generated);
916 }
917 err = GPG_ERR_GENERAL;
918 }
919 }
920
921 if (! err)
922 {
923 if (factors)
924 *factors = factors_generated;
925 *prime = prime_generated;
926 }
927
928 return gcry_error (err);
929}
930
931/* Check wether the number X is prime. */
932gcry_error_t
933gcry_prime_check (gcry_mpi_t x, unsigned int flags)
934{
935 gcry_err_code_t err = GPG_ERR_NO_ERROR;
936 gcry_mpi_t val_2 = mpi_alloc_set_ui (2); /* Used by the Fermat test. */
937
938 if (! check_prime (x, val_2, NULL, NULL))
939 err = GPG_ERR_NO_PRIME;
940
941 mpi_free (val_2);
942
943 return gcry_error (err);
944}
945
946/* Find a generator for PRIME where the factorization of (prime-1) is
947 in the NULL terminated array FACTORS. Return the generator as a
948 newly allocated MPI in R_G. If START_G is not NULL, use this as s
949 atart for the search. Returns 0 on success.*/
950gcry_error_t
951gcry_prime_group_generator (gcry_mpi_t *r_g,
952 gcry_mpi_t prime, gcry_mpi_t *factors,
953 gcry_mpi_t start_g)
954{
955 gcry_mpi_t tmp = gcry_mpi_new (0);
956 gcry_mpi_t b = gcry_mpi_new (0);
957 gcry_mpi_t pmin1 = gcry_mpi_new (0);
958 gcry_mpi_t g = start_g? gcry_mpi_copy (start_g) : gcry_mpi_set_ui (NULL, 3);
959 int first = 1;
960 int i, n;
961
962 if (!factors || !r_g || !prime)
963 return gpg_error (GPG_ERR_INV_ARG);
964 *r_g = NULL;
965
966 for (n=0; factors[n]; n++)
967 ;
968 if (n < 2)
969 return gpg_error (GPG_ERR_INV_ARG);
970
971 /* Extra sanity check - usually disabled. */
972/* mpi_set (tmp, factors[0]); */
973/* for(i = 1; i < n; i++) */
974/* mpi_mul (tmp, tmp, factors[i]); */
975/* mpi_add_ui (tmp, tmp, 1); */
976/* if (mpi_cmp (prime, tmp)) */
977/* return gpg_error (GPG_ERR_INV_ARG); */
978
979 gcry_mpi_sub_ui (pmin1, prime, 1);
980 do
981 {
982 if (first)
983 first = 0;
984 else
985 gcry_mpi_add_ui (g, g, 1);
986
987 if (DBG_CIPHER)
988 {
989 log_debug ("checking g:");
990 gcry_mpi_dump (g);
991 log_debug ("\n");
992 }
993 else
994 progress('^');
995
996 for (i = 0; i < n; i++)
997 {
998 mpi_fdiv_q (tmp, pmin1, factors[i]);
999 gcry_mpi_powm (b, g, tmp, prime);
1000 if (! mpi_cmp_ui (b, 1))
1001 break;
1002 }
1003 if (DBG_CIPHER)
1004 progress('\n');
1005 }
1006 while (i < n);
1007
1008 gcry_mpi_release (tmp);
1009 gcry_mpi_release (b);
1010 gcry_mpi_release (pmin1);
1011 *r_g = g;
1012
1013 return 0;
1014}
1015
1016/* Convenience function to release the factors array. */
1017void
1018gcry_prime_release_factors (gcry_mpi_t *factors)
1019{
1020 if (factors)
1021 {
1022 int i;
1023
1024 for (i=0; factors[i]; i++)
1025 mpi_free (factors[i]);
1026 gcry_free (factors);
1027 }
1028}
diff --git a/pwmanager/libcrypt/cipher/pubkey.c b/pwmanager/libcrypt/cipher/pubkey.c
new file mode 100644
index 0000000..45e816b
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/pubkey.c
@@ -0,0 +1,2352 @@
1 /* pubkey.c -pubkey dispatcher
2 * Copyright (C) 1998,1999,2000,2002,2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <config.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <errno.h>
26#include <assert.h>
27
28#include "g10lib.h"
29#include "mpi.h"
30#include "cipher.h"
31#include "ath.h"
32
33static gcry_err_code_t pubkey_decrypt (int algo, gcry_mpi_t *result,
34 gcry_mpi_t *data, gcry_mpi_t *skey,
35 int flags);
36static gcry_err_code_t pubkey_sign (int algo, gcry_mpi_t *resarr,
37 gcry_mpi_t hash, gcry_mpi_t *skey);
38static gcry_err_code_t pubkey_verify (int algo, gcry_mpi_t hash,
39 gcry_mpi_t *data, gcry_mpi_t *pkey,
40 int (*cmp) (void *, gcry_mpi_t),
41 void *opaque);
42
43/* This is the list of the default public-key ciphers included in
44 libgcrypt. */
45static struct pubkey_table_entry
46{
47 gcry_pk_spec_t *pubkey;
48 unsigned int algorithm;
49} pubkey_table[] =
50 {
51#if USE_RSA
52 { &_gcry_pubkey_spec_rsa, GCRY_PK_RSA },
53#endif
54#if USE_ELGAMAL
55 { &_gcry_pubkey_spec_elg, GCRY_PK_ELG },
56#endif
57#if USE_DSA
58 { &_gcry_pubkey_spec_dsa, GCRY_PK_DSA },
59#endif
60 { NULL, 0 },
61 };
62
63/* List of registered ciphers. */
64static gcry_module_t pubkeys_registered;
65
66/* This is the lock protecting PUBKEYS_REGISTERED. */
67static ath_mutex_t pubkeys_registered_lock;
68
69/* Flag to check wether the default pubkeys have already been
70 registered. */
71static int default_pubkeys_registered;
72
73/* Convenient macro for registering the default digests. */
74#define REGISTER_DEFAULT_PUBKEYS \
75 do \
76 { \
77 ath_mutex_lock (&pubkeys_registered_lock); \
78 if (! default_pubkeys_registered) \
79 { \
80 gcry_pk_register_default (); \
81 default_pubkeys_registered = 1; \
82 } \
83 ath_mutex_unlock (&pubkeys_registered_lock); \
84 } \
85 while (0)
86
87/* These dummy functions are used in case a cipher implementation
88 refuses to provide it's own functions. */
89
90static gcry_err_code_t
91dummy_generate (int algorithm, unsigned int nbits, unsigned long dummy,
92 gcry_mpi_t *skey, gcry_mpi_t **retfactors)
93{
94 log_bug ("no generate() for %d\n", algorithm);
95 return GPG_ERR_PUBKEY_ALGO;
96}
97
98static gcry_err_code_t
99dummy_check_secret_key (int algorithm, gcry_mpi_t *skey)
100{
101 log_bug ("no check_secret_key() for %d\n", algorithm);
102 return GPG_ERR_PUBKEY_ALGO;
103}
104
105static gcry_err_code_t
106dummy_encrypt (int algorithm, gcry_mpi_t *resarr, gcry_mpi_t data,
107 gcry_mpi_t *pkey, int flags)
108{
109 log_bug ("no encrypt() for %d\n", algorithm);
110 return GPG_ERR_PUBKEY_ALGO;
111}
112
113static gcry_err_code_t
114dummy_decrypt (int algorithm, gcry_mpi_t *result, gcry_mpi_t *data,
115 gcry_mpi_t *skey, int flags)
116{
117 log_bug ("no decrypt() for %d\n", algorithm);
118 return GPG_ERR_PUBKEY_ALGO;
119}
120
121static gcry_err_code_t
122dummy_sign (int algorithm, gcry_mpi_t *resarr, gcry_mpi_t data,
123 gcry_mpi_t *skey)
124{
125 log_bug ("no sign() for %d\n", algorithm);
126 return GPG_ERR_PUBKEY_ALGO;
127}
128
129static gcry_err_code_t
130dummy_verify (int algorithm, gcry_mpi_t hash, gcry_mpi_t *data,
131 gcry_mpi_t *pkey,
132 int (*cmp) (void *, gcry_mpi_t), void *opaquev)
133{
134 log_bug ("no verify() for %d\n", algorithm);
135 return GPG_ERR_PUBKEY_ALGO;
136}
137
138static unsigned
139dummy_get_nbits (int algorithm, gcry_mpi_t *pkey)
140{
141 log_bug ("no get_nbits() for %d\n", algorithm);
142 return 0;
143}
144
145/* Internal function. Register all the pubkeys included in
146 PUBKEY_TABLE. Returns zero on success or an error code. */
147static void
148gcry_pk_register_default (void)
149{
150 gcry_err_code_t err = 0;
151 int i;
152
153 for (i = 0; (! err) && pubkey_table[i].pubkey; i++)
154 {
155#define pubkey_use_dummy(func) \
156 if (! pubkey_table[i].pubkey->func) \
157 pubkey_table[i].pubkey->func = dummy_##func;
158
159 pubkey_use_dummy (generate);
160 pubkey_use_dummy (check_secret_key);
161 pubkey_use_dummy (encrypt);
162 pubkey_use_dummy (decrypt);
163 pubkey_use_dummy (sign);
164 pubkey_use_dummy (verify);
165 pubkey_use_dummy (get_nbits);
166#undef pubkey_use_dummy
167 err = _gcry_module_add (&pubkeys_registered,
168 pubkey_table[i].algorithm,
169 (void *) pubkey_table[i].pubkey, NULL);
170 }
171
172 if (err)
173 BUG ();
174}
175
176/* Internal callback function. Used via _gcry_module_lookup. */
177static int
178gcry_pk_lookup_func_name (void *spec, void *data)
179{
180 gcry_pk_spec_t *pubkey = (gcry_pk_spec_t *) spec;
181 char *name = (char *) data;
182 char **aliases = pubkey->aliases;
183 int ret = stricmp (name, pubkey->name);
184
185 while (ret && *aliases)
186 ret = stricmp (name, *aliases++);
187
188 return ! ret;
189}
190
191/* Internal function. Lookup a pubkey entry by it's name. */
192static gcry_module_t
193gcry_pk_lookup_name (const char *name)
194{
195 gcry_module_t pubkey;
196
197 pubkey = _gcry_module_lookup (pubkeys_registered, (void *) name,
198 gcry_pk_lookup_func_name);
199
200 return pubkey;
201}
202
203/* Register a new pubkey module whose specification can be found in
204 PUBKEY. On success, a new algorithm ID is stored in ALGORITHM_ID
205 and a pointer representhing this module is stored in MODULE. */
206gcry_error_t
207gcry_pk_register (gcry_pk_spec_t *pubkey,
208 unsigned int *algorithm_id,
209 gcry_module_t *module)
210{
211 gcry_err_code_t err = GPG_ERR_NO_ERROR;
212 gcry_module_t mod;
213
214 ath_mutex_lock (&pubkeys_registered_lock);
215 err = _gcry_module_add (&pubkeys_registered, 0,
216 (void *) pubkey, &mod);
217 ath_mutex_unlock (&pubkeys_registered_lock);
218
219 if (! err)
220 {
221 *module = mod;
222 *algorithm_id = mod->mod_id;
223 }
224
225 return err;
226}
227
228/* Unregister the pubkey identified by ID, which must have been
229 registered with gcry_pk_register. */
230void
231gcry_pk_unregister (gcry_module_t module)
232{
233 ath_mutex_lock (&pubkeys_registered_lock);
234 _gcry_module_release (module);
235 ath_mutex_unlock (&pubkeys_registered_lock);
236}
237
238static void
239release_mpi_array (gcry_mpi_t *array)
240{
241 for (; *array; array++)
242 {
243 mpi_free(*array);
244 *array = NULL;
245 }
246}
247
248/****************
249 * Map a string to the pubkey algo
250 */
251int
252gcry_pk_map_name (const char *string)
253{
254 gcry_module_t pubkey;
255 int algorithm = 0;
256
257 if (!string)
258 return 0;
259
260 REGISTER_DEFAULT_PUBKEYS;
261
262 ath_mutex_lock (&pubkeys_registered_lock);
263 pubkey = gcry_pk_lookup_name (string);
264 if (pubkey)
265 {
266 algorithm = pubkey->mod_id;
267 _gcry_module_release (pubkey);
268 }
269 ath_mutex_unlock (&pubkeys_registered_lock);
270
271 return algorithm;
272}
273
274
275/****************
276 * Map a pubkey algo to a string
277 */
278const char *
279gcry_pk_algo_name (int algorithm)
280{
281 const char *name = NULL;
282 gcry_module_t pubkey;
283
284 REGISTER_DEFAULT_PUBKEYS;
285
286 ath_mutex_lock (&pubkeys_registered_lock);
287 pubkey = _gcry_module_lookup_id (pubkeys_registered, algorithm);
288 if (pubkey)
289 {
290 name = ((gcry_pk_spec_t *) pubkey->spec)->name;
291 _gcry_module_release (pubkey);
292 }
293 ath_mutex_unlock (&pubkeys_registered_lock);
294
295 return name;
296}
297
298
299/* A special version of gcry_pk_algo name to return the first aliased
300 name of the algorithm. This is required to adhere to the spki
301 specs where the algorithm names are lowercase. */
302const char *
303_gcry_pk_aliased_algo_name (int algorithm)
304{
305 const char *name = NULL;
306 gcry_module_t module;
307
308 REGISTER_DEFAULT_PUBKEYS;
309
310 ath_mutex_lock (&pubkeys_registered_lock);
311 module = _gcry_module_lookup_id (pubkeys_registered, algorithm);
312 if (module)
313 {
314 gcry_pk_spec_t *pubkey = (gcry_pk_spec_t *) module->spec;
315
316 name = pubkey->aliases? *pubkey->aliases : NULL;
317 if (!name || !*name)
318 name = pubkey->name;
319 _gcry_module_release (module);
320 }
321 ath_mutex_unlock (&pubkeys_registered_lock);
322
323 return name;
324}
325
326
327static void
328disable_pubkey_algo (int algorithm)
329{
330 gcry_module_t pubkey;
331
332 ath_mutex_lock (&pubkeys_registered_lock);
333 pubkey = _gcry_module_lookup_id (pubkeys_registered, algorithm);
334 if (pubkey)
335 {
336 if (! (pubkey-> flags & FLAG_MODULE_DISABLED))
337 pubkey->flags |= FLAG_MODULE_DISABLED;
338 _gcry_module_release (pubkey);
339 }
340 ath_mutex_unlock (&pubkeys_registered_lock);
341}
342
343
344/****************
345 * A USE of 0 means: don't care.
346 */
347static gcry_err_code_t
348check_pubkey_algo (int algorithm, unsigned use)
349{
350 gcry_err_code_t err = GPG_ERR_NO_ERROR;
351 gcry_pk_spec_t *pubkey;
352 gcry_module_t module;
353
354 REGISTER_DEFAULT_PUBKEYS;
355
356 ath_mutex_lock (&pubkeys_registered_lock);
357 module = _gcry_module_lookup_id (pubkeys_registered, algorithm);
358 if (module)
359 {
360 pubkey = (gcry_pk_spec_t *) module->spec;
361
362 if (((use & GCRY_PK_USAGE_SIGN)
363 && (! (pubkey->use & GCRY_PK_USAGE_SIGN)))
364 || ((use & GCRY_PK_USAGE_ENCR)
365 && (! (pubkey->use & GCRY_PK_USAGE_ENCR))))
366 err = GPG_ERR_WRONG_PUBKEY_ALGO;
367 else if (module->flags & FLAG_MODULE_DISABLED)
368 err = GPG_ERR_PUBKEY_ALGO;
369 _gcry_module_release (module);
370 }
371 else
372 err = GPG_ERR_PUBKEY_ALGO;
373 ath_mutex_unlock (&pubkeys_registered_lock);
374
375 return err;
376}
377
378
379/****************
380 * Return the number of public key material numbers
381 */
382static int
383pubkey_get_npkey (int algorithm)
384{
385 gcry_module_t pubkey;
386 int npkey = 0;
387
388 REGISTER_DEFAULT_PUBKEYS;
389
390 ath_mutex_lock (&pubkeys_registered_lock);
391 pubkey = _gcry_module_lookup_id (pubkeys_registered, algorithm);
392 if (pubkey)
393 {
394 npkey = strlen (((gcry_pk_spec_t *) pubkey->spec)->elements_pkey);
395 _gcry_module_release (pubkey);
396 }
397 ath_mutex_unlock (&pubkeys_registered_lock);
398
399 return npkey;
400}
401
402/****************
403 * Return the number of secret key material numbers
404 */
405static int
406pubkey_get_nskey (int algorithm)
407{
408 gcry_module_t pubkey;
409 int nskey = 0;
410
411 REGISTER_DEFAULT_PUBKEYS;
412
413 ath_mutex_lock (&pubkeys_registered_lock);
414 pubkey = _gcry_module_lookup_id (pubkeys_registered, algorithm);
415 if (pubkey)
416 {
417 nskey = strlen (((gcry_pk_spec_t *) pubkey->spec)->elements_skey);
418 _gcry_module_release (pubkey);
419 }
420 ath_mutex_unlock (&pubkeys_registered_lock);
421
422 return nskey;
423}
424
425/****************
426 * Return the number of signature material numbers
427 */
428static int
429pubkey_get_nsig (int algorithm)
430{
431 gcry_module_t pubkey;
432 int nsig = 0;
433
434 REGISTER_DEFAULT_PUBKEYS;
435
436 ath_mutex_lock (&pubkeys_registered_lock);
437 pubkey = _gcry_module_lookup_id (pubkeys_registered, algorithm);
438 if (pubkey)
439 {
440 nsig = strlen (((gcry_pk_spec_t *) pubkey->spec)->elements_sig);
441 _gcry_module_release (pubkey);
442 }
443 ath_mutex_unlock (&pubkeys_registered_lock);
444
445 return nsig;
446}
447
448/****************
449 * Return the number of encryption material numbers
450 */
451static int
452pubkey_get_nenc (int algorithm)
453{
454 gcry_module_t pubkey;
455 int nenc = 0;
456
457 REGISTER_DEFAULT_PUBKEYS;
458
459 ath_mutex_lock (&pubkeys_registered_lock);
460 pubkey = _gcry_module_lookup_id (pubkeys_registered, algorithm);
461 if (pubkey)
462 {
463 nenc = strlen (((gcry_pk_spec_t *) pubkey->spec)->elements_enc);
464 _gcry_module_release (pubkey);
465 }
466 ath_mutex_unlock (&pubkeys_registered_lock);
467
468 return nenc;
469}
470
471
472static gcry_err_code_t
473pubkey_generate (int algorithm, unsigned int nbits, unsigned long use_e,
474 gcry_mpi_t *skey, gcry_mpi_t **retfactors)
475{
476 gcry_err_code_t err = GPG_ERR_PUBKEY_ALGO;
477 gcry_module_t pubkey;
478
479 REGISTER_DEFAULT_PUBKEYS;
480
481 ath_mutex_lock (&pubkeys_registered_lock);
482 pubkey = _gcry_module_lookup_id (pubkeys_registered, algorithm);
483 if (pubkey)
484 {
485 err = ((gcry_pk_spec_t *) pubkey->spec)->generate
486 (algorithm, nbits, use_e, skey, retfactors);
487 _gcry_module_release (pubkey);
488 }
489 ath_mutex_unlock (&pubkeys_registered_lock);
490
491 return err;
492}
493
494static gcry_err_code_t
495pubkey_check_secret_key (int algorithm, gcry_mpi_t *skey)
496{
497 gcry_err_code_t err = GPG_ERR_PUBKEY_ALGO;
498 gcry_module_t pubkey;
499
500 REGISTER_DEFAULT_PUBKEYS;
501
502 ath_mutex_lock (&pubkeys_registered_lock);
503 pubkey = _gcry_module_lookup_id (pubkeys_registered, algorithm);
504 if (pubkey)
505 {
506 err = ((gcry_pk_spec_t *) pubkey->spec)->check_secret_key
507 (algorithm, skey);
508 _gcry_module_release (pubkey);
509 }
510 ath_mutex_unlock (&pubkeys_registered_lock);
511
512 return err;
513}
514
515
516/****************
517 * This is the interface to the public key encryption. Encrypt DATA
518 * with PKEY and put it into RESARR which should be an array of MPIs
519 * of size PUBKEY_MAX_NENC (or less if the algorithm allows this -
520 * check with pubkey_get_nenc() )
521 */
522static gcry_err_code_t
523pubkey_encrypt (int algorithm, gcry_mpi_t *resarr, gcry_mpi_t data,
524 gcry_mpi_t *pkey, int flags)
525{
526 gcry_pk_spec_t *pubkey;
527 gcry_module_t module;
528 gcry_err_code_t rc;
529 int i;
530
531 if (DBG_CIPHER)
532 {
533 log_debug ("pubkey_encrypt: algo=%d\n", algorithm);
534 for(i = 0; i < pubkey_get_npkey (algorithm); i++)
535 log_mpidump (" pkey:", pkey[i]);
536 log_mpidump (" data:", data);
537 }
538
539 ath_mutex_lock (&pubkeys_registered_lock);
540 module = _gcry_module_lookup_id (pubkeys_registered, algorithm);
541 if (module)
542 {
543 pubkey = (gcry_pk_spec_t *) module->spec;
544 rc = pubkey->encrypt (algorithm, resarr, data, pkey, flags);
545 _gcry_module_release (module);
546 goto ready;
547 }
548 rc = GPG_ERR_PUBKEY_ALGO;
549
550 ready:
551 ath_mutex_unlock (&pubkeys_registered_lock);
552
553 if (!rc && DBG_CIPHER)
554 {
555 for(i = 0; i < pubkey_get_nenc (algorithm); i++)
556 log_mpidump(" encr:", resarr[i] );
557 }
558 return rc;
559}
560
561
562/****************
563 * This is the interface to the public key decryption.
564 * ALGO gives the algorithm to use and this implicitly determines
565 * the size of the arrays.
566 * result is a pointer to a mpi variable which will receive a
567 * newly allocated mpi or NULL in case of an error.
568 */
569static gcry_err_code_t
570pubkey_decrypt (int algorithm, gcry_mpi_t *result, gcry_mpi_t *data,
571 gcry_mpi_t *skey, int flags)
572{
573 gcry_pk_spec_t *pubkey;
574 gcry_module_t module;
575 gcry_err_code_t rc;
576 int i;
577
578 *result = NULL; /* so the caller can always do a mpi_free */
579 if (DBG_CIPHER)
580 {
581 log_debug ("pubkey_decrypt: algo=%d\n", algorithm);
582 for(i = 0; i < pubkey_get_nskey (algorithm); i++)
583 log_mpidump (" skey:", skey[i]);
584 for(i = 0; i < pubkey_get_nenc (algorithm); i++)
585 log_mpidump (" data:", data[i]);
586 }
587
588 ath_mutex_lock (&pubkeys_registered_lock);
589 module = _gcry_module_lookup_id (pubkeys_registered, algorithm);
590 if (module)
591 {
592 pubkey = (gcry_pk_spec_t *) module->spec;
593 rc = pubkey->decrypt (algorithm, result, data, skey, flags);
594 _gcry_module_release (module);
595 goto ready;
596 }
597
598 rc = GPG_ERR_PUBKEY_ALGO;
599
600 ready:
601 ath_mutex_unlock (&pubkeys_registered_lock);
602
603 if (! rc && DBG_CIPHER)
604 log_mpidump (" plain:", *result);
605
606 return rc;
607}
608
609
610/****************
611 * This is the interface to the public key signing.
612 * Sign data with skey and put the result into resarr which
613 * should be an array of MPIs of size PUBKEY_MAX_NSIG (or less if the
614 * algorithm allows this - check with pubkey_get_nsig() )
615 */
616static gcry_err_code_t
617pubkey_sign (int algorithm, gcry_mpi_t *resarr, gcry_mpi_t data,
618 gcry_mpi_t *skey)
619{
620 gcry_pk_spec_t *pubkey;
621 gcry_module_t module;
622 gcry_err_code_t rc;
623 int i;
624
625 if (DBG_CIPHER)
626 {
627 log_debug ("pubkey_sign: algo=%d\n", algorithm);
628 for(i = 0; i < pubkey_get_nskey (algorithm); i++)
629 log_mpidump (" skey:", skey[i]);
630 log_mpidump(" data:", data );
631 }
632
633 ath_mutex_lock (&pubkeys_registered_lock);
634 module = _gcry_module_lookup_id (pubkeys_registered, algorithm);
635 if (module)
636 {
637 pubkey = (gcry_pk_spec_t *) module->spec;
638 rc = pubkey->sign (algorithm, resarr, data, skey);
639 _gcry_module_release (module);
640 goto ready;
641 }
642
643 rc = GPG_ERR_PUBKEY_ALGO;
644
645 ready:
646 ath_mutex_unlock (&pubkeys_registered_lock);
647
648 if (! rc && DBG_CIPHER)
649 for (i = 0; i < pubkey_get_nsig (algorithm); i++)
650 log_mpidump (" sig:", resarr[i]);
651
652 return rc;
653}
654
655/****************
656 * Verify a public key signature.
657 * Return 0 if the signature is good
658 */
659static gcry_err_code_t
660pubkey_verify (int algorithm, gcry_mpi_t hash, gcry_mpi_t *data,
661 gcry_mpi_t *pkey,
662 int (*cmp)(void *, gcry_mpi_t), void *opaquev)
663{
664 gcry_pk_spec_t *pubkey;
665 gcry_module_t module;
666 gcry_err_code_t rc;
667 int i;
668
669 if (DBG_CIPHER)
670 {
671 log_debug ("pubkey_verify: algo=%d\n", algorithm);
672 for (i = 0; i < pubkey_get_npkey (algorithm); i++)
673 log_mpidump (" pkey:", pkey[i]);
674 for (i = 0; i < pubkey_get_nsig (algorithm); i++)
675 log_mpidump (" sig:", data[i]);
676 log_mpidump (" hash:", hash);
677 }
678
679 ath_mutex_lock (&pubkeys_registered_lock);
680 module = _gcry_module_lookup_id (pubkeys_registered, algorithm);
681 if (module)
682 {
683 pubkey = (gcry_pk_spec_t *) module->spec;
684 rc = pubkey->verify (algorithm, hash, data, pkey, cmp, opaquev);
685 _gcry_module_release (module);
686 goto ready;
687 }
688
689 rc = GPG_ERR_PUBKEY_ALGO;
690
691 ready:
692 ath_mutex_unlock (&pubkeys_registered_lock);
693 return rc;
694}
695
696
697/* Internal function. */
698static gcry_err_code_t
699sexp_elements_extract (gcry_sexp_t key_sexp, const char *element_names,
700 gcry_mpi_t *elements)
701{
702 gcry_err_code_t err = GPG_ERR_NO_ERROR;
703 int i, idx;
704 const char *name;
705 gcry_sexp_t list;
706
707 for (name = element_names, idx = 0; *name && !err; name++, idx++)
708 {
709 list = gcry_sexp_find_token (key_sexp, name, 1);
710 if (! list)
711 err = GPG_ERR_NO_OBJ;
712 else
713 {
714 elements[idx] = gcry_sexp_nth_mpi (list, 1, GCRYMPI_FMT_USG);
715 gcry_sexp_release (list);
716 if (! elements[idx])
717 err = GPG_ERR_INV_OBJ;
718 }
719 }
720
721 if (err)
722 {
723 for (i = 0; i < idx; i++)
724 if (elements[i])
725 gcry_free (elements[i]);
726 }
727 return err;
728}
729
730/****************
731 * Convert a S-Exp with either a private or a public key to our
732 * internal format. Currently we do only support the following
733 * algorithms:
734 * dsa
735 * rsa
736 * openpgp-dsa
737 * openpgp-rsa
738 * openpgp-elg
739 * openpgp-elg-sig
740 * Provide a SE with the first element be either "private-key" or
741 * or "public-key". It is followed by a list with its first element
742 * be one of the above algorithm identifiers and the remaning
743 * elements are pairs with parameter-id and value.
744 * NOTE: we look through the list to find a list beginning with
745 * "private-key" or "public-key" - the first one found is used.
746 *
747 * FIXME: Allow for encrypted secret keys here.
748 *
749 * Returns: A pointer to an allocated array of MPIs if the return value is
750 * zero; the caller has to release this array.
751 *
752 * Example of a DSA public key:
753 * (private-key
754 * (dsa
755 *(p <mpi>)
756 *(g <mpi>)
757 *(y <mpi>)
758 *(x <mpi>)
759 * )
760 * )
761 * The <mpi> are expected to be in GCRYMPI_FMT_USG
762 */
763static gcry_err_code_t
764sexp_to_key (gcry_sexp_t sexp, int want_private, gcry_mpi_t **retarray,
765 gcry_module_t *retalgo)
766{
767 gcry_sexp_t list, l2;
768 const char *name;
769 size_t n;
770 const char *elems;
771 gcry_mpi_t *array;
772 gcry_err_code_t err = GPG_ERR_NO_ERROR;
773 gcry_module_t module;
774 gcry_pk_spec_t *pubkey;
775
776 /* check that the first element is valid */
777 list = gcry_sexp_find_token( sexp, want_private? "private-key"
778 :"public-key", 0 );
779 if( !list )
780 return GPG_ERR_INV_OBJ; /* Does not contain a public-
781 or private-key object */
782 l2 = gcry_sexp_cadr( list );
783 gcry_sexp_release ( list );
784 list = l2;
785 name = gcry_sexp_nth_data( list, 0, &n );
786 if( !name ) {
787 gcry_sexp_release ( list );
788 return GPG_ERR_INV_OBJ; /* invalid structure of object */
789 }
790
791 {
792 char *name_terminated = gcry_xmalloc (n + 1);
793 memcpy (name_terminated, name, n);
794 name_terminated[n] = 0;
795
796 ath_mutex_lock (&pubkeys_registered_lock);
797 module = gcry_pk_lookup_name (name_terminated);
798 ath_mutex_unlock (&pubkeys_registered_lock);
799
800 gcry_free (name_terminated);
801 }
802
803 if (! module)
804 {
805 gcry_sexp_release (list);
806 return GPG_ERR_PUBKEY_ALGO; /* unknown algorithm */
807 }
808 else
809 pubkey = (gcry_pk_spec_t *) module->spec;
810
811 elems = want_private ? pubkey->elements_skey : pubkey->elements_pkey;
812 array = gcry_calloc (strlen (elems) + 1, sizeof (*array));
813 if (! array)
814 err = gpg_err_code_from_errno (errno);
815 if (! err)
816 err = sexp_elements_extract (list, elems, array);
817
818 if (list)
819 gcry_sexp_release (list);
820
821 if (err)
822 {
823 if (array)
824 gcry_free (array);
825
826 ath_mutex_lock (&pubkeys_registered_lock);
827 _gcry_module_release (module);
828 ath_mutex_unlock (&pubkeys_registered_lock);
829 }
830 else
831 {
832 *retarray = array;
833 *retalgo = module;
834 }
835
836 return err;
837}
838
839static gcry_err_code_t
840sexp_to_sig (gcry_sexp_t sexp, gcry_mpi_t **retarray,
841 gcry_module_t *retalgo)
842{
843 gcry_sexp_t list, l2;
844 const char *name;
845 size_t n;
846 const char *elems;
847 gcry_mpi_t *array;
848 gcry_err_code_t err = GPG_ERR_NO_ERROR;
849 gcry_module_t module;
850 gcry_pk_spec_t *pubkey;
851
852 /* check that the first element is valid */
853 list = gcry_sexp_find_token( sexp, "sig-val" , 0 );
854 if( !list )
855 return GPG_ERR_INV_OBJ; /* Does not contain a signature value object */
856 l2 = gcry_sexp_nth (list, 1);
857 if(! l2)
858 {
859 gcry_sexp_release (list);
860 return GPG_ERR_NO_OBJ; /* no cadr for the sig object */
861 }
862 name = gcry_sexp_nth_data( l2, 0, &n );
863 if( !name ) {
864 gcry_sexp_release ( list );
865 gcry_sexp_release ( l2 );
866 return GPG_ERR_INV_OBJ; /* invalid structure of object */
867 }
868 else if (n == 5 && (! memcmp (name, "flags", 5))) {
869 /* Skip flags, since they are not used but just here for the
870 sake of consistent S-expressions. */
871 gcry_sexp_release (l2);
872 l2 = gcry_sexp_nth (list, 2);
873 if (! l2)
874 {
875 gcry_sexp_release (list);
876 return GPG_ERR_INV_OBJ;
877 }
878 name = gcry_sexp_nth_data (l2, 0, &n);
879 }
880
881 {
882 char *name_terminated = gcry_xmalloc (n + 1);
883 memcpy (name_terminated, name, n);
884 name_terminated[n] = 0;
885
886 ath_mutex_lock (&pubkeys_registered_lock);
887 module = gcry_pk_lookup_name (name_terminated);
888 ath_mutex_unlock (&pubkeys_registered_lock);
889
890 gcry_free (name_terminated);
891 }
892
893 if (! module)
894 {
895 gcry_sexp_release (l2);
896 gcry_sexp_release (list);
897 return GPG_ERR_PUBKEY_ALGO; /* unknown algorithm */
898 }
899 else
900 pubkey = (gcry_pk_spec_t *) module->spec;
901
902 elems = pubkey->elements_sig;
903 array = gcry_calloc (strlen (elems) + 1 , sizeof (*array));
904 if (! array)
905 err = gpg_err_code_from_errno (errno);
906
907 if (! err)
908 err = sexp_elements_extract (list, elems, array);
909
910 gcry_sexp_release (l2);
911 gcry_sexp_release (list);
912
913 if (err)
914 {
915 ath_mutex_lock (&pubkeys_registered_lock);
916 _gcry_module_release (module);
917 ath_mutex_unlock (&pubkeys_registered_lock);
918
919 if (array)
920 gcry_free (array);
921 }
922 else
923 {
924 *retarray = array;
925 *retalgo = module;
926 }
927
928 return err;
929}
930
931
932/****************
933 * Take sexp and return an array of MPI as used for our internal decrypt
934 * function.
935 * s_data = (enc-val
936 * [(flags [pkcs1])
937 * (<algo>
938 * (<param_name1> <mpi>)
939 * ...
940 * (<param_namen> <mpi>)
941 * ))
942 * RET_MODERN is set to true when at least an empty flags list has been found.
943 */
944static gcry_err_code_t
945sexp_to_enc (gcry_sexp_t sexp, gcry_mpi_t **retarray, gcry_module_t *retalgo,
946 int *ret_modern, int *ret_want_pkcs1, int *flags)
947{
948 gcry_sexp_t list = NULL, l2 = NULL;
949 gcry_pk_spec_t *pubkey = NULL;
950 gcry_module_t module = NULL;
951 const char *name;
952 size_t n;
953 int parsed_flags = 0;
954 const char *elems;
955 gcry_mpi_t *array = NULL;
956 gcry_err_code_t err = GPG_ERR_NO_ERROR;
957
958 *ret_want_pkcs1 = 0;
959 *ret_modern = 0;
960
961 /* check that the first element is valid */
962 list = gcry_sexp_find_token (sexp, "enc-val" , 0);
963 if (! list)
964 {
965 err = GPG_ERR_INV_OBJ; /* Does not contain an encrypted value object */
966 goto leave;
967 }
968
969 l2 = gcry_sexp_nth (list, 1);
970 if (! l2)
971 {
972 err = GPG_ERR_NO_OBJ; /* no cdr for the data object */
973 goto leave;
974 }
975
976 /* Extract identifier of sublist. */
977 name = gcry_sexp_nth_data (l2, 0, &n);
978 if (! name)
979 {
980 err = GPG_ERR_INV_OBJ; /* invalid structure of object */
981 goto leave;
982 }
983
984 if ((n == 5) && (! memcmp (name, "flags", 5)))
985 {
986 /* There is a flags element - process it */
987 const char *s;
988 int i;
989
990 *ret_modern = 1;
991 for (i = gcry_sexp_length (l2) - 1; i > 0; i--)
992 {
993 s = gcry_sexp_nth_data (l2, i, &n);
994 if (! s)
995 ; /* not a data element - ignore */
996 else if (n == 3 && ! memcmp (s, "raw", 3))
997 ; /* just a dummy because it is the default */
998 else if (n == 5 && ! memcmp (s, "pkcs1", 5))
999 *ret_want_pkcs1 = 1;
1000 else if (n == 11 && ! memcmp (s, "no-blinding", 11))
1001 parsed_flags |= PUBKEY_FLAG_NO_BLINDING;
1002 else
1003 {
1004 err = GPG_ERR_INV_FLAG;
1005 goto leave;
1006 }
1007 }
1008
1009 /* Get the next which has the actual data */
1010 gcry_sexp_release (l2);
1011 l2 = gcry_sexp_nth (list, 2);
1012 if (! l2)
1013 {
1014 err = GPG_ERR_NO_OBJ; /* no cdr for the data object */
1015 goto leave;
1016 }
1017
1018 /* Extract sublist identifier. */
1019 name = gcry_sexp_nth_data (l2, 0, &n);
1020 if (! name)
1021 {
1022 err = GPG_ERR_INV_OBJ; /* invalid structure of object */
1023 goto leave;
1024 }
1025
1026 gcry_sexp_release (list);
1027 list = l2;
1028 l2 = NULL;
1029 }
1030
1031 {
1032 char *name_terminated = gcry_xmalloc (n + 1);
1033 memcpy (name_terminated, name, n);
1034 name_terminated[n] = 0;
1035
1036 ath_mutex_lock (&pubkeys_registered_lock);
1037 module = gcry_pk_lookup_name (name_terminated);
1038 ath_mutex_unlock (&pubkeys_registered_lock);
1039
1040 gcry_free (name_terminated);
1041
1042 if (! module)
1043 {
1044 err = GPG_ERR_PUBKEY_ALGO; /* unknown algorithm */
1045 goto leave;
1046 }
1047 pubkey = (gcry_pk_spec_t *) module->spec;
1048 }
1049
1050 elems = pubkey->elements_enc;
1051 array = gcry_calloc (strlen (elems) + 1, sizeof (*array));
1052 if (! array)
1053 {
1054 err = gpg_err_code_from_errno (errno);
1055 goto leave;
1056 }
1057
1058 err = sexp_elements_extract (list, elems, array);
1059
1060 leave:
1061 if (list)
1062 gcry_sexp_release (list);
1063 if (l2)
1064 gcry_sexp_release (l2);
1065
1066 if (err)
1067 {
1068 ath_mutex_lock (&pubkeys_registered_lock);
1069 _gcry_module_release (module);
1070 ath_mutex_unlock (&pubkeys_registered_lock);
1071 if (array)
1072 gcry_free (array);
1073 }
1074 else
1075 {
1076 *retarray = array;
1077 *retalgo = module;
1078 *flags = parsed_flags;
1079 }
1080
1081 return err;
1082}
1083
1084/* Take the hash value and convert into an MPI, suitable for for
1085 passing to the low level functions. We currently support the
1086 old style way of passing just a MPI and the modern interface which
1087 allows to pass flags so that we can choose between raw and pkcs1
1088 padding - may be more padding options later.
1089
1090 (<mpi>)
1091 or
1092 (data
1093 [(flags [pkcs1])]
1094 [(hash <algo> <value>)]
1095 [(value <text>)]
1096 )
1097
1098 Either the VALUE or the HASH element must be present for use
1099 with signatures. VALUE is used for encryption.
1100
1101 NBITS is the length of the key in bits.
1102
1103*/
1104static gcry_err_code_t
1105sexp_data_to_mpi (gcry_sexp_t input, unsigned int nbits, gcry_mpi_t *ret_mpi,
1106 int for_encryption, int *flags)
1107{
1108 gcry_err_code_t rc = 0;
1109 gcry_sexp_t ldata, lhash, lvalue;
1110 int i;
1111 size_t n;
1112 const char *s;
1113 int is_raw = 0, is_pkcs1 = 0, unknown_flag=0;
1114 int parsed_flags = 0, dummy_flags;
1115
1116 if (! flags)
1117 flags = &dummy_flags;
1118
1119 *ret_mpi = NULL;
1120 ldata = gcry_sexp_find_token (input, "data", 0);
1121 if (!ldata)
1122 { /* assume old style */
1123 *ret_mpi = gcry_sexp_nth_mpi (input, 0, 0);
1124 return *ret_mpi ? GPG_ERR_NO_ERROR : GPG_ERR_INV_OBJ;
1125 }
1126
1127 /* see whether there is a flags object */
1128 {
1129 gcry_sexp_t lflags = gcry_sexp_find_token (ldata, "flags", 0);
1130 if (lflags)
1131 { /* parse the flags list. */
1132 for (i=gcry_sexp_length (lflags)-1; i > 0; i--)
1133 {
1134 s = gcry_sexp_nth_data (lflags, i, &n);
1135 if (!s)
1136 ; /* not a data element*/
1137 else if ( n == 3 && !memcmp (s, "raw", 3))
1138 is_raw = 1;
1139 else if ( n == 5 && !memcmp (s, "pkcs1", 5))
1140 is_pkcs1 = 1;
1141 else if (n == 11 && ! memcmp (s, "no-blinding", 11))
1142 parsed_flags |= PUBKEY_FLAG_NO_BLINDING;
1143 else
1144 unknown_flag = 1;
1145 }
1146 gcry_sexp_release (lflags);
1147 }
1148 }
1149
1150 if (!is_pkcs1 && !is_raw)
1151 is_raw = 1; /* default to raw */
1152
1153 /* Get HASH or MPI */
1154 lhash = gcry_sexp_find_token (ldata, "hash", 0);
1155 lvalue = lhash? NULL : gcry_sexp_find_token (ldata, "value", 0);
1156
1157 if (!(!lhash ^ !lvalue))
1158 rc = GPG_ERR_INV_OBJ; /* none or both given */
1159 else if (unknown_flag)
1160 rc = GPG_ERR_INV_FLAG;
1161 else if (is_raw && is_pkcs1 && !for_encryption)
1162 rc = GPG_ERR_CONFLICT;
1163 else if (is_raw && lvalue)
1164 {
1165 *ret_mpi = gcry_sexp_nth_mpi (lvalue, 1, 0);
1166 if (!*ret_mpi)
1167 rc = GPG_ERR_INV_OBJ;
1168 }
1169 else if (is_pkcs1 && lvalue && for_encryption)
1170 { /* Create pkcs#1 block type 2 padding. */
1171 unsigned char *frame = NULL;
1172 size_t nframe = (nbits+7) / 8;
1173 const void * value;
1174 size_t valuelen;
1175 unsigned char *p;
1176
1177 if ( !(value=gcry_sexp_nth_data (lvalue, 1, &valuelen)) || !valuelen )
1178 rc = GPG_ERR_INV_OBJ;
1179 else if (valuelen + 7 > nframe || !nframe)
1180 {
1181 /* Can't encode a VALUELEN value in a NFRAME bytes frame. */
1182 rc = GPG_ERR_TOO_SHORT; /* the key is too short */
1183 }
1184 else if ( !(frame = gcry_malloc_secure (nframe)))
1185 rc = gpg_err_code_from_errno (errno);
1186 else
1187 {
1188 n = 0;
1189 frame[n++] = 0;
1190 frame[n++] = 2; /* block type */
1191 i = nframe - 3 - valuelen;
1192 assert (i > 0);
1193 p = gcry_random_bytes_secure (i, GCRY_STRONG_RANDOM);
1194 /* Replace zero bytes by new values. */
1195 for (;;)
1196 {
1197 int j, k;
1198 unsigned char *pp;
1199
1200 /* Count the zero bytes. */
1201 for (j=k=0; j < i; j++)
1202 {
1203 if (!p[j])
1204 k++;
1205 }
1206 if (!k)
1207 break; /* Okay: no (more) zero bytes. */
1208
1209 k += k/128; /* Better get some more. */
1210 pp = gcry_random_bytes_secure (k, GCRY_STRONG_RANDOM);
1211 for (j=0; j < i && k; j++)
1212 {
1213 if (!p[j])
1214 p[j] = pp[--k];
1215 }
1216 gcry_free (pp);
1217 }
1218 memcpy (frame+n, p, i);
1219 n += i;
1220 gcry_free (p);
1221
1222 frame[n++] = 0;
1223 memcpy (frame+n, value, valuelen);
1224 n += valuelen;
1225 assert (n == nframe);
1226
1227 /* FIXME, error checking? */
1228 gcry_mpi_scan (ret_mpi, GCRYMPI_FMT_USG, frame, n, &nframe);
1229 }
1230
1231 gcry_free(frame);
1232 }
1233 else if (is_pkcs1 && lhash && !for_encryption)
1234 { /* Create pkcs#1 block type 1 padding. */
1235 if (gcry_sexp_length (lhash) != 3)
1236 rc = GPG_ERR_INV_OBJ;
1237 else if ( !(s=gcry_sexp_nth_data (lhash, 1, &n)) || !n )
1238 rc = GPG_ERR_INV_OBJ;
1239 else
1240 {
1241 static struct { const char *name; int algo; } hashnames[] =
1242 { { "sha1", GCRY_MD_SHA1 },
1243 { "md5", GCRY_MD_MD5 },
1244 { "rmd160", GCRY_MD_RMD160 },
1245 { "sha256", GCRY_MD_SHA256 },
1246 { "sha384", GCRY_MD_SHA384 },
1247 { "sha512", GCRY_MD_SHA512 },
1248 { "md2", GCRY_MD_MD2 },
1249 { "md4", GCRY_MD_MD4 },
1250 { "tiger", GCRY_MD_TIGER },
1251 { "haval", GCRY_MD_HAVAL },
1252 { NULL }
1253 };
1254 int algo;
1255 byte asn[100];
1256 byte *frame = NULL;
1257 size_t nframe = (nbits+7) / 8;
1258 const void * value;
1259 size_t valuelen;
1260 size_t asnlen, dlen;
1261
1262 for (i=0; hashnames[i].name; i++)
1263 {
1264 if ( strlen (hashnames[i].name) == n
1265 && !memcmp (hashnames[i].name, s, n))
1266 break;
1267 }
1268
1269 algo = hashnames[i].algo;
1270 asnlen = DIM(asn);
1271 dlen = gcry_md_get_algo_dlen (algo);
1272
1273 if (!hashnames[i].name)
1274 rc = GPG_ERR_DIGEST_ALGO;
1275 else if ( !(value=gcry_sexp_nth_data (lhash, 2, &valuelen))
1276 || !valuelen )
1277 rc = GPG_ERR_INV_OBJ;
1278 else if (gcry_md_algo_info (algo, GCRYCTL_GET_ASNOID, asn, &asnlen))
1279 {
1280 /* We don't have yet all of the above algorithms. */
1281 rc = GPG_ERR_NOT_IMPLEMENTED;
1282 }
1283 else if ( valuelen != dlen )
1284 {
1285 /* Hash value does not match the length of digest for
1286 the given algorithm. */
1287 rc = GPG_ERR_CONFLICT;
1288 }
1289 else if( !dlen || dlen + asnlen + 4 > nframe)
1290 {
1291 /* Can't encode an DLEN byte digest MD into a NFRAME
1292 byte frame. */
1293 rc = GPG_ERR_TOO_SHORT;
1294 }
1295 else if ( !(frame = gcry_malloc (nframe)) )
1296 rc = gpg_err_code_from_errno (errno);
1297 else
1298 { /* Assemble the pkcs#1 block type 1. */
1299 n = 0;
1300 frame[n++] = 0;
1301 frame[n++] = 1; /* block type */
1302 i = nframe - valuelen - asnlen - 3 ;
1303 assert (i > 1);
1304 memset (frame+n, 0xff, i );
1305 n += i;
1306 frame[n++] = 0;
1307 memcpy (frame+n, asn, asnlen);
1308 n += asnlen;
1309 memcpy (frame+n, value, valuelen );
1310 n += valuelen;
1311 assert (n == nframe);
1312
1313 /* convert it into an MPI, FIXME: error checking? */
1314 gcry_mpi_scan (ret_mpi, GCRYMPI_FMT_USG, frame, n, &nframe);
1315 }
1316
1317 gcry_free (frame);
1318 }
1319 }
1320 else
1321 rc = GPG_ERR_CONFLICT;
1322
1323 gcry_sexp_release (ldata);
1324 gcry_sexp_release (lhash);
1325 gcry_sexp_release (lvalue);
1326
1327 if (!rc)
1328 *flags = parsed_flags;
1329
1330 return rc;
1331}
1332
1333
1334/*
1335 Do a PK encrypt operation
1336
1337 Caller has to provide a public key as the SEXP pkey and data as a
1338 SEXP with just one MPI in it. Alternativly S_DATA might be a
1339 complex S-Expression, similar to the one used for signature
1340 verification. This provides a flag which allows to handle PKCS#1
1341 block type 2 padding. The function returns a a sexp which may be
1342 passed to to pk_decrypt.
1343
1344 Returns: 0 or an errorcode.
1345
1346 s_data = See comment for sexp_data_to_mpi
1347 s_pkey = <key-as-defined-in-sexp_to_key>
1348 r_ciph = (enc-val
1349 (<algo>
1350 (<param_name1> <mpi>)
1351 ...
1352 (<param_namen> <mpi>)
1353 ))
1354
1355*/
1356gcry_error_t
1357gcry_pk_encrypt (gcry_sexp_t *r_ciph, gcry_sexp_t s_data, gcry_sexp_t s_pkey)
1358{
1359 gcry_mpi_t *pkey = NULL, data = NULL, *ciph = NULL;
1360 const char *algo_name, *algo_elems;
1361 int flags;
1362 gcry_err_code_t rc;
1363 gcry_pk_spec_t *pubkey = NULL;
1364 gcry_module_t module = NULL;
1365
1366 REGISTER_DEFAULT_PUBKEYS;
1367
1368 *r_ciph = NULL;
1369 /* get the key */
1370 rc = sexp_to_key (s_pkey, 0, &pkey, &module);
1371 if (rc)
1372 goto leave;
1373
1374 assert (module);
1375 pubkey = (gcry_pk_spec_t *) module->spec;
1376
1377 /* If aliases for the algorithm name exists, take the first one
1378 instead of the regular name to adhere to SPKI conventions. We
1379 assume that the first alias name is the lowercase version of the
1380 regular one. This change is required for compatibility with
1381 1.1.12 generated S-expressions. */
1382 algo_name = pubkey->aliases? *pubkey->aliases : NULL;
1383 if (!algo_name || !*algo_name)
1384 algo_name = pubkey->name;
1385
1386 algo_elems = pubkey->elements_enc;
1387
1388 /* Get the stuff we want to encrypt. */
1389 rc = sexp_data_to_mpi (s_data, gcry_pk_get_nbits (s_pkey), &data, 1,
1390 &flags);
1391 if (rc)
1392 goto leave;
1393
1394 /* Now we can encrypt DATA to CIPH. */
1395 ciph = gcry_xcalloc (strlen (algo_elems) + 1, sizeof (*ciph));
1396 rc = pubkey_encrypt (module->mod_id, ciph, data, pkey, flags);
1397 mpi_free (data);
1398 data = NULL;
1399 if (rc)
1400 goto leave;
1401
1402 /* We did it. Now build the return list */
1403 {
1404 char *string, *p;
1405 int i;
1406 size_t nelem = strlen (algo_elems);
1407 size_t needed = 19 + strlen (algo_name) + (nelem * 5);
1408 void **arg_list;
1409
1410 /* Build the string. */
1411 string = p = gcry_xmalloc (needed);
1412 p = stpcpy ( p, "(enc-val(" );
1413 p = stpcpy ( p, algo_name );
1414 for (i=0; algo_elems[i]; i++ )
1415 {
1416 *p++ = '(';
1417 *p++ = algo_elems[i];
1418 p = stpcpy ( p, "%m)" );
1419 }
1420 strcpy ( p, "))" );
1421
1422 /* And now the ugly part: We don't have a function to pass an
1423 * array to a format string, so we have to do it this way :-(. */
1424 /* FIXME: There is now such a format spefier, so we can could
1425 change the code to be more clear. */
1426 arg_list = malloc (nelem * sizeof *arg_list);
1427 if (!arg_list)
1428 {
1429 rc = gpg_err_code_from_errno (errno);
1430 goto leave;
1431 }
1432
1433 for (i = 0; i < nelem; i++)
1434 arg_list[i] = ciph + i;
1435
1436 rc = gcry_sexp_build_array (r_ciph, NULL, string, arg_list);
1437 free (arg_list);
1438 if (rc)
1439 BUG ();
1440 gcry_free (string);
1441 }
1442
1443 leave:
1444 if (pkey)
1445 {
1446 release_mpi_array (pkey);
1447 gcry_free (pkey);
1448 }
1449
1450 if (ciph)
1451 {
1452 release_mpi_array (ciph);
1453 gcry_free (ciph);
1454 }
1455
1456 if (module)
1457 {
1458 ath_mutex_lock (&pubkeys_registered_lock);
1459 _gcry_module_release (module);
1460 ath_mutex_unlock (&pubkeys_registered_lock);
1461 }
1462
1463 return gcry_error (rc);
1464}
1465
1466/*
1467 Do a PK decrypt operation
1468
1469 Caller has to provide a secret key as the SEXP skey and data in a
1470 format as created by gcry_pk_encrypt. For historic reasons the
1471 function returns simply an MPI as an S-expression part; this is
1472 deprecated and the new method should be used which returns a real
1473 S-expressionl this is selected by adding at least an empty flags
1474 list to S_DATA.
1475
1476 Returns: 0 or an errorcode.
1477
1478 s_data = (enc-val
1479 [(flags)]
1480 (<algo>
1481 (<param_name1> <mpi>)
1482 ...
1483 (<param_namen> <mpi>)
1484 ))
1485 s_skey = <key-as-defined-in-sexp_to_key>
1486 r_plain= Either an incomplete S-expression without the parentheses
1487 or if the flags list is used (even if empty) a real S-expression:
1488 (value PLAIN).
1489 */
1490gcry_error_t
1491gcry_pk_decrypt (gcry_sexp_t *r_plain, gcry_sexp_t s_data, gcry_sexp_t s_skey)
1492{
1493 gcry_mpi_t *skey = NULL, *data = NULL, plain = NULL;
1494 int modern, want_pkcs1, flags;
1495 gcry_err_code_t rc;
1496 gcry_module_t module_enc = NULL, module_key = NULL;
1497 gcry_pk_spec_t *pubkey = NULL;
1498
1499 REGISTER_DEFAULT_PUBKEYS;
1500
1501 *r_plain = NULL;
1502 rc = sexp_to_key (s_skey, 1, &skey, &module_key);
1503 if (rc)
1504 goto leave;
1505
1506 rc = sexp_to_enc (s_data, &data, &module_enc, &modern, &want_pkcs1, &flags);
1507 if (rc)
1508 goto leave;
1509
1510 if (module_key->mod_id != module_enc->mod_id)
1511 {
1512 rc = GPG_ERR_CONFLICT; /* Key algo does not match data algo. */
1513 goto leave;
1514 }
1515
1516 pubkey = (gcry_pk_spec_t *) module_key->spec;
1517
1518 rc = pubkey_decrypt (module_key->mod_id, &plain, data, skey, flags);
1519 if (rc)
1520 goto leave;
1521
1522 if (gcry_sexp_build (r_plain, NULL, modern? "(value %m)" : "%m", plain))
1523 BUG ();
1524
1525 leave:
1526 if (skey)
1527 {
1528 release_mpi_array (skey);
1529 gcry_free (skey);
1530 }
1531
1532 if (plain)
1533 mpi_free (plain);
1534
1535 if (data)
1536 {
1537 release_mpi_array (data);
1538 gcry_free (data);
1539 }
1540
1541 if (module_key || module_enc)
1542 {
1543 ath_mutex_lock (&pubkeys_registered_lock);
1544 if (module_key)
1545 _gcry_module_release (module_key);
1546 if (module_enc)
1547 _gcry_module_release (module_enc);
1548 ath_mutex_unlock (&pubkeys_registered_lock);
1549 }
1550
1551 return gcry_error (rc);
1552}
1553
1554
1555
1556/*
1557 Create a signature.
1558
1559 Caller has to provide a secret key as the SEXP skey and data
1560 expressed as a SEXP list hash with only one element which should
1561 instantly be available as a MPI. Alternatively the structure given
1562 below may be used for S_HASH, it provides the abiliy to pass flags
1563 to the operation; the only flag defined by now is "pkcs1" which
1564 does PKCS#1 block type 1 style padding.
1565
1566 Returns: 0 or an errorcode.
1567 In case of 0 the function returns a new SEXP with the
1568 signature value; the structure of this signature depends on the
1569 other arguments but is always suitable to be passed to
1570 gcry_pk_verify
1571
1572 s_hash = See comment for sexp_data_to_mpi
1573
1574 s_skey = <key-as-defined-in-sexp_to_key>
1575 r_sig = (sig-val
1576 (<algo>
1577 (<param_name1> <mpi>)
1578 ...
1579 (<param_namen> <mpi>)))
1580*/
1581gcry_error_t
1582gcry_pk_sign (gcry_sexp_t *r_sig, gcry_sexp_t s_hash, gcry_sexp_t s_skey)
1583{
1584 gcry_mpi_t *skey = NULL, hash = NULL, *result = NULL;
1585 gcry_pk_spec_t *pubkey = NULL;
1586 gcry_module_t module = NULL;
1587 const char *algo_name, *algo_elems;
1588 int i;
1589 gcry_err_code_t rc;
1590
1591 REGISTER_DEFAULT_PUBKEYS;
1592
1593 *r_sig = NULL;
1594 rc = sexp_to_key (s_skey, 1, &skey, &module);
1595 if (rc)
1596 goto leave;
1597
1598 assert (module);
1599 pubkey = (gcry_pk_spec_t *) module->spec;
1600 algo_name = pubkey->aliases? *pubkey->aliases : NULL;
1601 if (!algo_name || !*algo_name)
1602 algo_name = pubkey->name;
1603
1604 algo_elems = pubkey->elements_sig;
1605
1606 /* Get the stuff we want to sign. Note that pk_get_nbits does also
1607 work on a private key. */
1608 rc = sexp_data_to_mpi (s_hash, gcry_pk_get_nbits (s_skey),
1609 &hash, 0, NULL);
1610 if (rc)
1611 goto leave;
1612
1613 result = gcry_xcalloc (strlen (algo_elems) + 1, sizeof (*result));
1614 rc = pubkey_sign (module->mod_id, result, hash, skey);
1615 if (rc)
1616 goto leave;
1617
1618 {
1619 char *string, *p;
1620 size_t nelem, needed = strlen (algo_name) + 20;
1621 void **arg_list;
1622
1623 nelem = strlen (algo_elems);
1624
1625 /* Count elements, so that we can allocate enough space. */
1626 needed += 10 * nelem;
1627
1628 /* Build the string. */
1629 string = p = gcry_xmalloc (needed);
1630 p = stpcpy (p, "(sig-val(");
1631 p = stpcpy (p, algo_name);
1632 for (i = 0; algo_elems[i]; i++)
1633 {
1634 *p++ = '(';
1635 *p++ = algo_elems[i];
1636 p = stpcpy (p, "%m)");
1637 }
1638 strcpy (p, "))");
1639
1640 arg_list = malloc (nelem * sizeof *arg_list);
1641 if (!arg_list)
1642 {
1643 rc = gpg_err_code_from_errno (errno);
1644 goto leave;
1645 }
1646
1647 for (i = 0; i < nelem; i++)
1648 arg_list[i] = result + i;
1649
1650 rc = gcry_sexp_build_array (r_sig, NULL, string, arg_list);
1651 free (arg_list);
1652 if (rc)
1653 BUG ();
1654 gcry_free (string);
1655 }
1656
1657 leave:
1658 if (skey)
1659 {
1660 release_mpi_array (skey);
1661 gcry_free (skey);
1662 }
1663
1664 if (hash)
1665 mpi_free (hash);
1666
1667 if (result)
1668 gcry_free (result);
1669
1670 return gcry_error (rc);
1671}
1672
1673
1674/*
1675 Verify a signature.
1676
1677 Caller has to supply the public key pkey, the signature sig and his
1678 hashvalue data. Public key has to be a standard public key given
1679 as an S-Exp, sig is a S-Exp as returned from gcry_pk_sign and data
1680 must be an S-Exp like the one in sign too. */
1681gcry_error_t
1682gcry_pk_verify (gcry_sexp_t s_sig, gcry_sexp_t s_hash, gcry_sexp_t s_pkey)
1683{
1684 gcry_module_t module_key = NULL, module_sig = NULL;
1685 gcry_mpi_t *pkey = NULL, hash = NULL, *sig = NULL;
1686 gcry_err_code_t rc;
1687
1688 REGISTER_DEFAULT_PUBKEYS;
1689
1690 rc = sexp_to_key (s_pkey, 0, &pkey, &module_key);
1691 if (rc)
1692 goto leave;
1693
1694 rc = sexp_to_sig (s_sig, &sig, &module_sig);
1695 if (rc)
1696 goto leave;
1697
1698 if (module_key->mod_id != module_sig->mod_id)
1699 {
1700 rc = GPG_ERR_CONFLICT;
1701 goto leave;
1702 }
1703
1704 rc = sexp_data_to_mpi (s_hash, gcry_pk_get_nbits (s_pkey), &hash, 0, 0);
1705 if (rc)
1706 goto leave;
1707
1708 rc = pubkey_verify (module_key->mod_id, hash, sig, pkey, NULL, NULL);
1709
1710 leave:
1711 if (pkey)
1712 {
1713 release_mpi_array (pkey);
1714 gcry_free (pkey);
1715 }
1716 if (sig)
1717 {
1718 release_mpi_array (sig);
1719 gcry_free (sig);
1720 }
1721 if (hash)
1722 mpi_free (hash);
1723
1724 if (module_key || module_sig)
1725 {
1726 ath_mutex_lock (&pubkeys_registered_lock);
1727 if (module_key)
1728 _gcry_module_release (module_key);
1729 if (module_sig)
1730 _gcry_module_release (module_sig);
1731 ath_mutex_unlock (&pubkeys_registered_lock);
1732 }
1733
1734 return gcry_error (rc);
1735}
1736
1737
1738/*
1739 Test a key.
1740
1741 This may be used either for a public or a secret key to see whether
1742 internal structre is valid.
1743
1744 Returns: 0 or an errorcode.
1745
1746 s_key = <key-as-defined-in-sexp_to_key> */
1747gcry_error_t
1748gcry_pk_testkey (gcry_sexp_t s_key)
1749{
1750 gcry_module_t module = NULL;
1751 gcry_mpi_t *key = NULL;
1752 gcry_err_code_t rc;
1753
1754 REGISTER_DEFAULT_PUBKEYS;
1755
1756 /* Note we currently support only secret key checking. */
1757 rc = sexp_to_key (s_key, 1, &key, &module);
1758 if (! rc)
1759 {
1760 rc = pubkey_check_secret_key (module->mod_id, key);
1761 release_mpi_array (key);
1762 gcry_free (key);
1763 }
1764 return gcry_error (rc);
1765}
1766
1767
1768/*
1769 Create a public key pair and return it in r_key.
1770 How the key is created depends on s_parms:
1771 (genkey
1772 (algo
1773 (parameter_name_1 ....)
1774 ....
1775 (parameter_name_n ....)
1776 ))
1777 The key is returned in a format depending on the
1778 algorithm. Both, private and secret keys are returned
1779 and optionally some additional informatin.
1780 For elgamal we return this structure:
1781 (key-data
1782 (public-key
1783 (elg
1784 (p <mpi>)
1785 (g <mpi>)
1786 (y <mpi>)
1787 )
1788 )
1789 (private-key
1790 (elg
1791 (p <mpi>)
1792 (g <mpi>)
1793 (y <mpi>)
1794 (x <mpi>)
1795 )
1796 )
1797 (misc-key-info
1798 (pm1-factors n1 n2 ... nn)
1799 ))
1800 */
1801gcry_error_t
1802gcry_pk_genkey (gcry_sexp_t *r_key, gcry_sexp_t s_parms)
1803{
1804 gcry_pk_spec_t *pubkey = NULL;
1805 gcry_module_t module = NULL;
1806 gcry_sexp_t list = NULL, l2 = NULL;
1807 const char *name;
1808 size_t n;
1809 gcry_err_code_t rc = GPG_ERR_NO_ERROR;
1810 int i;
1811 const char *algo_name = NULL;
1812 int algo;
1813 const char *sec_elems = NULL, *pub_elems = NULL;
1814 gcry_mpi_t skey[10], *factors = NULL;
1815 unsigned int nbits = 0;
1816 unsigned long use_e = 0;
1817 char *name_terminated;
1818
1819 REGISTER_DEFAULT_PUBKEYS;
1820
1821 skey[0] = NULL;
1822 *r_key = NULL;
1823
1824 list = gcry_sexp_find_token (s_parms, "genkey", 0);
1825 if (!list)
1826 {
1827 rc = GPG_ERR_INV_OBJ; /* Does not contain genkey data. */
1828 goto leave;
1829 }
1830
1831 l2 = gcry_sexp_cadr (list);
1832 gcry_sexp_release (list);
1833 list = l2;
1834 l2 = NULL;
1835 if (! list)
1836 {
1837 rc = GPG_ERR_NO_OBJ; /* No cdr for the genkey. */
1838 goto leave;
1839 }
1840
1841 name = gcry_sexp_nth_data (list, 0, &n);
1842 if (! name)
1843 {
1844 rc = GPG_ERR_INV_OBJ; /* Algo string missing. */
1845 goto leave;
1846 }
1847
1848 name_terminated = gcry_xmalloc (n + 1);
1849 memcpy (name_terminated, name, n);
1850 name_terminated[n] = 0;
1851 ath_mutex_lock (&pubkeys_registered_lock);
1852 module = gcry_pk_lookup_name (name_terminated);
1853 ath_mutex_unlock (&pubkeys_registered_lock);
1854 gcry_free (name_terminated);
1855
1856 if (! module)
1857 {
1858 rc = GPG_ERR_PUBKEY_ALGO; /* Unknown algorithm. */
1859 goto leave;
1860 }
1861
1862 pubkey = (gcry_pk_spec_t *) module->spec;
1863 algo = module->mod_id;
1864 algo_name = pubkey->aliases? *pubkey->aliases : NULL;
1865 if (!algo_name || !*algo_name)
1866 algo_name = pubkey->name;
1867 pub_elems = pubkey->elements_pkey;
1868 sec_elems = pubkey->elements_skey;
1869
1870 /* Handle the optional rsa-use-e element. */
1871 l2 = gcry_sexp_find_token (list, "rsa-use-e", 0);
1872 if (l2)
1873 {
1874 char buf[50];
1875
1876 name = gcry_sexp_nth_data (l2, 1, &n);
1877 if ((! name) || (n >= DIM (buf) - 1))
1878 {
1879 rc = GPG_ERR_INV_OBJ; /* No value or value too large. */
1880 goto leave;
1881 }
1882 memcpy (buf, name, n);
1883 buf[n] = 0;
1884 use_e = strtoul (buf, NULL, 0);
1885 gcry_sexp_release (l2);
1886 l2 = NULL;
1887 }
1888 else
1889 use_e = 65537; /* Not given, use the value generated by old versions. */
1890
1891 l2 = gcry_sexp_find_token (list, "nbits", 0);
1892 gcry_sexp_release (list);
1893 list = l2;
1894 l2 = NULL;
1895
1896 if (! list)
1897 {
1898 rc = GPG_ERR_NO_OBJ; /* No nbits parameter. */
1899 goto leave;
1900 }
1901
1902 name = gcry_sexp_nth_data (list, 1, &n);
1903 if (! name)
1904 {
1905 rc = GPG_ERR_INV_OBJ; /* nbits without a cdr. */
1906 goto leave;
1907 }
1908
1909 name_terminated = gcry_xmalloc (n + 1);
1910 memcpy (name_terminated, name, n);
1911 name_terminated[n] = 0;
1912 nbits = (unsigned int) strtoul (name_terminated, NULL, 0);
1913 gcry_free (name_terminated);
1914
1915 rc = pubkey_generate (module->mod_id, nbits, use_e, skey, &factors);
1916 if (rc)
1917 goto leave;
1918
1919 {
1920 char *string, *p;
1921 size_t nelem=0, nelem_cp = 0, needed=0;
1922 gcry_mpi_t mpis[30];
1923
1924 nelem = strlen (pub_elems) + strlen (sec_elems);
1925 for (i = 0; factors[i]; i++)
1926 nelem++;
1927 nelem_cp = nelem;
1928
1929 needed += nelem * 10;
1930 needed += 2 * strlen (algo_name) + 300;
1931 if (nelem > DIM (mpis))
1932 BUG ();
1933
1934 /* Build the string. */
1935 nelem = 0;
1936 string = p = gcry_xmalloc (needed);
1937 p = stpcpy (p, "(key-data");
1938 p = stpcpy (p, "(public-key(");
1939 p = stpcpy (p, algo_name);
1940 for(i = 0; pub_elems[i]; i++)
1941 {
1942 *p++ = '(';
1943 *p++ = pub_elems[i];
1944 p = stpcpy (p, "%m)");
1945 mpis[nelem++] = skey[i];
1946 }
1947 p = stpcpy (p, "))");
1948 p = stpcpy (p, "(private-key(");
1949 p = stpcpy (p, algo_name);
1950 for (i = 0; sec_elems[i]; i++)
1951 {
1952 *p++ = '(';
1953 *p++ = sec_elems[i];
1954 p = stpcpy (p, "%m)");
1955 mpis[nelem++] = skey[i];
1956 }
1957 p = stpcpy (p, "))");
1958
1959 /* Very ugly hack to make release_mpi_array() work FIXME */
1960 skey[i] = NULL;
1961
1962 p = stpcpy (p, "(misc-key-info(pm1-factors");
1963 for(i = 0; factors[i]; i++)
1964 {
1965 p = stpcpy (p, "%m");
1966 mpis[nelem++] = factors[i];
1967 }
1968 strcpy (p, ")))");
1969
1970 while (nelem < DIM (mpis))
1971 mpis[nelem++] = NULL;
1972
1973 {
1974 int elem_n = strlen (pub_elems) + strlen (sec_elems);
1975 void **arg_list;
1976
1977 arg_list = malloc (nelem_cp * sizeof *arg_list);
1978 if (!arg_list)
1979 {
1980 rc = gpg_err_code_from_errno (errno);
1981 goto leave;
1982 }
1983 for (i = 0; i < elem_n; i++)
1984 arg_list[i] = mpis + i;
1985 for (; i < nelem_cp; i++)
1986 arg_list[i] = factors + i - elem_n;
1987
1988 rc = gcry_sexp_build_array (r_key, NULL, string, arg_list);
1989 free (arg_list);
1990 if (rc)
1991 BUG ();
1992 assert (DIM (mpis) == 30); /* Reminder to make sure that the
1993 array gets increased if new
1994 parameters are added. */
1995 }
1996 gcry_free (string);
1997 }
1998
1999 leave:
2000 release_mpi_array (skey);
2001 /* Don't free SKEY itself, it is a static array. */
2002
2003 if (factors)
2004 {
2005 release_mpi_array ( factors );
2006 gcry_free (factors);
2007 }
2008
2009 if (l2)
2010 gcry_sexp_release (l2);
2011 if (list)
2012 gcry_sexp_release (list);
2013
2014 if (module)
2015 {
2016 ath_mutex_lock (&pubkeys_registered_lock);
2017 _gcry_module_release (module);
2018 ath_mutex_unlock (&pubkeys_registered_lock);
2019 }
2020
2021 return gcry_error (rc);
2022}
2023
2024
2025/*
2026 Get the number of nbits from the public key.
2027
2028 Hmmm: Should we have really this function or is it better to have a
2029 more general function to retrieve different propoerties of the key? */
2030unsigned int
2031gcry_pk_get_nbits (gcry_sexp_t key)
2032{
2033 gcry_module_t module = NULL;
2034 gcry_pk_spec_t *pubkey;
2035 gcry_mpi_t *keyarr = NULL;
2036 unsigned int nbits = 0;
2037 gcry_err_code_t rc;
2038
2039 REGISTER_DEFAULT_PUBKEYS;
2040
2041 rc = sexp_to_key (key, 0, &keyarr, &module);
2042 if (rc == GPG_ERR_INV_OBJ)
2043 rc = sexp_to_key (key, 1, &keyarr, &module);
2044 if (rc)
2045 return 0; /* Error - 0 is a suitable indication for that. */
2046
2047 pubkey = (gcry_pk_spec_t *) module->spec;
2048 nbits = (*pubkey->get_nbits) (module->mod_id, keyarr);
2049
2050 ath_mutex_lock (&pubkeys_registered_lock);
2051 _gcry_module_release (module);
2052 ath_mutex_unlock (&pubkeys_registered_lock);
2053
2054 release_mpi_array (keyarr);
2055 gcry_free (keyarr);
2056
2057 return nbits;
2058}
2059
2060
2061/* Return the so called KEYGRIP which is the SHA-1 hash of the public
2062 key parameters expressed in a way depended on the algorithm.
2063
2064 ARRAY must either be 20 bytes long or NULL; in the latter case a
2065 newly allocated array of that size is returned, otherwise ARRAY or
2066 NULL is returned to indicate an error which is most likely an
2067 unknown algorithm. The function accepts public or secret keys. */
2068unsigned char *
2069gcry_pk_get_keygrip (gcry_sexp_t key, unsigned char *array)
2070{
2071 gcry_sexp_t list = NULL, l2 = NULL;
2072 gcry_pk_spec_t *pubkey = NULL;
2073 gcry_module_t module = NULL;
2074 const char *s, *name;
2075 size_t n;
2076 int idx;
2077 int is_rsa;
2078 const char *elems;
2079 gcry_md_hd_t md = NULL;
2080
2081 REGISTER_DEFAULT_PUBKEYS;
2082
2083 /* Check that the first element is valid. */
2084 list = gcry_sexp_find_token (key, "public-key", 0);
2085 if (! list)
2086 list = gcry_sexp_find_token (key, "private-key", 0);
2087 if (! list)
2088 list = gcry_sexp_find_token (key, "protected-private-key", 0);
2089 if (! list)
2090 return NULL; /* No public- or private-key object. */
2091
2092 l2 = gcry_sexp_cadr (list);
2093 gcry_sexp_release (list);
2094 list = l2;
2095 l2 = NULL;
2096
2097 name = gcry_sexp_nth_data (list, 0, &n);
2098 if (! name)
2099 goto fail; /* Invalid structure of object. */
2100
2101 {
2102 char *name_terminated = gcry_xmalloc (n + 1);
2103 memcpy (name_terminated, name, n);
2104 name_terminated[n] = 0;
2105 ath_mutex_lock (&pubkeys_registered_lock);
2106 module = gcry_pk_lookup_name (name_terminated);
2107 ath_mutex_unlock (&pubkeys_registered_lock);
2108 gcry_free (name_terminated);
2109 }
2110
2111 if (! module)
2112 goto fail; /* unknown algorithm */
2113
2114 pubkey = (gcry_pk_spec_t *) module->spec;
2115
2116 /* FIXME, special handling should be implemented by the algorithms,
2117 not by the libgcrypt core. */
2118 is_rsa = module->mod_id == GCRY_PK_RSA;
2119 elems = pubkey->elements_grip;
2120 if (! elems)
2121 goto fail; /* no grip parameter */
2122
2123 if (gcry_md_open (&md, GCRY_MD_SHA1, 0))
2124 goto fail;
2125
2126 for (idx = 0, s = elems; *s; s++, idx++)
2127 {
2128 const char *data;
2129 size_t datalen;
2130
2131 l2 = gcry_sexp_find_token (list, s, 1);
2132 if (! l2)
2133 goto fail;
2134 data = gcry_sexp_nth_data (l2, 1, &datalen);
2135 if (! data)
2136 goto fail;
2137 if (!is_rsa)
2138 {
2139 char buf[30];
2140
2141 sprintf (buf, "(1:%c%u:", *s, (unsigned int)datalen);
2142 gcry_md_write (md, buf, strlen (buf));
2143 }
2144
2145 /* PKCS-15 says that for RSA only the modulus should be hashed -
2146 however, it is not clear wether this is meant to has the raw
2147 bytes assuming this is an unsigned integer or whether the DER
2148 required 0 should be prefixed. We hash the raw bytes. For
2149 non-RSA we hash S-expressions. */
2150 gcry_md_write (md, data, datalen);
2151 gcry_sexp_release (l2);
2152 if (!is_rsa)
2153 gcry_md_write (md, ")", 1);
2154 }
2155
2156 if (!array)
2157 {
2158 array = gcry_malloc (20);
2159 if (! array)
2160 goto fail;
2161 }
2162
2163 memcpy (array, gcry_md_read (md, GCRY_MD_SHA1), 20);
2164 gcry_md_close (md);
2165 gcry_sexp_release (list);
2166 return array;
2167
2168 fail:
2169 if (l2)
2170 gcry_sexp_release (l2);
2171 if (md)
2172 gcry_md_close (md);
2173 gcry_sexp_release (list);
2174 return NULL;
2175}
2176
2177
2178gcry_error_t
2179gcry_pk_ctl (int cmd, void *buffer, size_t buflen)
2180{
2181 gcry_err_code_t err = GPG_ERR_NO_ERROR;
2182
2183 REGISTER_DEFAULT_PUBKEYS;
2184
2185 switch (cmd)
2186 {
2187 case GCRYCTL_DISABLE_ALGO:
2188 /* This one expects a buffer pointing to an integer with the
2189 algo number. */
2190 if ((! buffer) || (buflen != sizeof (int)))
2191 err = GPG_ERR_INV_ARG;
2192 else
2193 disable_pubkey_algo (*((int *) buffer));
2194 break;
2195
2196 default:
2197 err = GPG_ERR_INV_OP;
2198 }
2199
2200 return gcry_error (err);
2201}
2202
2203
2204/*
2205 Return information about the given algorithm
2206 WHAT select the kind of information returned:
2207 GCRYCTL_TEST_ALGO:
2208 Returns 0 when the specified algorithm is available for use.
2209 Buffer must be NULL, nbytes may have the address of a variable
2210 with the required usage of the algorithm. It may be 0 for don't
2211 care or a combination of the GCRY_PK_USAGE_xxx flags;
2212 GCRYCTL_GET_ALGO_USAGE:
2213 Return the usage glafs for the give algo. An invalid alog
2214 does return 0. Disabled algos are ignored here becuase we
2215 only want to know whether the algo is at all capable of
2216 the usage.
2217
2218 Note: Because this function is in most cases used to return an
2219 integer value, we can make it easier for the caller to just look at
2220 the return value. The caller will in all cases consult the value
2221 and thereby detecting whether a error occured or not (i.e. while
2222 checking the block size) */
2223gcry_error_t
2224gcry_pk_algo_info (int algorithm, int what, void *buffer, size_t *nbytes)
2225{
2226 gcry_err_code_t err = GPG_ERR_NO_ERROR;
2227
2228 switch (what)
2229 {
2230 case GCRYCTL_TEST_ALGO:
2231 {
2232 int use = nbytes ? *nbytes : 0;
2233 if (buffer)
2234 err = GPG_ERR_INV_ARG;
2235 else if (check_pubkey_algo (algorithm, use))
2236 err = GPG_ERR_PUBKEY_ALGO;
2237 break;
2238 }
2239
2240 case GCRYCTL_GET_ALGO_USAGE:
2241 {
2242 gcry_module_t pubkey;
2243 int use = 0;
2244
2245 REGISTER_DEFAULT_PUBKEYS;
2246
2247 ath_mutex_lock (&pubkeys_registered_lock);
2248 pubkey = _gcry_module_lookup_id (pubkeys_registered, algorithm);
2249 if (pubkey)
2250 {
2251 use = ((gcry_pk_spec_t *) pubkey->spec)->use;
2252 _gcry_module_release (pubkey);
2253 }
2254 ath_mutex_unlock (&pubkeys_registered_lock);
2255
2256 /* FIXME? */
2257 *nbytes = use;
2258 }
2259
2260 case GCRYCTL_GET_ALGO_NPKEY:
2261 {
2262 /* FIXME? */
2263 int npkey = pubkey_get_npkey (algorithm);
2264 *nbytes = npkey;
2265 break;
2266 }
2267 case GCRYCTL_GET_ALGO_NSKEY:
2268 {
2269 /* FIXME? */
2270 int nskey = pubkey_get_nskey (algorithm);
2271 *nbytes = nskey;
2272 break;
2273 }
2274 case GCRYCTL_GET_ALGO_NSIGN:
2275 {
2276 /* FIXME? */
2277 int nsign = pubkey_get_nsig (algorithm);
2278 *nbytes = nsign;
2279 break;
2280 }
2281 case GCRYCTL_GET_ALGO_NENCR:
2282 {
2283 /* FIXME? */
2284 int nencr = pubkey_get_nenc (algorithm);
2285 *nbytes = nencr;
2286 break;
2287 }
2288
2289 default:
2290 err = GPG_ERR_INV_OP;
2291 }
2292
2293 return gcry_error (err);
2294}
2295
2296
2297gcry_err_code_t
2298_gcry_pk_init (void)
2299{
2300 gcry_err_code_t err = GPG_ERR_NO_ERROR;
2301
2302 REGISTER_DEFAULT_PUBKEYS;
2303
2304 return err;
2305}
2306
2307
2308gcry_err_code_t
2309_gcry_pk_module_lookup (int algorithm, gcry_module_t *module)
2310{
2311 gcry_err_code_t err = GPG_ERR_NO_ERROR;
2312 gcry_module_t pubkey;
2313
2314 REGISTER_DEFAULT_PUBKEYS;
2315
2316 ath_mutex_lock (&pubkeys_registered_lock);
2317 pubkey = _gcry_module_lookup_id (pubkeys_registered, algorithm);
2318 if (pubkey)
2319 *module = pubkey;
2320 else
2321 err = GPG_ERR_PUBKEY_ALGO;
2322 ath_mutex_unlock (&pubkeys_registered_lock);
2323
2324 return err;
2325}
2326
2327
2328void
2329_gcry_pk_module_release (gcry_module_t module)
2330{
2331 ath_mutex_lock (&pubkeys_registered_lock);
2332 _gcry_module_release (module);
2333 ath_mutex_unlock (&pubkeys_registered_lock);
2334}
2335
2336/* Get a list consisting of the IDs of the loaded pubkey modules. If
2337 LIST is zero, write the number of loaded pubkey modules to
2338 LIST_LENGTH and return. If LIST is non-zero, the first
2339 *LIST_LENGTH algorithm IDs are stored in LIST, which must be of
2340 according size. In case there are less pubkey modules than
2341 *LIST_LENGTH, *LIST_LENGTH is updated to the correct number. */
2342gcry_error_t
2343gcry_pk_list (int *list, int *list_length)
2344{
2345 gcry_err_code_t err = GPG_ERR_NO_ERROR;
2346
2347 ath_mutex_lock (&pubkeys_registered_lock);
2348 err = _gcry_module_list (pubkeys_registered, list, list_length);
2349 ath_mutex_unlock (&pubkeys_registered_lock);
2350
2351 return err;
2352}
diff --git a/pwmanager/libcrypt/cipher/rand-internal.h b/pwmanager/libcrypt/cipher/rand-internal.h
new file mode 100644
index 0000000..bf1b2af
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/rand-internal.h
@@ -0,0 +1,41 @@
1/* rand-internal.h - header to glue the random functions
2 *Copyright (C) 1998, 2002 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20#ifndef G10_RAND_INTERNAL_H
21#define G10_RAND_INTERNAL_H
22
23void _gcry_random_progress (const char *what, int printchar,
24 int current, int total);
25
26
27int _gcry_rndlinux_gather_random (void (*add) (const void *, size_t, int),
28 int requester, size_t length, int level);
29int _gcry_rndunix_gather_random (void (*add) (const void *, size_t, int),
30 int requester, size_t length, int level);
31int _gcry_rndegd_gather_random (void (*add) (const void *, size_t, int),
32 int requester, size_t length, int level);
33int _gcry_rndegd_connect_socket (int nofail);
34int _gcry_rndw32_gather_random (void (*add) (const void *, size_t, int),
35 int requester, size_t length, int level);
36int _gcry_rndw32_gather_random_fast (void (*add)(const void*, size_t, int),
37 int requester );
38
39
40
41#endif /*G10_RAND_INTERNAL_H*/
diff --git a/pwmanager/libcrypt/cipher/random.c b/pwmanager/libcrypt/cipher/random.c
new file mode 100644
index 0000000..e0d04a4
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/random.c
@@ -0,0 +1,1134 @@
1 /* random.c -random number generator
2 * Copyright (C) 1998, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21/****************
22 * This random number generator is modelled after the one described in
23 * Peter Gutmann's paper: "Software Generation of Practically Strong
24 * Random Numbers". See also chapter 6 in his book "Cryptographic
25 * Security Architecture", New York, 2004, ISBN 0-387-95387-6.
26 */
27
28
29#include <config.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <assert.h>
33#include <errno.h>
34#include <string.h>
35#include <sys/time.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <unistd.h>
39#include <fcntl.h>
40#include <time.h>
41 #ifdefHAVE_GETHRTIME
42#include <sys/times.h>
43#endif
44#ifdef HAVE_GETTIMEOFDAY
45#include <sys/times.h>
46#endif
47#ifdef HAVE_GETRUSAGE
48#include <sys/resource.h>
49#endif
50#ifdef __MINGW32__
51#include <process.h>
52#endif
53#include "g10lib.h"
54#include "rmd.h"
55#include "random.h"
56#include "rand-internal.h"
57#include "cipher.h" /* only used for the rmd160_hash_buffer() prototype */
58#include "ath.h"
59
60#ifndef RAND_MAX /* for SunOS */
61#define RAND_MAX 32767
62#endif
63
64
65#if SIZEOF_UNSIGNED_LONG == 8
66#define ADD_VALUE 0xa5a5a5a5a5a5a5a5
67#elif SIZEOF_UNSIGNED_LONG == 4
68#define ADD_VALUE 0xa5a5a5a5
69#else
70#error weird size for an unsigned long
71#endif
72
73#define BLOCKLEN 64 /* hash this amount of bytes */
74#define DIGESTLEN 20 /* into a digest of this length (rmd160) */
75/* poolblocks is the number of digests which make up the pool
76 * and poolsize must be a multiple of the digest length
77 * to make the AND operations faster, the size should also be
78 * a multiple of ulong
79 */
80#define POOLBLOCKS 30
81#define POOLSIZE (POOLBLOCKS*DIGESTLEN)
82#if (POOLSIZE % SIZEOF_UNSIGNED_LONG)
83#error Please make sure that poolsize is a multiple of ulong
84#endif
85#define POOLWORDS (POOLSIZE / SIZEOF_UNSIGNED_LONG)
86
87
88static int is_initialized;
89#define MASK_LEVEL(a) do { (a) &= 3; } while(0)
90 static char *rndpool;/* allocated size is POOLSIZE+BLOCKLEN */
91 static char *keypool;/* allocated size is POOLSIZE+BLOCKLEN */
92static size_t pool_readpos;
93static size_t pool_writepos;
94static int pool_filled;
95static int pool_balance;
96static int just_mixed;
97static int did_initial_extra_seeding;
98static char *seed_file_name;
99static int allow_seed_file_update;
100
101static int secure_alloc;
102static int quick_test;
103static int faked_rng;
104
105static ath_mutex_t pool_lock = ATH_MUTEX_INITIALIZER;
106static int pool_is_locked; /* only used for assertion */
107
108static ath_mutex_t nonce_buffer_lock = ATH_MUTEX_INITIALIZER;
109
110static byte *get_random_bytes( size_t nbytes, int level, int secure );
111static void read_pool( byte *buffer, size_t length, int level );
112static void add_randomness( const void *buffer, size_t length, int source );
113static void random_poll(void);
114static void do_fast_random_poll (void);
115static void read_random_source( int requester, size_t length, int level);
116static int gather_faked( void (*add)(const void*, size_t, int), int requester,
117 size_t length, int level );
118
119static struct {
120 ulong mixrnd;
121 ulong mixkey;
122 ulong slowpolls;
123 ulong fastpolls;
124 ulong getbytes1;
125 ulong ngetbytes1;
126 ulong getbytes2;
127 ulong ngetbytes2;
128 ulong addbytes;
129 ulong naddbytes;
130} rndstats;
131
132static void (*progress_cb) (void *,const char*,int,int, int );
133static void *progress_cb_data;
134
135/* Note, we assume that this function is used before any concurrent
136 access happens. */
137static void
138initialize(void)
139{
140 int err;
141
142 err = ath_mutex_init (&pool_lock);
143 if (err)
144 log_fatal ("failed to create the pool lock: %s\n", strerror (err) );
145
146 err = ath_mutex_init (&nonce_buffer_lock);
147 if (err)
148 log_fatal ("failed to create the nonce buffer lock: %s\n",
149 strerror (err) );
150
151 /* The data buffer is allocated somewhat larger, so that we can use
152 this extra space (which is allocated in secure memory) as a
153 temporary hash buffer */
154 rndpool = secure_alloc ? gcry_xcalloc_secure(1,POOLSIZE+BLOCKLEN)
155 : gcry_xcalloc(1,POOLSIZE+BLOCKLEN);
156 keypool = secure_alloc ? gcry_xcalloc_secure(1,POOLSIZE+BLOCKLEN)
157 : gcry_xcalloc(1,POOLSIZE+BLOCKLEN);
158 is_initialized = 1;
159
160}
161
162
163/* Used to register a progress callback. */
164void
165_gcry_register_random_progress (void (*cb)(void *,const char*,int,int,int),
166 void *cb_data )
167{
168 progress_cb = cb;
169 progress_cb_data = cb_data;
170}
171
172
173/* This progress function is currently used by the random modules to give hint
174 on how much more entropy is required. */
175void
176_gcry_random_progress (const char *what, int printchar, int current, int total)
177{
178 if (progress_cb)
179 progress_cb (progress_cb_data, what, printchar, current, total);
180}
181
182
183/* Initialize this random subsystem. This function merely calls the
184 initialize and does not do anything more. Doing this is not really
185 required but when running in a threaded environment we might get a
186 race condition otherwise. */
187void
188_gcry_random_initialize ()
189{
190 if (!is_initialized)
191 initialize ();
192}
193
194void
195_gcry_random_dump_stats()
196{
197 log_info (
198 "random usage: poolsize=%d mixed=%lu polls=%lu/%lu added=%lu/%lu\n"
199 " outmix=%lu getlvl1=%lu/%lu getlvl2=%lu/%lu\n",
200 POOLSIZE, rndstats.mixrnd, rndstats.slowpolls, rndstats.fastpolls,
201 rndstats.naddbytes, rndstats.addbytes,
202 rndstats.mixkey, rndstats.ngetbytes1, rndstats.getbytes1,
203 rndstats.ngetbytes2, rndstats.getbytes2 );
204}
205
206void
207_gcry_secure_random_alloc()
208{
209 secure_alloc = 1;
210}
211
212
213int
214_gcry_quick_random_gen( int onoff )
215{
216 int last;
217
218 /* No need to lock it here because we are only initializing. A
219 prerequisite of the entire code is that it has already been
220 initialized before any possible concurrent access */
221 read_random_source(0,0,0); /* init */
222 last = quick_test;
223 if( onoff != -1 )
224 quick_test = onoff;
225 return faked_rng? 1 : last;
226}
227
228int
229_gcry_random_is_faked()
230{
231 if( !is_initialized )
232 initialize();
233 return (faked_rng || quick_test);
234}
235
236/*
237 * Return a pointer to a randomized buffer of LEVEL and NBYTES length.
238 * Caller must free the buffer.
239 */
240static byte *
241get_random_bytes ( size_t nbytes, int level, int secure)
242{
243 byte *buf, *p;
244 int err;
245
246 /* First a hack toavoid the strong random using our regression test suite. */
247 if (quick_test && level > 1)
248 level = 1;
249
250 /* Make sure the requested level is in range. */
251 MASK_LEVEL(level);
252
253 /* Lock the pool. */
254 err = ath_mutex_lock (&pool_lock);
255 if (err)
256 log_fatal ("failed to acquire the pool lock: %s\n", strerror (err));
257 pool_is_locked = 1;
258
259 /* Keep some statistics. */
260 if (level >= 2)
261 {
262 rndstats.getbytes2 += nbytes;
263 rndstats.ngetbytes2++;
264 }
265 else
266 {
267 rndstats.getbytes1 += nbytes;
268 rndstats.ngetbytes1++;
269 }
270
271 /* Allocate the return buffer. */
272 buf = secure && secure_alloc ? gcry_xmalloc_secure( nbytes )
273 : gcry_xmalloc( nbytes );
274
275 /* Fill that buffer with random. */
276 for (p = buf; nbytes > 0; )
277 {
278 size_t n;
279
280 n = nbytes > POOLSIZE? POOLSIZE : nbytes;
281 read_pool( p, n, level );
282 nbytes -= n;
283 p += n;
284 }
285
286 /* Release the pool lock. */
287 pool_is_locked = 0;
288 err = ath_mutex_unlock (&pool_lock);
289 if (err)
290 log_fatal ("failed to release the pool lock: %s\n", strerror (err));
291
292 /* Return the buffer. */
293 return buf;
294}
295
296
297/* Add BUFLEN bytes from BUF to the internal random pool. QUALITY
298 should be in the range of 0..100 to indicate the goodness of the
299 entropy added, or -1 for goodness not known.
300
301 Note, that this function currently does nothing.
302*/
303gcry_error_t
304gcry_random_add_bytes (const void * buf, size_t buflen, int quality)
305{
306 gcry_err_code_t err = GPG_ERR_NO_ERROR;
307
308 if (!buf || quality < -1 || quality > 100)
309 err = GPG_ERR_INV_ARG;
310 if (!buflen)
311 return 0; /* Shortcut this dummy case. */
312#if 0
313 /* Before we actuall enable this code, we need to lock the pool,
314 have a look at the quality and find a way to add them without
315 disturbing the real entropy (we have estimated). */
316 /*add_randomness( buf, buflen, 1 );*/
317#endif
318 return err;
319}
320
321/* The public function to return random data of the quality LEVEL. */
322void *
323gcry_random_bytes( size_t nbytes, enum gcry_random_level level )
324{
325 if (!is_initialized)
326 initialize();
327 return get_random_bytes( nbytes, level, 0 );
328}
329
330/* The public function to return random data of the quality LEVEL;
331 this version of the function retrun the random a buffer allocated
332 in secure memory. */
333void *
334gcry_random_bytes_secure( size_t nbytes, enum gcry_random_level level )
335{
336 if (!is_initialized)
337 initialize();
338 return get_random_bytes( nbytes, level, 1 );
339}
340
341
342/* Public function to fill the buffer with LENGTH bytes of
343 cryptographically strong random bytes. level 0 is not very strong,
344 1 is strong enough for most usage, 2 is good for key generation
345 stuff but may be very slow. */
346void
347gcry_randomize (byte *buffer, size_t length, enum gcry_random_level level)
348{
349 byte *p;
350 int err;
351
352 /* Make sure we are initialized. */
353 if (!is_initialized)
354 initialize ();
355
356 /* Handle our hack used for regression tests of Libgcrypt. */
357 if( quick_test && level > 1 )
358 level = 1;
359
360 /* Make sure the level is okay. */
361 MASK_LEVEL(level);
362
363 /* Acquire the pool lock. */
364 err = ath_mutex_lock (&pool_lock);
365 if (err)
366 log_fatal ("failed to acquire the pool lock: %s\n", strerror (err));
367 pool_is_locked = 1;
368
369 /* Update the statistics. */
370 if (level >= 2)
371 {
372 rndstats.getbytes2 += length;
373 rndstats.ngetbytes2++;
374 }
375 else
376 {
377 rndstats.getbytes1 += length;
378 rndstats.ngetbytes1++;
379 }
380
381 /* Read the random into the provided buffer. */
382 for (p = buffer; length > 0;)
383 {
384 size_t n;
385
386 n = length > POOLSIZE? POOLSIZE : length;
387 read_pool (p, n, level);
388 length -= n;
389 p += n;
390 }
391
392 /* Release the pool lock. */
393 pool_is_locked = 0;
394 err = ath_mutex_unlock (&pool_lock);
395 if (err)
396 log_fatal ("failed to release the pool lock: %s\n", strerror (err));
397
398}
399
400
401
402
403/*
404 Mix the pool:
405
406 |........blocks*20byte........|20byte|..44byte..|
407 <..44byte..> <20byte>
408 | |
409 | +------+
410 +---------------------------|----------+
411 v v
412 |........blocks*20byte........|20byte|..44byte..|
413 <.....64bytes.....>
414 |
415 +----------------------------------+
416 Hash
417 v
418 |.............................|20byte|..44byte..|
419 <20byte><20byte><..44byte..>
420 | |
421 | +---------------------+
422 +-----------------------------+ |
423 v v
424 |.............................|20byte|..44byte..|
425 <.....64byte......>
426 |
427 +-------------------------+
428 Hash
429 v
430 |.............................|20byte|..44byte..|
431 <20byte><20byte><..44byte..>
432
433 and so on until we did this for all blocks.
434
435 To better protect against implementation errors in this code, we
436 xor a digest of the entire pool into the pool before mixing.
437
438 Note, that this function muts only be called with a locked pool.
439 */
440static void
441mix_pool(byte *pool)
442{
443 static unsigned char failsafe_digest[DIGESTLEN];
444 static int failsafe_digest_valid;
445
446 char *hashbuf = pool + POOLSIZE;
447 char *p, *pend;
448 int i, n;
449 RMD160_CONTEXT md;
450
451#if DIGESTLEN != 20
452#error must have a digest length of 20 for ripe-md-160
453#endif
454
455 assert (pool_is_locked);
456 _gcry_rmd160_init( &md );
457
458 /* loop over the pool */
459 pend = pool + POOLSIZE;
460 memcpy(hashbuf, pend - DIGESTLEN, DIGESTLEN );
461 memcpy(hashbuf+DIGESTLEN, pool, BLOCKLEN-DIGESTLEN);
462 _gcry_rmd160_mixblock( &md, hashbuf);
463 memcpy(pool, hashbuf, 20 );
464
465 if (failsafe_digest_valid && (char *)pool == rndpool)
466 {
467 for (i=0; i < 20; i++)
468 pool[i] ^= failsafe_digest[i];
469 }
470
471 p = pool;
472 for (n=1; n < POOLBLOCKS; n++)
473 {
474 memcpy (hashbuf, p, DIGESTLEN);
475
476 p += DIGESTLEN;
477 if (p+DIGESTLEN+BLOCKLEN < pend)
478 memcpy (hashbuf+DIGESTLEN, p+DIGESTLEN, BLOCKLEN-DIGESTLEN);
479 else
480 {
481 char *pp = p + DIGESTLEN;
482
483 for (i=DIGESTLEN; i < BLOCKLEN; i++ )
484 {
485 if ( pp >= pend )
486 pp = pool;
487 hashbuf[i] = *pp++;
488 }
489 }
490
491 _gcry_rmd160_mixblock( &md, hashbuf);
492 memcpy(p, hashbuf, 20 );
493 }
494
495 /* Our hash implementation does only leave small parts (64 bytes)
496 of the pool on the stack, so it is okay not to require secure
497 memory here. Before we use this pool, it will be copied to the
498 help buffer anyway. */
499 if ( (char*)pool == rndpool)
500 {
501 _gcry_rmd160_hash_buffer (failsafe_digest, pool, POOLSIZE);
502 failsafe_digest_valid = 1;
503 }
504
505 _gcry_burn_stack (384); /* for the rmd160_mixblock(), rmd160_hash_buffer */
506}
507
508
509void
510_gcry_set_random_seed_file( const char *name )
511{
512 if (seed_file_name)
513 BUG ();
514 seed_file_name = gcry_xstrdup (name);
515}
516
517
518/*
519 Read in a seed form the random_seed file
520 and return true if this was successful.
521 */
522static int
523read_seed_file (void)
524{
525 int fd;
526 struct stat sb;
527 unsigned char buffer[POOLSIZE];
528 int n;
529
530 assert (pool_is_locked);
531
532 if (!seed_file_name)
533 return 0;
534
535#ifdef HAVE_DOSISH_SYSTEM
536 fd = open( seed_file_name, O_RDONLY | O_BINARY );
537#else
538 fd = open( seed_file_name, O_RDONLY );
539#endif
540 if( fd == -1 && errno == ENOENT)
541 {
542 allow_seed_file_update = 1;
543 return 0;
544 }
545
546 if (fd == -1 )
547 {
548 log_info(_("can't open `%s': %s\n"), seed_file_name, strerror(errno) );
549 return 0;
550 }
551 if (fstat( fd, &sb ) )
552 {
553 log_info(_("can't stat `%s': %s\n"), seed_file_name, strerror(errno) );
554 close(fd);
555 return 0;
556 }
557 if (!S_ISREG(sb.st_mode) )
558 {
559 log_info(_("`%s' is not a regular file - ignored\n"), seed_file_name );
560 close(fd);
561 return 0;
562 }
563 if (!sb.st_size )
564 {
565 log_info(_("note: random_seed file is empty\n") );
566 close(fd);
567 allow_seed_file_update = 1;
568 return 0;
569 }
570 if (sb.st_size != POOLSIZE )
571 {
572 log_info(_("warning: invalid size of random_seed file - not used\n") );
573 close(fd);
574 return 0;
575 }
576
577 do
578 {
579 n = read( fd, buffer, POOLSIZE );
580 }
581 while (n == -1 && errno == EINTR );
582
583 if (n != POOLSIZE)
584 {
585 log_fatal(_("can't read `%s': %s\n"), seed_file_name,strerror(errno) );
586 close(fd);/*NOTREACHED*/
587 return 0;
588 }
589
590 close(fd);
591
592 add_randomness( buffer, POOLSIZE, 0 );
593 /* add some minor entropy to the pool now (this will also force a mixing) */
594 {
595 pid_t x = getpid();
596 add_randomness( &x, sizeof(x), 0 );
597 }
598 {
599 time_t x = time(NULL);
600 add_randomness( &x, sizeof(x), 0 );
601 }
602 {
603 clock_t x = clock();
604 add_randomness( &x, sizeof(x), 0 );
605 }
606
607 /* And read a few bytes from our entropy source. By using a level
608 * of 0 this will not block and might not return anything with some
609 * entropy drivers, however the rndlinux driver will use
610 * /dev/urandom and return some stuff - Do not read to much as we
611 * want to be friendly to the scare system entropy resource. */
612 read_random_source( 0, 16, 0 );
613
614 allow_seed_file_update = 1;
615 return 1;
616}
617
618
619void
620_gcry_update_random_seed_file()
621{
622 ulong *sp, *dp;
623 int fd, i;
624 int err;
625
626 if ( !seed_file_name || !is_initialized || !pool_filled )
627 return;
628 if ( !allow_seed_file_update )
629 {
630 log_info(_("note: random_seed file not updated\n"));
631 return;
632 }
633
634 err = ath_mutex_lock (&pool_lock);
635 if (err)
636 log_fatal ("failed to acquire the pool lock: %s\n", strerror (err));
637 pool_is_locked = 1;
638
639 /* copy the entropy pool to a scratch pool and mix both of them */
640 for (i=0,dp=(ulong*)keypool, sp=(ulong*)rndpool;
641 i < POOLWORDS; i++, dp++, sp++ )
642 {
643 *dp = *sp + ADD_VALUE;
644 }
645 mix_pool(rndpool); rndstats.mixrnd++;
646 mix_pool(keypool); rndstats.mixkey++;
647
648#ifdef HAVE_DOSISH_SYSTEM
649 fd = open (seed_file_name, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,
650 S_IRUSR|S_IWUSR );
651#else
652 fd = open (seed_file_name, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR );
653#endif
654
655 if (fd == -1 )
656 log_info (_("can't create `%s': %s\n"), seed_file_name, strerror(errno) );
657 else
658 {
659 do
660 {
661 i = write (fd, keypool, POOLSIZE );
662 }
663 while( i == -1 && errno == EINTR );
664 if (i != POOLSIZE)
665 log_info (_("can't write `%s': %s\n"),
666 seed_file_name, strerror(errno) );
667 if (close(fd))
668 log_info(_("can't close `%s': %s\n"),
669 seed_file_name, strerror(errno) );
670 }
671
672 pool_is_locked = 0;
673 err = ath_mutex_unlock (&pool_lock);
674 if (err)
675 log_fatal ("failed to release the pool lock: %s\n", strerror (err));
676
677}
678
679
680/* Read random out of the pool. This function is the core of the
681 public random fucntions. Note that Level 0 is not anymore handeld
682 special and in fact an alias for level 1. */
683static void
684read_pool (byte *buffer, size_t length, int level)
685{
686 int i;
687 unsigned long *sp, *dp;
688 volatile pid_t my_pid; /* The volatile is there to make sure the
689 compiler does not optimize the code away
690 in case the getpid function is badly
691 attributed. */
692
693 retry:
694 /* Get our own pid, so that we can detect a fork. */
695 my_pid = getpid ();
696
697 assert (pool_is_locked);
698
699 /* Our code does not allow to extract more than POOLSIZE. Better
700 check it here. */
701 if (length > POOLSIZE)
702 {
703 log_bug("too many random bits requested\n");
704 }
705
706 if (!pool_filled)
707 {
708 if (read_seed_file() )
709 pool_filled = 1;
710 }
711
712 /* For level 2 quality (key generation) we always make sure that the
713 pool has been seeded enough initially. */
714 if (level == 2 && !did_initial_extra_seeding)
715 {
716 size_t needed;
717
718 pool_balance = 0;
719 needed = length - pool_balance;
720 if (needed < POOLSIZE/2)
721 needed = POOLSIZE/2;
722 else if( needed > POOLSIZE )
723 BUG ();
724 read_random_source (3, needed, 2);
725 pool_balance += needed;
726 did_initial_extra_seeding = 1;
727 }
728
729 /* For level 2 make sure that there is enough random in the pool. */
730 if (level == 2 && pool_balance < length)
731 {
732 size_t needed;
733
734 if (pool_balance < 0)
735 pool_balance = 0;
736 needed = length - pool_balance;
737 if (needed > POOLSIZE)
738 BUG ();
739 read_random_source( 3, needed, 2 );
740 pool_balance += needed;
741 }
742
743 /* make sure the pool is filled */
744 while (!pool_filled)
745 random_poll();
746
747 /* Always do a fast random poll (we have to use the unlocked version). */
748 do_fast_random_poll();
749
750 /* Mix the pool (if add_randomness() didn't it). */
751 if (!just_mixed)
752 {
753 mix_pool(rndpool);
754 rndstats.mixrnd++;
755 }
756
757 /* Create a new pool. */
758 for(i=0,dp=(ulong*)keypool, sp=(ulong*)rndpool;
759 i < POOLWORDS; i++, dp++, sp++ )
760 *dp = *sp + ADD_VALUE;
761
762 /* Mix both pools. */
763 mix_pool(rndpool); rndstats.mixrnd++;
764 mix_pool(keypool); rndstats.mixkey++;
765
766 /* Read the required data. We use a readpoiter to read from a
767 different position each time */
768 while (length--)
769 {
770 *buffer++ = keypool[pool_readpos++];
771 if (pool_readpos >= POOLSIZE)
772 pool_readpos = 0;
773 pool_balance--;
774 }
775
776 if (pool_balance < 0)
777 pool_balance = 0;
778
779 /* Clear the keypool. */
780 memset (keypool, 0, POOLSIZE);
781
782 /* We need to detect whether a fork has happened. A fork might have
783 an identical pool and thus the child and the parent could emit
784 the very same random number. Obviously this can only happen when
785 running multi-threaded and the pool lock should even catch this.
786 However things do get wrong and thus we better check and retry it
787 here. We assume that the thread library has no other fatal
788 faults, though.
789 */
790 if ( getpid () != my_pid )
791 goto retry;
792}
793
794
795/*
796 * Add LENGTH bytes of randomness from buffer to the pool.
797 * source may be used to specify the randomness source.
798 * Source is:
799 *0 - used ony for initialization
800 *1 - fast random poll function
801 *2 - normal poll function
802 *3 - used when level 2 random quality has been requested
803 * to do an extra pool seed.
804 */
805static void
806add_randomness( const void *buffer, size_t length, int source )
807{
808 const byte *p = buffer;
809
810 assert (pool_is_locked);
811 if (!is_initialized)
812 initialize ();
813 rndstats.addbytes += length;
814 rndstats.naddbytes++;
815 while (length-- )
816 {
817 rndpool[pool_writepos++] ^= *p++;
818 if (pool_writepos >= POOLSIZE )
819 {
820 if (source > 1)
821 pool_filled = 1;
822 pool_writepos = 0;
823 mix_pool(rndpool); rndstats.mixrnd++;
824 just_mixed = !length;
825 }
826 }
827}
828
829
830
831static void
832random_poll()
833{
834 rndstats.slowpolls++;
835 read_random_source (2, POOLSIZE/5, 1);
836}
837
838
839static int (*
840getfnc_gather_random (void))(void (*)(const void*, size_t, int), int,
841 size_t, int)
842{
843 static int (*fnc)(void (*)(const void*, size_t, int), int, size_t, int);
844
845 if (fnc)
846 return fnc;
847
848#if USE_RNDLINUX
849 if ( !access (NAME_OF_DEV_RANDOM, R_OK)
850 && !access (NAME_OF_DEV_URANDOM, R_OK))
851 {
852 fnc = _gcry_rndlinux_gather_random;
853 return fnc;
854 }
855#endif
856
857#if USE_RNDEGD
858 if ( _gcry_rndegd_connect_socket (1) != -1 )
859 {
860 fnc = _gcry_rndegd_gather_random;
861 return fnc;
862 }
863#endif
864
865#if USE_RNDUNIX
866 fnc = _gcry_rndunix_gather_random;
867 return fnc;
868#endif
869
870#if USE_RNDW32
871 fnc = _gcry_rndw32_gather_random;
872 return fnc;
873#endif
874
875 log_fatal (_("no entropy gathering module detected\n"));
876
877 return NULL; /*NOTREACHED*/
878}
879
880static void (*
881getfnc_fast_random_poll (void))( void (*)(const void*, size_t, int), int)
882{
883#if USE_RNDW32
884 return _gcry_rndw32_gather_random_fast;
885#endif
886 return NULL;
887}
888
889
890static void
891do_fast_random_poll (void)
892{
893 static void (*fnc)( void (*)(const void*, size_t, int), int) = NULL;
894 static int initialized = 0;
895
896 assert (pool_is_locked);
897
898 rndstats.fastpolls++;
899
900 if (!initialized )
901 {
902 if (!is_initialized )
903 initialize();
904 initialized = 1;
905 fnc = getfnc_fast_random_poll ();
906 }
907
908 if (fnc)
909 (*fnc)( add_randomness, 1 );
910
911 /* Continue with the generic functions. */
912#if HAVE_GETHRTIME
913 {
914 hrtime_t tv;
915 tv = gethrtime();
916 add_randomness( &tv, sizeof(tv), 1 );
917 }
918#elif HAVE_GETTIMEOFDAY
919 {
920 struct timeval tv;
921 if( gettimeofday( &tv, NULL ) )
922 BUG();
923 add_randomness( &tv.tv_sec, sizeof(tv.tv_sec), 1 );
924 add_randomness( &tv.tv_usec, sizeof(tv.tv_usec), 1 );
925 }
926#elif HAVE_CLOCK_GETTIME
927 {struct timespec tv;
928 if( clock_gettime( CLOCK_REALTIME, &tv ) == -1 )
929 BUG();
930 add_randomness( &tv.tv_sec, sizeof(tv.tv_sec), 1 );
931 add_randomness( &tv.tv_nsec, sizeof(tv.tv_nsec), 1 );
932 }
933#else /* use times */
934# ifndef HAVE_DOSISH_SYSTEM
935 {struct tms buf;
936 times( &buf );
937 add_randomness( &buf, sizeof buf, 1 );
938 }
939# endif
940#endif
941
942#ifdef HAVE_GETRUSAGE
943# ifdef RUSAGE_SELF
944 {
945 struct rusage buf;
946 /* QNX/Neutrino does return ENOSYS - so we just ignore it and
947 * add whatever is in buf. In a chroot environment it might not
948 * work at all (i.e. because /proc/ is not accessible), so we better
949 * ugnore all error codes and hope for the best
950 */
951 getrusage (RUSAGE_SELF, &buf );
952 add_randomness( &buf, sizeof buf, 1 );
953 memset( &buf, 0, sizeof buf );
954 }
955# else /*!RUSAGE_SELF*/
956# ifdef __GCC__
957# warning There is no RUSAGE_SELF on this system
958# endif
959# endif /*!RUSAGE_SELF*/
960#endif /*HAVE_GETRUSAGE*/
961
962 /* time and clock are availabe on all systems - so we better do it
963 just in case one of the above functions didn't work */
964 {
965 time_t x = time(NULL);
966 add_randomness( &x, sizeof(x), 1 );
967 }
968 {
969 clock_t x = clock();
970 add_randomness( &x, sizeof(x), 1 );
971 }
972}
973
974
975/* The fast random pool function as called at some places in
976 libgcrypt. This is merely a wrapper to make sure that this module
977 is initalized and to look the pool. */
978void
979_gcry_fast_random_poll (void)
980{
981 int err;
982
983 /* We have to make sure that the intialization is done because this
984 gatherer might be called before any other functions and it is not
985 sufficient to initialize it within do_fast_random_pool because we
986 want to use the mutex here. FIXME: Whe should initialize the
987 mutex using a global constructor independent from the
988 initialization of the pool. */
989 if (!is_initialized)
990 initialize ();
991 err = ath_mutex_lock (&pool_lock);
992 if (err)
993 log_fatal ("failed to acquire the pool lock: %s\n", strerror (err));
994 pool_is_locked = 1;
995
996 do_fast_random_poll ();
997
998 pool_is_locked = 0;
999 err = ath_mutex_unlock (&pool_lock);
1000 if (err)
1001 log_fatal ("failed to acquire the pool lock: %s\n", strerror (err));
1002
1003}
1004
1005
1006
1007static void
1008read_random_source( int requester, size_t length, int level )
1009{
1010 static int (*fnc)(void (*)(const void*, size_t, int), int,
1011 size_t, int) = NULL;
1012 if (!fnc )
1013 {
1014 if (!is_initialized )
1015 initialize();
1016
1017 fnc = getfnc_gather_random ();
1018
1019 if (!fnc)
1020 {
1021 faked_rng = 1;
1022 fnc = gather_faked;
1023 }
1024 if (!requester && !length && !level)
1025 return; /* Just the init was requested. */
1026 }
1027
1028 if ((*fnc)( add_randomness, requester, length, level ) < 0)
1029 log_fatal ("No way to gather entropy for the RNG\n");
1030}
1031
1032
1033static int
1034gather_faked( void (*add)(const void*, size_t, int), int requester,
1035 size_t length, int level )
1036{
1037 static int initialized=0;
1038 size_t n;
1039 char *buffer, *p;
1040
1041 if( !initialized ) {
1042 log_info(_("WARNING: using insecure random number generator!!\n"));
1043 /* we can't use tty_printf here - do we need this function at
1044 all - does it really make sense or canit be viewed as a potential
1045 security problem ? wk 17.11.99 */
1046#if 0
1047 tty_printf(_("The random number generator is only a kludge to let\n"
1048 "it run - it is in no way a strong RNG!\n\n"
1049 "DON'T USE ANY DATA GENERATED BY THIS PROGRAM!!\n\n"));
1050#endif
1051 initialized=1;
1052#ifdef HAVE_RAND
1053 srand( time(NULL)*getpid());
1054#else
1055 srandom( time(NULL)*getpid());
1056#endif
1057 }
1058
1059 p = buffer = gcry_xmalloc( length );
1060 n = length;
1061#ifdef HAVE_RAND
1062 while( n-- )
1063 *p++ = ((unsigned)(1 + (int) (256.0*rand()/(RAND_MAX+1.0)))-1);
1064#else
1065 while( n-- )
1066 *p++ = ((unsigned)(1 + (int) (256.0*random()/(RAND_MAX+1.0)))-1);
1067#endif
1068 add_randomness( buffer, length, requester );
1069 gcry_free(buffer);
1070 return 0; /* okay */
1071}
1072
1073
1074/* Create an unpredicable nonce of LENGTH bytes in BUFFER. */
1075void
1076gcry_create_nonce (unsigned char *buffer, size_t length)
1077{
1078 static unsigned char nonce_buffer[20+8];
1079 static int nonce_buffer_initialized = 0;
1080 unsigned char *p;
1081 size_t n;
1082 int err;
1083
1084 /* Make sure we are initialized. */
1085 if (!is_initialized)
1086 initialize ();
1087
1088 /* Acquire the nonce buffer lock. */
1089 err = ath_mutex_lock (&nonce_buffer_lock);
1090 if (err)
1091 log_fatal ("failed to acquire the nonce buffer lock: %s\n",
1092 strerror (err));
1093
1094 /* The first time intialize our buffer. */
1095 if (!nonce_buffer_initialized)
1096 {
1097 pid_t apid = getpid ();
1098 time_t atime = time (NULL);
1099
1100 if ((sizeof apid + sizeof atime) > sizeof nonce_buffer)
1101 BUG ();
1102
1103 /* Initialize the first 20 bytes with a reasonable value so that
1104 a failure of gcry_randomize won't affect us too much. Don't
1105 care about the uninitialized remaining bytes. */
1106 p = nonce_buffer;
1107 memcpy (p, &apid, sizeof apid);
1108 p += sizeof apid;
1109 memcpy (p, &atime, sizeof atime);
1110
1111 /* Initialize the never changing private part of 64 bits. */
1112 gcry_randomize (nonce_buffer+20, 8, GCRY_WEAK_RANDOM);
1113
1114 nonce_buffer_initialized = 1;
1115 }
1116
1117 /* Create the nonce by hashing the entire buffer, returning the hash
1118 and updating the first 20 bytes of the buffer with this hash. */
1119 for (p = buffer; length > 0; length -= n, p += n)
1120 {
1121 _gcry_sha1_hash_buffer (nonce_buffer,
1122 nonce_buffer, sizeof nonce_buffer);
1123 n = length > 20? 20 : length;
1124 memcpy (p, nonce_buffer, n);
1125 }
1126
1127
1128 /* Release the nonce buffer lock. */
1129 err = ath_mutex_unlock (&nonce_buffer_lock);
1130 if (err)
1131 log_fatal ("failed to release the nonce buffer lock: %s\n",
1132 strerror (err));
1133
1134}
diff --git a/pwmanager/libcrypt/cipher/random.h b/pwmanager/libcrypt/cipher/random.h
new file mode 100644
index 0000000..5d0f8bf
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/random.h
@@ -0,0 +1,42 @@
1/* random.h - random functions
2 *Copyright (C) 1998, 2002 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20#ifndef G10_RANDOM_H
21#define G10_RANDOM_H
22
23#include "types.h"
24
25void _gcry_random_initialize (void);
26void _gcry_register_random_progress (void (*cb)(void *,const char*,int,int,int),
27 void *cb_data );
28void _gcry_random_dump_stats(void);
29void _gcry_secure_random_alloc(void);
30int _gcry_quick_random_gen( int onoff );
31int _gcry_random_is_faked(void);
32void _gcry_set_random_seed_file (const char *name);
33void _gcry_update_random_seed_file (void);
34
35byte *_gcry_get_random_bits( size_t nbits, int level, int secure );
36void _gcry_fast_random_poll( void );
37
38#endif /*G10_RANDOM_H*/
39
40
41
42
diff --git a/pwmanager/libcrypt/cipher/rfc2268.c b/pwmanager/libcrypt/cipher/rfc2268.c
new file mode 100644
index 0000000..7d63fce
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/rfc2268.c
@@ -0,0 +1,345 @@
1/* rfc2268.c - The cipher described in rfc2268; aka Ron's Cipher 2.
2 * Copyright (C) 2003 Nikos Mavroyanopoulos
3 * Copyright (C) 2004 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser general Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 */
21
22/* This implementation was written by Nikos Mavroyanopoulos for GNUTLS
23 * as a Libgcrypt module (gnutls/lib/x509/rc2.c) and later adapted for
24 * direct use by Libgcrypt by Werner Koch. This implementation is
25 * only useful for pkcs#12 descryption.
26 *
27 * The implementation here is based on Peter Gutmann's RRC.2 paper.
28 */
29
30
31#include <config.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include "g10lib.h"
36#include "types.h"
37#include "cipher.h"
38
39#define RFC2268_BLOCKSIZE 8
40
41typedef struct
42{
43 u16 S[64];
44} RFC2268_context;
45
46static const unsigned char rfc2268_sbox[] = {
47 217, 120, 249, 196, 25, 221, 181, 237,
48 40, 233, 253, 121, 74, 160, 216, 157,
49 198, 126, 55, 131, 43, 118, 83, 142,
50 98, 76, 100, 136, 68, 139, 251, 162,
51 23, 154, 89, 245, 135, 179, 79, 19,
52 97, 69, 109, 141, 9, 129, 125, 50,
53 189, 143, 64, 235, 134, 183, 123, 11,
54 240, 149, 33, 34, 92, 107, 78, 130,
55 84, 214, 101, 147, 206, 96, 178, 28,
56 115, 86, 192, 20, 167, 140, 241, 220,
57 18, 117, 202, 31, 59, 190, 228, 209,
58 66, 61, 212, 48, 163, 60, 182, 38,
59 111, 191, 14, 218, 70, 105, 7, 87,
60 39, 242, 29, 155, 188, 148, 67, 3,
61 248, 17, 199, 246, 144, 239, 62, 231,
62 6, 195, 213, 47, 200, 102, 30, 215,
63 8, 232, 234, 222, 128, 82, 238, 247,
64 132, 170, 114, 172, 53, 77, 106, 42,
65 150, 26, 210, 113, 90, 21, 73, 116,
66 75, 159, 208, 94, 4, 24, 164, 236,
67 194, 224, 65, 110, 15, 81, 203, 204,
68 36, 145, 175, 80, 161, 244, 112, 57,
69 153, 124, 58, 133, 35, 184, 180, 122,
70 252, 2, 54, 91, 37, 85, 151, 49,
71 45, 93, 250, 152, 227, 138, 146, 174,
72 5, 223, 41, 16, 103, 108, 186, 201,
73 211, 0, 230, 207, 225, 158, 168, 44,
74 99, 22, 1, 63, 88, 226, 137, 169,
75 13, 56, 52, 27, 171, 51, 255, 176,
76 187, 72, 12, 95, 185, 177, 205, 46,
77 197, 243, 219, 71, 229, 165, 156, 119,
78 10, 166, 32, 104, 254, 127, 193, 173
79};
80
81#define rotl16(x,n) (((x) << ((u16)(n))) | ((x) >> (16 - (u16)(n))))
82#define rotr16(x,n) (((x) >> ((u16)(n))) | ((x) << (16 - (u16)(n))))
83
84static const char *selftest (void);
85
86
87static void
88do_encrypt (void *context, unsigned char *outbuf, const unsigned char *inbuf)
89{
90 RFC2268_context *ctx = context;
91 register int i, j;
92 u16 word0 = 0, word1 = 0, word2 = 0, word3 = 0;
93
94 word0 = (word0 << 8) | inbuf[1];
95 word0 = (word0 << 8) | inbuf[0];
96 word1 = (word1 << 8) | inbuf[3];
97 word1 = (word1 << 8) | inbuf[2];
98 word2 = (word2 << 8) | inbuf[5];
99 word2 = (word2 << 8) | inbuf[4];
100 word3 = (word3 << 8) | inbuf[7];
101 word3 = (word3 << 8) | inbuf[6];
102
103 for (i = 0; i < 16; i++)
104 {
105 j = i * 4;
106 /* For some reason I cannot combine those steps. */
107 word0 += (word1 & ~word3) + (word2 & word3) + ctx->S[j];
108 word0 = rotl16(word0, 1);
109
110 word1 += (word2 & ~word0) + (word3 & word0) + ctx->S[j + 1];
111 word1 = rotl16(word1, 2);
112
113 word2 += (word3 & ~word1) + (word0 & word1) + ctx->S[j + 2];
114 word2 = rotl16(word2, 3);
115
116 word3 += (word0 & ~word2) + (word1 & word2) + ctx->S[j + 3];
117 word3 = rotl16(word3, 5);
118
119 if (i == 4 || i == 10)
120 {
121 word0 += ctx->S[word3 & 63];
122 word1 += ctx->S[word0 & 63];
123 word2 += ctx->S[word1 & 63];
124 word3 += ctx->S[word2 & 63];
125 }
126
127 }
128
129 outbuf[0] = word0 & 255;
130 outbuf[1] = word0 >> 8;
131 outbuf[2] = word1 & 255;
132 outbuf[3] = word1 >> 8;
133 outbuf[4] = word2 & 255;
134 outbuf[5] = word2 >> 8;
135 outbuf[6] = word3 & 255;
136 outbuf[7] = word3 >> 8;
137}
138
139static void
140do_decrypt (void *context, unsigned char *outbuf, const unsigned char *inbuf)
141{
142 RFC2268_context *ctx = context;
143 register int i, j;
144 u16 word0 = 0, word1 = 0, word2 = 0, word3 = 0;
145
146 word0 = (word0 << 8) | inbuf[1];
147 word0 = (word0 << 8) | inbuf[0];
148 word1 = (word1 << 8) | inbuf[3];
149 word1 = (word1 << 8) | inbuf[2];
150 word2 = (word2 << 8) | inbuf[5];
151 word2 = (word2 << 8) | inbuf[4];
152 word3 = (word3 << 8) | inbuf[7];
153 word3 = (word3 << 8) | inbuf[6];
154
155 for (i = 15; i >= 0; i--)
156 {
157 j = i * 4;
158
159 word3 = rotr16(word3, 5);
160 word3 -= (word0 & ~word2) + (word1 & word2) + ctx->S[j + 3];
161
162 word2 = rotr16(word2, 3);
163 word2 -= (word3 & ~word1) + (word0 & word1) + ctx->S[j + 2];
164
165 word1 = rotr16(word1, 2);
166 word1 -= (word2 & ~word0) + (word3 & word0) + ctx->S[j + 1];
167
168 word0 = rotr16(word0, 1);
169 word0 -= (word1 & ~word3) + (word2 & word3) + ctx->S[j];
170
171 if (i == 5 || i == 11)
172 {
173 word3 = word3 - ctx->S[word2 & 63];
174 word2 = word2 - ctx->S[word1 & 63];
175 word1 = word1 - ctx->S[word0 & 63];
176 word0 = word0 - ctx->S[word3 & 63];
177 }
178
179 }
180
181 outbuf[0] = word0 & 255;
182 outbuf[1] = word0 >> 8;
183 outbuf[2] = word1 & 255;
184 outbuf[3] = word1 >> 8;
185 outbuf[4] = word2 & 255;
186 outbuf[5] = word2 >> 8;
187 outbuf[6] = word3 & 255;
188 outbuf[7] = word3 >> 8;
189}
190
191
192static gpg_err_code_t
193setkey_core (void *context, const unsigned char *key, unsigned int keylen, int with_phase2)
194{
195 static int initialized;
196 static const char *selftest_failed;
197 RFC2268_context *ctx = context;
198 unsigned int i;
199 unsigned char *S, x;
200 int len;
201 int bits = keylen * 8;
202
203 if (!initialized)
204 {
205 initialized = 1;
206 selftest_failed = selftest ();
207 if (selftest_failed)
208 log_error ("RFC2268 selftest failed (%s).\n", selftest_failed);
209 }
210 if (selftest_failed)
211 return GPG_ERR_SELFTEST_FAILED;
212
213 if (keylen < 40 / 8)/* We want at least 40 bits. */
214 return GPG_ERR_INV_KEYLEN;
215
216 S = (unsigned char *) ctx->S;
217
218 for (i = 0; i < keylen; i++)
219 S[i] = key[i];
220
221 for (i = keylen; i < 128; i++)
222 S[i] = rfc2268_sbox[(S[i - keylen] + S[i - 1]) & 255];
223
224 S[0] = rfc2268_sbox[S[0]];
225
226 /* Phase 2 - reduce effective key size to "bits". This was not
227 * discussed in Gutmann's paper. I've copied that from the public
228 * domain code posted in sci.crypt. */
229 if (with_phase2)
230 {
231 len = (bits + 7) >> 3;
232 i = 128 - len;
233 x = rfc2268_sbox[S[i] & (255 >> (7 & -bits))];
234 S[i] = x;
235
236 while (i--)
237 {
238 x = rfc2268_sbox[x ^ S[i + len]];
239 S[i] = x;
240 }
241 }
242
243 /* Make the expanded key, endian independent. */
244 for (i = 0; i < 64; i++)
245 ctx->S[i] = ( (u16) S[i * 2] | (((u16) S[i * 2 + 1]) << 8));
246
247 return 0;
248}
249
250static gpg_err_code_t
251do_setkey (void *context, const unsigned char *key, unsigned int keylen)
252{
253 return setkey_core (context, key, keylen, 1);
254}
255
256static const char *
257selftest (void)
258{
259 RFC2268_context ctx;
260 unsigned char scratch[16];
261
262 /* Test vectors from Peter Gutmann's paper. */
263 static unsigned char key_1[] =
264 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
265 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
266 };
267 static unsigned char plaintext_1[] =
268 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
269 static const unsigned char ciphertext_1[] =
270 { 0x1C, 0x19, 0x8A, 0x83, 0x8D, 0xF0, 0x28, 0xB7 };
271
272 static unsigned char key_2[] =
273 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
274 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
275 };
276 static unsigned char plaintext_2[] =
277 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
278 static unsigned char ciphertext_2[] =
279 { 0x50, 0xDC, 0x01, 0x62, 0xBD, 0x75, 0x7F, 0x31 };
280
281 /* This one was checked against libmcrypt's RFC2268. */
282 static unsigned char key_3[] =
283 { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
284 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
285 };
286 static unsigned char plaintext_3[] =
287 { 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
288 static unsigned char ciphertext_3[] =
289 { 0x8f, 0xd1, 0x03, 0x89, 0x33, 0x6b, 0xf9, 0x5e };
290
291
292 /* First test. */
293 setkey_core (&ctx, key_1, sizeof(key_1), 0);
294 do_encrypt (&ctx, scratch, plaintext_1);
295
296 if (memcmp (scratch, ciphertext_1, sizeof(ciphertext_1)))
297 return "RFC2268 encryption test 1 failed.";
298
299 setkey_core (&ctx, key_1, sizeof(key_1), 0);
300 do_decrypt (&ctx, scratch, scratch);
301 if (memcmp (scratch, plaintext_1, sizeof(plaintext_1)))
302 return "RFC2268 decryption test 1 failed.";
303
304 /* Second test. */
305 setkey_core (&ctx, key_2, sizeof(key_2), 0);
306 do_encrypt (&ctx, scratch, plaintext_2);
307 if (memcmp (scratch, ciphertext_2, sizeof(ciphertext_2)))
308 return "RFC2268 encryption test 2 failed.";
309
310 setkey_core (&ctx, key_2, sizeof(key_2), 0);
311 do_decrypt (&ctx, scratch, scratch);
312 if (memcmp (scratch, plaintext_2, sizeof(plaintext_2)))
313 return "RFC2268 decryption test 2 failed.";
314
315 /* Third test. */
316 setkey_core(&ctx, key_3, sizeof(key_3), 0);
317 do_encrypt(&ctx, scratch, plaintext_3);
318
319 if (memcmp(scratch, ciphertext_3, sizeof(ciphertext_3)))
320 return "RFC2268 encryption test 3 failed.";
321
322 setkey_core (&ctx, key_3, sizeof(key_3), 0);
323 do_decrypt (&ctx, scratch, scratch);
324 if (memcmp(scratch, plaintext_3, sizeof(plaintext_3)))
325 return "RFC2268 decryption test 3 failed.";
326
327 return NULL;
328}
329
330
331
332static gcry_cipher_oid_spec_t oids_rfc2268_40[] =
333 {
334 /*{ "1.2.840.113549.3.2", GCRY_CIPHER_MODE_CBC },*/
335 /* pbeWithSHAAnd40BitRC2_CBC */
336 { "1.2.840.113549.1.12.1.6", GCRY_CIPHER_MODE_CBC },
337 { NULL }
338 };
339
340gcry_cipher_spec_t _gcry_cipher_spec_rfc2268_40 = {
341 "RFC2268_40", NULL, oids_rfc2268_40,
342 RFC2268_BLOCKSIZE, 40, sizeof(RFC2268_context),
343 do_setkey, do_encrypt, do_decrypt
344};
345
diff --git a/pwmanager/libcrypt/cipher/rijndael.c b/pwmanager/libcrypt/cipher/rijndael.c
new file mode 100644
index 0000000..e53880f
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/rijndael.c
@@ -0,0 +1,2228 @@
1/* Rijndael (AES) for GnuPG
2 *Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 *******************************************************************
20 * The code here is based on the optimized implementation taken from
21 * http://www.esat.kuleuven.ac.be/~rijmen/rijndael/ on Oct 2, 2000,
22 * which carries this notice:
23 *------------------------------------------
24 * rijndael-alg-fst.c v2.3 April '2000
25 *
26 * Optimised ANSI C code
27 *
28 * authors: v1.0: Antoon Bosselaers
29 * v2.0: Vincent Rijmen
30 * v2.3: Paulo Barreto
31 *
32 * This code is placed in the public domain.
33 *------------------------------------------
34 */
35
36#include <config.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h> /* for memcmp() */
40
41#include "types.h" /* for byte and u32 typedefs */
42#include "g10lib.h"
43#include "cipher.h"
44
45 #define MAXKC (256/32)
46 #define MAXROUNDS 14
47
48
49static const char *selftest(void);
50
51typedef struct {
52 int ROUNDS; /* key-length-dependent number of rounds */
53 int decryption_prepared;
54 byte keySched[MAXROUNDS+1][4][4]; /* key schedule */
55 byte keySched2[MAXROUNDS+1][4][4]; /* key schedule */
56} RIJNDAEL_context;
57
58
59static const byte S[256] = {
60 99, 124, 119, 123, 242, 107, 111, 197,
61 48, 1, 103, 43, 254, 215, 171, 118,
62 202, 130, 201, 125, 250, 89, 71, 240,
63 173, 212, 162, 175, 156, 164, 114, 192,
64 183, 253, 147, 38, 54, 63, 247, 204,
65 52, 165, 229, 241, 113, 216, 49, 21,
66 4, 199, 35, 195, 24, 150, 5, 154,
67 7, 18, 128, 226, 235, 39, 178, 117,
68 9, 131, 44, 26, 27, 110, 90, 160,
69 82, 59, 214, 179, 41, 227, 47, 132,
70 83, 209, 0, 237, 32, 252, 177, 91,
71 106, 203, 190, 57, 74, 76, 88, 207,
72 208, 239, 170, 251, 67, 77, 51, 133,
73 69, 249, 2, 127, 80, 60, 159, 168,
74 81, 163, 64, 143, 146, 157, 56, 245,
75 188, 182, 218, 33, 16, 255, 243, 210,
76 205, 12, 19, 236, 95, 151, 68, 23,
77 196, 167, 126, 61, 100, 93, 25, 115,
78 96, 129, 79, 220, 34, 42, 144, 136,
79 70, 238, 184, 20, 222, 94, 11, 219,
80 224, 50, 58, 10, 73, 6, 36, 92,
81 194, 211, 172, 98, 145, 149, 228, 121,
82 231, 200, 55, 109, 141, 213, 78, 169,
83 108, 86, 244, 234, 101, 122, 174, 8,
84 186, 120, 37, 46, 28, 166, 180, 198,
85 232, 221, 116, 31, 75, 189, 139, 138,
86 112, 62, 181, 102, 72, 3, 246, 14,
87 97, 53, 87, 185, 134, 193, 29, 158,
88 225, 248, 152, 17, 105, 217, 142, 148,
89 155, 30, 135, 233, 206, 85, 40, 223,
90 140, 161, 137, 13, 191, 230, 66, 104,
91 65, 153, 45, 15, 176, 84, 187, 22
92};
93
94
95static const byte T1[256][4] = {
96 { 0xc6,0x63,0x63,0xa5 }, { 0xf8,0x7c,0x7c,0x84 },
97 { 0xee,0x77,0x77,0x99 }, { 0xf6,0x7b,0x7b,0x8d },
98 { 0xff,0xf2,0xf2,0x0d }, { 0xd6,0x6b,0x6b,0xbd },
99 { 0xde,0x6f,0x6f,0xb1 }, { 0x91,0xc5,0xc5,0x54 },
100 { 0x60,0x30,0x30,0x50 }, { 0x02,0x01,0x01,0x03 },
101 { 0xce,0x67,0x67,0xa9 }, { 0x56,0x2b,0x2b,0x7d },
102 { 0xe7,0xfe,0xfe,0x19 }, { 0xb5,0xd7,0xd7,0x62 },
103 { 0x4d,0xab,0xab,0xe6 }, { 0xec,0x76,0x76,0x9a },
104 { 0x8f,0xca,0xca,0x45 }, { 0x1f,0x82,0x82,0x9d },
105 { 0x89,0xc9,0xc9,0x40 }, { 0xfa,0x7d,0x7d,0x87 },
106 { 0xef,0xfa,0xfa,0x15 }, { 0xb2,0x59,0x59,0xeb },
107 { 0x8e,0x47,0x47,0xc9 }, { 0xfb,0xf0,0xf0,0x0b },
108 { 0x41,0xad,0xad,0xec }, { 0xb3,0xd4,0xd4,0x67 },
109 { 0x5f,0xa2,0xa2,0xfd }, { 0x45,0xaf,0xaf,0xea },
110 { 0x23,0x9c,0x9c,0xbf }, { 0x53,0xa4,0xa4,0xf7 },
111 { 0xe4,0x72,0x72,0x96 }, { 0x9b,0xc0,0xc0,0x5b },
112 { 0x75,0xb7,0xb7,0xc2 }, { 0xe1,0xfd,0xfd,0x1c },
113 { 0x3d,0x93,0x93,0xae }, { 0x4c,0x26,0x26,0x6a },
114 { 0x6c,0x36,0x36,0x5a }, { 0x7e,0x3f,0x3f,0x41 },
115 { 0xf5,0xf7,0xf7,0x02 }, { 0x83,0xcc,0xcc,0x4f },
116 { 0x68,0x34,0x34,0x5c }, { 0x51,0xa5,0xa5,0xf4 },
117 { 0xd1,0xe5,0xe5,0x34 }, { 0xf9,0xf1,0xf1,0x08 },
118 { 0xe2,0x71,0x71,0x93 }, { 0xab,0xd8,0xd8,0x73 },
119 { 0x62,0x31,0x31,0x53 }, { 0x2a,0x15,0x15,0x3f },
120 { 0x08,0x04,0x04,0x0c }, { 0x95,0xc7,0xc7,0x52 },
121 { 0x46,0x23,0x23,0x65 }, { 0x9d,0xc3,0xc3,0x5e },
122 { 0x30,0x18,0x18,0x28 }, { 0x37,0x96,0x96,0xa1 },
123 { 0x0a,0x05,0x05,0x0f }, { 0x2f,0x9a,0x9a,0xb5 },
124 { 0x0e,0x07,0x07,0x09 }, { 0x24,0x12,0x12,0x36 },
125 { 0x1b,0x80,0x80,0x9b }, { 0xdf,0xe2,0xe2,0x3d },
126 { 0xcd,0xeb,0xeb,0x26 }, { 0x4e,0x27,0x27,0x69 },
127 { 0x7f,0xb2,0xb2,0xcd }, { 0xea,0x75,0x75,0x9f },
128 { 0x12,0x09,0x09,0x1b }, { 0x1d,0x83,0x83,0x9e },
129 { 0x58,0x2c,0x2c,0x74 }, { 0x34,0x1a,0x1a,0x2e },
130 { 0x36,0x1b,0x1b,0x2d }, { 0xdc,0x6e,0x6e,0xb2 },
131 { 0xb4,0x5a,0x5a,0xee }, { 0x5b,0xa0,0xa0,0xfb },
132 { 0xa4,0x52,0x52,0xf6 }, { 0x76,0x3b,0x3b,0x4d },
133 { 0xb7,0xd6,0xd6,0x61 }, { 0x7d,0xb3,0xb3,0xce },
134 { 0x52,0x29,0x29,0x7b }, { 0xdd,0xe3,0xe3,0x3e },
135 { 0x5e,0x2f,0x2f,0x71 }, { 0x13,0x84,0x84,0x97 },
136 { 0xa6,0x53,0x53,0xf5 }, { 0xb9,0xd1,0xd1,0x68 },
137 { 0x00,0x00,0x00,0x00 }, { 0xc1,0xed,0xed,0x2c },
138 { 0x40,0x20,0x20,0x60 }, { 0xe3,0xfc,0xfc,0x1f },
139 { 0x79,0xb1,0xb1,0xc8 }, { 0xb6,0x5b,0x5b,0xed },
140 { 0xd4,0x6a,0x6a,0xbe }, { 0x8d,0xcb,0xcb,0x46 },
141 { 0x67,0xbe,0xbe,0xd9 }, { 0x72,0x39,0x39,0x4b },
142 { 0x94,0x4a,0x4a,0xde }, { 0x98,0x4c,0x4c,0xd4 },
143 { 0xb0,0x58,0x58,0xe8 }, { 0x85,0xcf,0xcf,0x4a },
144 { 0xbb,0xd0,0xd0,0x6b }, { 0xc5,0xef,0xef,0x2a },
145 { 0x4f,0xaa,0xaa,0xe5 }, { 0xed,0xfb,0xfb,0x16 },
146 { 0x86,0x43,0x43,0xc5 }, { 0x9a,0x4d,0x4d,0xd7 },
147 { 0x66,0x33,0x33,0x55 }, { 0x11,0x85,0x85,0x94 },
148 { 0x8a,0x45,0x45,0xcf }, { 0xe9,0xf9,0xf9,0x10 },
149 { 0x04,0x02,0x02,0x06 }, { 0xfe,0x7f,0x7f,0x81 },
150 { 0xa0,0x50,0x50,0xf0 }, { 0x78,0x3c,0x3c,0x44 },
151 { 0x25,0x9f,0x9f,0xba }, { 0x4b,0xa8,0xa8,0xe3 },
152 { 0xa2,0x51,0x51,0xf3 }, { 0x5d,0xa3,0xa3,0xfe },
153 { 0x80,0x40,0x40,0xc0 }, { 0x05,0x8f,0x8f,0x8a },
154 { 0x3f,0x92,0x92,0xad }, { 0x21,0x9d,0x9d,0xbc },
155 { 0x70,0x38,0x38,0x48 }, { 0xf1,0xf5,0xf5,0x04 },
156 { 0x63,0xbc,0xbc,0xdf }, { 0x77,0xb6,0xb6,0xc1 },
157 { 0xaf,0xda,0xda,0x75 }, { 0x42,0x21,0x21,0x63 },
158 { 0x20,0x10,0x10,0x30 }, { 0xe5,0xff,0xff,0x1a },
159 { 0xfd,0xf3,0xf3,0x0e }, { 0xbf,0xd2,0xd2,0x6d },
160 { 0x81,0xcd,0xcd,0x4c }, { 0x18,0x0c,0x0c,0x14 },
161 { 0x26,0x13,0x13,0x35 }, { 0xc3,0xec,0xec,0x2f },
162 { 0xbe,0x5f,0x5f,0xe1 }, { 0x35,0x97,0x97,0xa2 },
163 { 0x88,0x44,0x44,0xcc }, { 0x2e,0x17,0x17,0x39 },
164 { 0x93,0xc4,0xc4,0x57 }, { 0x55,0xa7,0xa7,0xf2 },
165 { 0xfc,0x7e,0x7e,0x82 }, { 0x7a,0x3d,0x3d,0x47 },
166 { 0xc8,0x64,0x64,0xac }, { 0xba,0x5d,0x5d,0xe7 },
167 { 0x32,0x19,0x19,0x2b }, { 0xe6,0x73,0x73,0x95 },
168 { 0xc0,0x60,0x60,0xa0 }, { 0x19,0x81,0x81,0x98 },
169 { 0x9e,0x4f,0x4f,0xd1 }, { 0xa3,0xdc,0xdc,0x7f },
170 { 0x44,0x22,0x22,0x66 }, { 0x54,0x2a,0x2a,0x7e },
171 { 0x3b,0x90,0x90,0xab }, { 0x0b,0x88,0x88,0x83 },
172 { 0x8c,0x46,0x46,0xca }, { 0xc7,0xee,0xee,0x29 },
173 { 0x6b,0xb8,0xb8,0xd3 }, { 0x28,0x14,0x14,0x3c },
174 { 0xa7,0xde,0xde,0x79 }, { 0xbc,0x5e,0x5e,0xe2 },
175 { 0x16,0x0b,0x0b,0x1d }, { 0xad,0xdb,0xdb,0x76 },
176 { 0xdb,0xe0,0xe0,0x3b }, { 0x64,0x32,0x32,0x56 },
177 { 0x74,0x3a,0x3a,0x4e }, { 0x14,0x0a,0x0a,0x1e },
178 { 0x92,0x49,0x49,0xdb }, { 0x0c,0x06,0x06,0x0a },
179 { 0x48,0x24,0x24,0x6c }, { 0xb8,0x5c,0x5c,0xe4 },
180 { 0x9f,0xc2,0xc2,0x5d }, { 0xbd,0xd3,0xd3,0x6e },
181 { 0x43,0xac,0xac,0xef }, { 0xc4,0x62,0x62,0xa6 },
182 { 0x39,0x91,0x91,0xa8 }, { 0x31,0x95,0x95,0xa4 },
183 { 0xd3,0xe4,0xe4,0x37 }, { 0xf2,0x79,0x79,0x8b },
184 { 0xd5,0xe7,0xe7,0x32 }, { 0x8b,0xc8,0xc8,0x43 },
185 { 0x6e,0x37,0x37,0x59 }, { 0xda,0x6d,0x6d,0xb7 },
186 { 0x01,0x8d,0x8d,0x8c }, { 0xb1,0xd5,0xd5,0x64 },
187 { 0x9c,0x4e,0x4e,0xd2 }, { 0x49,0xa9,0xa9,0xe0 },
188 { 0xd8,0x6c,0x6c,0xb4 }, { 0xac,0x56,0x56,0xfa },
189 { 0xf3,0xf4,0xf4,0x07 }, { 0xcf,0xea,0xea,0x25 },
190 { 0xca,0x65,0x65,0xaf }, { 0xf4,0x7a,0x7a,0x8e },
191 { 0x47,0xae,0xae,0xe9 }, { 0x10,0x08,0x08,0x18 },
192 { 0x6f,0xba,0xba,0xd5 }, { 0xf0,0x78,0x78,0x88 },
193 { 0x4a,0x25,0x25,0x6f }, { 0x5c,0x2e,0x2e,0x72 },
194 { 0x38,0x1c,0x1c,0x24 }, { 0x57,0xa6,0xa6,0xf1 },
195 { 0x73,0xb4,0xb4,0xc7 }, { 0x97,0xc6,0xc6,0x51 },
196 { 0xcb,0xe8,0xe8,0x23 }, { 0xa1,0xdd,0xdd,0x7c },
197 { 0xe8,0x74,0x74,0x9c }, { 0x3e,0x1f,0x1f,0x21 },
198 { 0x96,0x4b,0x4b,0xdd }, { 0x61,0xbd,0xbd,0xdc },
199 { 0x0d,0x8b,0x8b,0x86 }, { 0x0f,0x8a,0x8a,0x85 },
200 { 0xe0,0x70,0x70,0x90 }, { 0x7c,0x3e,0x3e,0x42 },
201 { 0x71,0xb5,0xb5,0xc4 }, { 0xcc,0x66,0x66,0xaa },
202 { 0x90,0x48,0x48,0xd8 }, { 0x06,0x03,0x03,0x05 },
203 { 0xf7,0xf6,0xf6,0x01 }, { 0x1c,0x0e,0x0e,0x12 },
204 { 0xc2,0x61,0x61,0xa3 }, { 0x6a,0x35,0x35,0x5f },
205 { 0xae,0x57,0x57,0xf9 }, { 0x69,0xb9,0xb9,0xd0 },
206 { 0x17,0x86,0x86,0x91 }, { 0x99,0xc1,0xc1,0x58 },
207 { 0x3a,0x1d,0x1d,0x27 }, { 0x27,0x9e,0x9e,0xb9 },
208 { 0xd9,0xe1,0xe1,0x38 }, { 0xeb,0xf8,0xf8,0x13 },
209 { 0x2b,0x98,0x98,0xb3 }, { 0x22,0x11,0x11,0x33 },
210 { 0xd2,0x69,0x69,0xbb }, { 0xa9,0xd9,0xd9,0x70 },
211 { 0x07,0x8e,0x8e,0x89 }, { 0x33,0x94,0x94,0xa7 },
212 { 0x2d,0x9b,0x9b,0xb6 }, { 0x3c,0x1e,0x1e,0x22 },
213 { 0x15,0x87,0x87,0x92 }, { 0xc9,0xe9,0xe9,0x20 },
214 { 0x87,0xce,0xce,0x49 }, { 0xaa,0x55,0x55,0xff },
215 { 0x50,0x28,0x28,0x78 }, { 0xa5,0xdf,0xdf,0x7a },
216 { 0x03,0x8c,0x8c,0x8f }, { 0x59,0xa1,0xa1,0xf8 },
217 { 0x09,0x89,0x89,0x80 }, { 0x1a,0x0d,0x0d,0x17 },
218 { 0x65,0xbf,0xbf,0xda }, { 0xd7,0xe6,0xe6,0x31 },
219 { 0x84,0x42,0x42,0xc6 }, { 0xd0,0x68,0x68,0xb8 },
220 { 0x82,0x41,0x41,0xc3 }, { 0x29,0x99,0x99,0xb0 },
221 { 0x5a,0x2d,0x2d,0x77 }, { 0x1e,0x0f,0x0f,0x11 },
222 { 0x7b,0xb0,0xb0,0xcb }, { 0xa8,0x54,0x54,0xfc },
223 { 0x6d,0xbb,0xbb,0xd6 }, { 0x2c,0x16,0x16,0x3a }
224};
225
226static const byte T2[256][4] = {
227 { 0xa5,0xc6,0x63,0x63 }, { 0x84,0xf8,0x7c,0x7c },
228 { 0x99,0xee,0x77,0x77 }, { 0x8d,0xf6,0x7b,0x7b },
229 { 0x0d,0xff,0xf2,0xf2 }, { 0xbd,0xd6,0x6b,0x6b },
230 { 0xb1,0xde,0x6f,0x6f }, { 0x54,0x91,0xc5,0xc5 },
231 { 0x50,0x60,0x30,0x30 }, { 0x03,0x02,0x01,0x01 },
232 { 0xa9,0xce,0x67,0x67 }, { 0x7d,0x56,0x2b,0x2b },
233 { 0x19,0xe7,0xfe,0xfe }, { 0x62,0xb5,0xd7,0xd7 },
234 { 0xe6,0x4d,0xab,0xab }, { 0x9a,0xec,0x76,0x76 },
235 { 0x45,0x8f,0xca,0xca }, { 0x9d,0x1f,0x82,0x82 },
236 { 0x40,0x89,0xc9,0xc9 }, { 0x87,0xfa,0x7d,0x7d },
237 { 0x15,0xef,0xfa,0xfa }, { 0xeb,0xb2,0x59,0x59 },
238 { 0xc9,0x8e,0x47,0x47 }, { 0x0b,0xfb,0xf0,0xf0 },
239 { 0xec,0x41,0xad,0xad }, { 0x67,0xb3,0xd4,0xd4 },
240 { 0xfd,0x5f,0xa2,0xa2 }, { 0xea,0x45,0xaf,0xaf },
241 { 0xbf,0x23,0x9c,0x9c }, { 0xf7,0x53,0xa4,0xa4 },
242 { 0x96,0xe4,0x72,0x72 }, { 0x5b,0x9b,0xc0,0xc0 },
243 { 0xc2,0x75,0xb7,0xb7 }, { 0x1c,0xe1,0xfd,0xfd },
244 { 0xae,0x3d,0x93,0x93 }, { 0x6a,0x4c,0x26,0x26 },
245 { 0x5a,0x6c,0x36,0x36 }, { 0x41,0x7e,0x3f,0x3f },
246 { 0x02,0xf5,0xf7,0xf7 }, { 0x4f,0x83,0xcc,0xcc },
247 { 0x5c,0x68,0x34,0x34 }, { 0xf4,0x51,0xa5,0xa5 },
248 { 0x34,0xd1,0xe5,0xe5 }, { 0x08,0xf9,0xf1,0xf1 },
249 { 0x93,0xe2,0x71,0x71 }, { 0x73,0xab,0xd8,0xd8 },
250 { 0x53,0x62,0x31,0x31 }, { 0x3f,0x2a,0x15,0x15 },
251 { 0x0c,0x08,0x04,0x04 }, { 0x52,0x95,0xc7,0xc7 },
252 { 0x65,0x46,0x23,0x23 }, { 0x5e,0x9d,0xc3,0xc3 },
253 { 0x28,0x30,0x18,0x18 }, { 0xa1,0x37,0x96,0x96 },
254 { 0x0f,0x0a,0x05,0x05 }, { 0xb5,0x2f,0x9a,0x9a },
255 { 0x09,0x0e,0x07,0x07 }, { 0x36,0x24,0x12,0x12 },
256 { 0x9b,0x1b,0x80,0x80 }, { 0x3d,0xdf,0xe2,0xe2 },
257 { 0x26,0xcd,0xeb,0xeb }, { 0x69,0x4e,0x27,0x27 },
258 { 0xcd,0x7f,0xb2,0xb2 }, { 0x9f,0xea,0x75,0x75 },
259 { 0x1b,0x12,0x09,0x09 }, { 0x9e,0x1d,0x83,0x83 },
260 { 0x74,0x58,0x2c,0x2c }, { 0x2e,0x34,0x1a,0x1a },
261 { 0x2d,0x36,0x1b,0x1b }, { 0xb2,0xdc,0x6e,0x6e },
262 { 0xee,0xb4,0x5a,0x5a }, { 0xfb,0x5b,0xa0,0xa0 },
263 { 0xf6,0xa4,0x52,0x52 }, { 0x4d,0x76,0x3b,0x3b },
264 { 0x61,0xb7,0xd6,0xd6 }, { 0xce,0x7d,0xb3,0xb3 },
265 { 0x7b,0x52,0x29,0x29 }, { 0x3e,0xdd,0xe3,0xe3 },
266 { 0x71,0x5e,0x2f,0x2f }, { 0x97,0x13,0x84,0x84 },
267 { 0xf5,0xa6,0x53,0x53 }, { 0x68,0xb9,0xd1,0xd1 },
268 { 0x00,0x00,0x00,0x00 }, { 0x2c,0xc1,0xed,0xed },
269 { 0x60,0x40,0x20,0x20 }, { 0x1f,0xe3,0xfc,0xfc },
270 { 0xc8,0x79,0xb1,0xb1 }, { 0xed,0xb6,0x5b,0x5b },
271 { 0xbe,0xd4,0x6a,0x6a }, { 0x46,0x8d,0xcb,0xcb },
272 { 0xd9,0x67,0xbe,0xbe }, { 0x4b,0x72,0x39,0x39 },
273 { 0xde,0x94,0x4a,0x4a }, { 0xd4,0x98,0x4c,0x4c },
274 { 0xe8,0xb0,0x58,0x58 }, { 0x4a,0x85,0xcf,0xcf },
275 { 0x6b,0xbb,0xd0,0xd0 }, { 0x2a,0xc5,0xef,0xef },
276 { 0xe5,0x4f,0xaa,0xaa }, { 0x16,0xed,0xfb,0xfb },
277 { 0xc5,0x86,0x43,0x43 }, { 0xd7,0x9a,0x4d,0x4d },
278 { 0x55,0x66,0x33,0x33 }, { 0x94,0x11,0x85,0x85 },
279 { 0xcf,0x8a,0x45,0x45 }, { 0x10,0xe9,0xf9,0xf9 },
280 { 0x06,0x04,0x02,0x02 }, { 0x81,0xfe,0x7f,0x7f },
281 { 0xf0,0xa0,0x50,0x50 }, { 0x44,0x78,0x3c,0x3c },
282 { 0xba,0x25,0x9f,0x9f }, { 0xe3,0x4b,0xa8,0xa8 },
283 { 0xf3,0xa2,0x51,0x51 }, { 0xfe,0x5d,0xa3,0xa3 },
284 { 0xc0,0x80,0x40,0x40 }, { 0x8a,0x05,0x8f,0x8f },
285 { 0xad,0x3f,0x92,0x92 }, { 0xbc,0x21,0x9d,0x9d },
286 { 0x48,0x70,0x38,0x38 }, { 0x04,0xf1,0xf5,0xf5 },
287 { 0xdf,0x63,0xbc,0xbc }, { 0xc1,0x77,0xb6,0xb6 },
288 { 0x75,0xaf,0xda,0xda }, { 0x63,0x42,0x21,0x21 },
289 { 0x30,0x20,0x10,0x10 }, { 0x1a,0xe5,0xff,0xff },
290 { 0x0e,0xfd,0xf3,0xf3 }, { 0x6d,0xbf,0xd2,0xd2 },
291 { 0x4c,0x81,0xcd,0xcd }, { 0x14,0x18,0x0c,0x0c },
292 { 0x35,0x26,0x13,0x13 }, { 0x2f,0xc3,0xec,0xec },
293 { 0xe1,0xbe,0x5f,0x5f }, { 0xa2,0x35,0x97,0x97 },
294 { 0xcc,0x88,0x44,0x44 }, { 0x39,0x2e,0x17,0x17 },
295 { 0x57,0x93,0xc4,0xc4 }, { 0xf2,0x55,0xa7,0xa7 },
296 { 0x82,0xfc,0x7e,0x7e }, { 0x47,0x7a,0x3d,0x3d },
297 { 0xac,0xc8,0x64,0x64 }, { 0xe7,0xba,0x5d,0x5d },
298 { 0x2b,0x32,0x19,0x19 }, { 0x95,0xe6,0x73,0x73 },
299 { 0xa0,0xc0,0x60,0x60 }, { 0x98,0x19,0x81,0x81 },
300 { 0xd1,0x9e,0x4f,0x4f }, { 0x7f,0xa3,0xdc,0xdc },
301 { 0x66,0x44,0x22,0x22 }, { 0x7e,0x54,0x2a,0x2a },
302 { 0xab,0x3b,0x90,0x90 }, { 0x83,0x0b,0x88,0x88 },
303 { 0xca,0x8c,0x46,0x46 }, { 0x29,0xc7,0xee,0xee },
304 { 0xd3,0x6b,0xb8,0xb8 }, { 0x3c,0x28,0x14,0x14 },
305 { 0x79,0xa7,0xde,0xde }, { 0xe2,0xbc,0x5e,0x5e },
306 { 0x1d,0x16,0x0b,0x0b }, { 0x76,0xad,0xdb,0xdb },
307 { 0x3b,0xdb,0xe0,0xe0 }, { 0x56,0x64,0x32,0x32 },
308 { 0x4e,0x74,0x3a,0x3a }, { 0x1e,0x14,0x0a,0x0a },
309 { 0xdb,0x92,0x49,0x49 }, { 0x0a,0x0c,0x06,0x06 },
310 { 0x6c,0x48,0x24,0x24 }, { 0xe4,0xb8,0x5c,0x5c },
311 { 0x5d,0x9f,0xc2,0xc2 }, { 0x6e,0xbd,0xd3,0xd3 },
312 { 0xef,0x43,0xac,0xac }, { 0xa6,0xc4,0x62,0x62 },
313 { 0xa8,0x39,0x91,0x91 }, { 0xa4,0x31,0x95,0x95 },
314 { 0x37,0xd3,0xe4,0xe4 }, { 0x8b,0xf2,0x79,0x79 },
315 { 0x32,0xd5,0xe7,0xe7 }, { 0x43,0x8b,0xc8,0xc8 },
316 { 0x59,0x6e,0x37,0x37 }, { 0xb7,0xda,0x6d,0x6d },
317 { 0x8c,0x01,0x8d,0x8d }, { 0x64,0xb1,0xd5,0xd5 },
318 { 0xd2,0x9c,0x4e,0x4e }, { 0xe0,0x49,0xa9,0xa9 },
319 { 0xb4,0xd8,0x6c,0x6c }, { 0xfa,0xac,0x56,0x56 },
320 { 0x07,0xf3,0xf4,0xf4 }, { 0x25,0xcf,0xea,0xea },
321 { 0xaf,0xca,0x65,0x65 }, { 0x8e,0xf4,0x7a,0x7a },
322 { 0xe9,0x47,0xae,0xae }, { 0x18,0x10,0x08,0x08 },
323 { 0xd5,0x6f,0xba,0xba }, { 0x88,0xf0,0x78,0x78 },
324 { 0x6f,0x4a,0x25,0x25 }, { 0x72,0x5c,0x2e,0x2e },
325 { 0x24,0x38,0x1c,0x1c }, { 0xf1,0x57,0xa6,0xa6 },
326 { 0xc7,0x73,0xb4,0xb4 }, { 0x51,0x97,0xc6,0xc6 },
327 { 0x23,0xcb,0xe8,0xe8 }, { 0x7c,0xa1,0xdd,0xdd },
328 { 0x9c,0xe8,0x74,0x74 }, { 0x21,0x3e,0x1f,0x1f },
329 { 0xdd,0x96,0x4b,0x4b }, { 0xdc,0x61,0xbd,0xbd },
330 { 0x86,0x0d,0x8b,0x8b }, { 0x85,0x0f,0x8a,0x8a },
331 { 0x90,0xe0,0x70,0x70 }, { 0x42,0x7c,0x3e,0x3e },
332 { 0xc4,0x71,0xb5,0xb5 }, { 0xaa,0xcc,0x66,0x66 },
333 { 0xd8,0x90,0x48,0x48 }, { 0x05,0x06,0x03,0x03 },
334 { 0x01,0xf7,0xf6,0xf6 }, { 0x12,0x1c,0x0e,0x0e },
335 { 0xa3,0xc2,0x61,0x61 }, { 0x5f,0x6a,0x35,0x35 },
336 { 0xf9,0xae,0x57,0x57 }, { 0xd0,0x69,0xb9,0xb9 },
337 { 0x91,0x17,0x86,0x86 }, { 0x58,0x99,0xc1,0xc1 },
338 { 0x27,0x3a,0x1d,0x1d }, { 0xb9,0x27,0x9e,0x9e },
339 { 0x38,0xd9,0xe1,0xe1 }, { 0x13,0xeb,0xf8,0xf8 },
340 { 0xb3,0x2b,0x98,0x98 }, { 0x33,0x22,0x11,0x11 },
341 { 0xbb,0xd2,0x69,0x69 }, { 0x70,0xa9,0xd9,0xd9 },
342 { 0x89,0x07,0x8e,0x8e }, { 0xa7,0x33,0x94,0x94 },
343 { 0xb6,0x2d,0x9b,0x9b }, { 0x22,0x3c,0x1e,0x1e },
344 { 0x92,0x15,0x87,0x87 }, { 0x20,0xc9,0xe9,0xe9 },
345 { 0x49,0x87,0xce,0xce }, { 0xff,0xaa,0x55,0x55 },
346 { 0x78,0x50,0x28,0x28 }, { 0x7a,0xa5,0xdf,0xdf },
347 { 0x8f,0x03,0x8c,0x8c }, { 0xf8,0x59,0xa1,0xa1 },
348 { 0x80,0x09,0x89,0x89 }, { 0x17,0x1a,0x0d,0x0d },
349 { 0xda,0x65,0xbf,0xbf }, { 0x31,0xd7,0xe6,0xe6 },
350 { 0xc6,0x84,0x42,0x42 }, { 0xb8,0xd0,0x68,0x68 },
351 { 0xc3,0x82,0x41,0x41 }, { 0xb0,0x29,0x99,0x99 },
352 { 0x77,0x5a,0x2d,0x2d }, { 0x11,0x1e,0x0f,0x0f },
353 { 0xcb,0x7b,0xb0,0xb0 }, { 0xfc,0xa8,0x54,0x54 },
354 { 0xd6,0x6d,0xbb,0xbb }, { 0x3a,0x2c,0x16,0x16 }
355};
356
357static const byte T3[256][4] = {
358 { 0x63,0xa5,0xc6,0x63 }, { 0x7c,0x84,0xf8,0x7c },
359 { 0x77,0x99,0xee,0x77 }, { 0x7b,0x8d,0xf6,0x7b },
360 { 0xf2,0x0d,0xff,0xf2 }, { 0x6b,0xbd,0xd6,0x6b },
361 { 0x6f,0xb1,0xde,0x6f }, { 0xc5,0x54,0x91,0xc5 },
362 { 0x30,0x50,0x60,0x30 }, { 0x01,0x03,0x02,0x01 },
363 { 0x67,0xa9,0xce,0x67 }, { 0x2b,0x7d,0x56,0x2b },
364 { 0xfe,0x19,0xe7,0xfe }, { 0xd7,0x62,0xb5,0xd7 },
365 { 0xab,0xe6,0x4d,0xab }, { 0x76,0x9a,0xec,0x76 },
366 { 0xca,0x45,0x8f,0xca }, { 0x82,0x9d,0x1f,0x82 },
367 { 0xc9,0x40,0x89,0xc9 }, { 0x7d,0x87,0xfa,0x7d },
368 { 0xfa,0x15,0xef,0xfa }, { 0x59,0xeb,0xb2,0x59 },
369 { 0x47,0xc9,0x8e,0x47 }, { 0xf0,0x0b,0xfb,0xf0 },
370 { 0xad,0xec,0x41,0xad }, { 0xd4,0x67,0xb3,0xd4 },
371 { 0xa2,0xfd,0x5f,0xa2 }, { 0xaf,0xea,0x45,0xaf },
372 { 0x9c,0xbf,0x23,0x9c }, { 0xa4,0xf7,0x53,0xa4 },
373 { 0x72,0x96,0xe4,0x72 }, { 0xc0,0x5b,0x9b,0xc0 },
374 { 0xb7,0xc2,0x75,0xb7 }, { 0xfd,0x1c,0xe1,0xfd },
375 { 0x93,0xae,0x3d,0x93 }, { 0x26,0x6a,0x4c,0x26 },
376 { 0x36,0x5a,0x6c,0x36 }, { 0x3f,0x41,0x7e,0x3f },
377 { 0xf7,0x02,0xf5,0xf7 }, { 0xcc,0x4f,0x83,0xcc },
378 { 0x34,0x5c,0x68,0x34 }, { 0xa5,0xf4,0x51,0xa5 },
379 { 0xe5,0x34,0xd1,0xe5 }, { 0xf1,0x08,0xf9,0xf1 },
380 { 0x71,0x93,0xe2,0x71 }, { 0xd8,0x73,0xab,0xd8 },
381 { 0x31,0x53,0x62,0x31 }, { 0x15,0x3f,0x2a,0x15 },
382 { 0x04,0x0c,0x08,0x04 }, { 0xc7,0x52,0x95,0xc7 },
383 { 0x23,0x65,0x46,0x23 }, { 0xc3,0x5e,0x9d,0xc3 },
384 { 0x18,0x28,0x30,0x18 }, { 0x96,0xa1,0x37,0x96 },
385 { 0x05,0x0f,0x0a,0x05 }, { 0x9a,0xb5,0x2f,0x9a },
386 { 0x07,0x09,0x0e,0x07 }, { 0x12,0x36,0x24,0x12 },
387 { 0x80,0x9b,0x1b,0x80 }, { 0xe2,0x3d,0xdf,0xe2 },
388 { 0xeb,0x26,0xcd,0xeb }, { 0x27,0x69,0x4e,0x27 },
389 { 0xb2,0xcd,0x7f,0xb2 }, { 0x75,0x9f,0xea,0x75 },
390 { 0x09,0x1b,0x12,0x09 }, { 0x83,0x9e,0x1d,0x83 },
391 { 0x2c,0x74,0x58,0x2c }, { 0x1a,0x2e,0x34,0x1a },
392 { 0x1b,0x2d,0x36,0x1b }, { 0x6e,0xb2,0xdc,0x6e },
393 { 0x5a,0xee,0xb4,0x5a }, { 0xa0,0xfb,0x5b,0xa0 },
394 { 0x52,0xf6,0xa4,0x52 }, { 0x3b,0x4d,0x76,0x3b },
395 { 0xd6,0x61,0xb7,0xd6 }, { 0xb3,0xce,0x7d,0xb3 },
396 { 0x29,0x7b,0x52,0x29 }, { 0xe3,0x3e,0xdd,0xe3 },
397 { 0x2f,0x71,0x5e,0x2f }, { 0x84,0x97,0x13,0x84 },
398 { 0x53,0xf5,0xa6,0x53 }, { 0xd1,0x68,0xb9,0xd1 },
399 { 0x00,0x00,0x00,0x00 }, { 0xed,0x2c,0xc1,0xed },
400 { 0x20,0x60,0x40,0x20 }, { 0xfc,0x1f,0xe3,0xfc },
401 { 0xb1,0xc8,0x79,0xb1 }, { 0x5b,0xed,0xb6,0x5b },
402 { 0x6a,0xbe,0xd4,0x6a }, { 0xcb,0x46,0x8d,0xcb },
403 { 0xbe,0xd9,0x67,0xbe }, { 0x39,0x4b,0x72,0x39 },
404 { 0x4a,0xde,0x94,0x4a }, { 0x4c,0xd4,0x98,0x4c },
405 { 0x58,0xe8,0xb0,0x58 }, { 0xcf,0x4a,0x85,0xcf },
406 { 0xd0,0x6b,0xbb,0xd0 }, { 0xef,0x2a,0xc5,0xef },
407 { 0xaa,0xe5,0x4f,0xaa }, { 0xfb,0x16,0xed,0xfb },
408 { 0x43,0xc5,0x86,0x43 }, { 0x4d,0xd7,0x9a,0x4d },
409 { 0x33,0x55,0x66,0x33 }, { 0x85,0x94,0x11,0x85 },
410 { 0x45,0xcf,0x8a,0x45 }, { 0xf9,0x10,0xe9,0xf9 },
411 { 0x02,0x06,0x04,0x02 }, { 0x7f,0x81,0xfe,0x7f },
412 { 0x50,0xf0,0xa0,0x50 }, { 0x3c,0x44,0x78,0x3c },
413 { 0x9f,0xba,0x25,0x9f }, { 0xa8,0xe3,0x4b,0xa8 },
414 { 0x51,0xf3,0xa2,0x51 }, { 0xa3,0xfe,0x5d,0xa3 },
415 { 0x40,0xc0,0x80,0x40 }, { 0x8f,0x8a,0x05,0x8f },
416 { 0x92,0xad,0x3f,0x92 }, { 0x9d,0xbc,0x21,0x9d },
417 { 0x38,0x48,0x70,0x38 }, { 0xf5,0x04,0xf1,0xf5 },
418 { 0xbc,0xdf,0x63,0xbc }, { 0xb6,0xc1,0x77,0xb6 },
419 { 0xda,0x75,0xaf,0xda }, { 0x21,0x63,0x42,0x21 },
420 { 0x10,0x30,0x20,0x10 }, { 0xff,0x1a,0xe5,0xff },
421 { 0xf3,0x0e,0xfd,0xf3 }, { 0xd2,0x6d,0xbf,0xd2 },
422 { 0xcd,0x4c,0x81,0xcd }, { 0x0c,0x14,0x18,0x0c },
423 { 0x13,0x35,0x26,0x13 }, { 0xec,0x2f,0xc3,0xec },
424 { 0x5f,0xe1,0xbe,0x5f }, { 0x97,0xa2,0x35,0x97 },
425 { 0x44,0xcc,0x88,0x44 }, { 0x17,0x39,0x2e,0x17 },
426 { 0xc4,0x57,0x93,0xc4 }, { 0xa7,0xf2,0x55,0xa7 },
427 { 0x7e,0x82,0xfc,0x7e }, { 0x3d,0x47,0x7a,0x3d },
428 { 0x64,0xac,0xc8,0x64 }, { 0x5d,0xe7,0xba,0x5d },
429 { 0x19,0x2b,0x32,0x19 }, { 0x73,0x95,0xe6,0x73 },
430 { 0x60,0xa0,0xc0,0x60 }, { 0x81,0x98,0x19,0x81 },
431 { 0x4f,0xd1,0x9e,0x4f }, { 0xdc,0x7f,0xa3,0xdc },
432 { 0x22,0x66,0x44,0x22 }, { 0x2a,0x7e,0x54,0x2a },
433 { 0x90,0xab,0x3b,0x90 }, { 0x88,0x83,0x0b,0x88 },
434 { 0x46,0xca,0x8c,0x46 }, { 0xee,0x29,0xc7,0xee },
435 { 0xb8,0xd3,0x6b,0xb8 }, { 0x14,0x3c,0x28,0x14 },
436 { 0xde,0x79,0xa7,0xde }, { 0x5e,0xe2,0xbc,0x5e },
437 { 0x0b,0x1d,0x16,0x0b }, { 0xdb,0x76,0xad,0xdb },
438 { 0xe0,0x3b,0xdb,0xe0 }, { 0x32,0x56,0x64,0x32 },
439 { 0x3a,0x4e,0x74,0x3a }, { 0x0a,0x1e,0x14,0x0a },
440 { 0x49,0xdb,0x92,0x49 }, { 0x06,0x0a,0x0c,0x06 },
441 { 0x24,0x6c,0x48,0x24 }, { 0x5c,0xe4,0xb8,0x5c },
442 { 0xc2,0x5d,0x9f,0xc2 }, { 0xd3,0x6e,0xbd,0xd3 },
443 { 0xac,0xef,0x43,0xac }, { 0x62,0xa6,0xc4,0x62 },
444 { 0x91,0xa8,0x39,0x91 }, { 0x95,0xa4,0x31,0x95 },
445 { 0xe4,0x37,0xd3,0xe4 }, { 0x79,0x8b,0xf2,0x79 },
446 { 0xe7,0x32,0xd5,0xe7 }, { 0xc8,0x43,0x8b,0xc8 },
447 { 0x37,0x59,0x6e,0x37 }, { 0x6d,0xb7,0xda,0x6d },
448 { 0x8d,0x8c,0x01,0x8d }, { 0xd5,0x64,0xb1,0xd5 },
449 { 0x4e,0xd2,0x9c,0x4e }, { 0xa9,0xe0,0x49,0xa9 },
450 { 0x6c,0xb4,0xd8,0x6c }, { 0x56,0xfa,0xac,0x56 },
451 { 0xf4,0x07,0xf3,0xf4 }, { 0xea,0x25,0xcf,0xea },
452 { 0x65,0xaf,0xca,0x65 }, { 0x7a,0x8e,0xf4,0x7a },
453 { 0xae,0xe9,0x47,0xae }, { 0x08,0x18,0x10,0x08 },
454 { 0xba,0xd5,0x6f,0xba }, { 0x78,0x88,0xf0,0x78 },
455 { 0x25,0x6f,0x4a,0x25 }, { 0x2e,0x72,0x5c,0x2e },
456 { 0x1c,0x24,0x38,0x1c }, { 0xa6,0xf1,0x57,0xa6 },
457 { 0xb4,0xc7,0x73,0xb4 }, { 0xc6,0x51,0x97,0xc6 },
458 { 0xe8,0x23,0xcb,0xe8 }, { 0xdd,0x7c,0xa1,0xdd },
459 { 0x74,0x9c,0xe8,0x74 }, { 0x1f,0x21,0x3e,0x1f },
460 { 0x4b,0xdd,0x96,0x4b }, { 0xbd,0xdc,0x61,0xbd },
461 { 0x8b,0x86,0x0d,0x8b }, { 0x8a,0x85,0x0f,0x8a },
462 { 0x70,0x90,0xe0,0x70 }, { 0x3e,0x42,0x7c,0x3e },
463 { 0xb5,0xc4,0x71,0xb5 }, { 0x66,0xaa,0xcc,0x66 },
464 { 0x48,0xd8,0x90,0x48 }, { 0x03,0x05,0x06,0x03 },
465 { 0xf6,0x01,0xf7,0xf6 }, { 0x0e,0x12,0x1c,0x0e },
466 { 0x61,0xa3,0xc2,0x61 }, { 0x35,0x5f,0x6a,0x35 },
467 { 0x57,0xf9,0xae,0x57 }, { 0xb9,0xd0,0x69,0xb9 },
468 { 0x86,0x91,0x17,0x86 }, { 0xc1,0x58,0x99,0xc1 },
469 { 0x1d,0x27,0x3a,0x1d }, { 0x9e,0xb9,0x27,0x9e },
470 { 0xe1,0x38,0xd9,0xe1 }, { 0xf8,0x13,0xeb,0xf8 },
471 { 0x98,0xb3,0x2b,0x98 }, { 0x11,0x33,0x22,0x11 },
472 { 0x69,0xbb,0xd2,0x69 }, { 0xd9,0x70,0xa9,0xd9 },
473 { 0x8e,0x89,0x07,0x8e }, { 0x94,0xa7,0x33,0x94 },
474 { 0x9b,0xb6,0x2d,0x9b }, { 0x1e,0x22,0x3c,0x1e },
475 { 0x87,0x92,0x15,0x87 }, { 0xe9,0x20,0xc9,0xe9 },
476 { 0xce,0x49,0x87,0xce }, { 0x55,0xff,0xaa,0x55 },
477 { 0x28,0x78,0x50,0x28 }, { 0xdf,0x7a,0xa5,0xdf },
478 { 0x8c,0x8f,0x03,0x8c }, { 0xa1,0xf8,0x59,0xa1 },
479 { 0x89,0x80,0x09,0x89 }, { 0x0d,0x17,0x1a,0x0d },
480 { 0xbf,0xda,0x65,0xbf }, { 0xe6,0x31,0xd7,0xe6 },
481 { 0x42,0xc6,0x84,0x42 }, { 0x68,0xb8,0xd0,0x68 },
482 { 0x41,0xc3,0x82,0x41 }, { 0x99,0xb0,0x29,0x99 },
483 { 0x2d,0x77,0x5a,0x2d }, { 0x0f,0x11,0x1e,0x0f },
484 { 0xb0,0xcb,0x7b,0xb0 }, { 0x54,0xfc,0xa8,0x54 },
485 { 0xbb,0xd6,0x6d,0xbb }, { 0x16,0x3a,0x2c,0x16 }
486};
487
488static const byte T4[256][4] = {
489 { 0x63,0x63,0xa5,0xc6 }, { 0x7c,0x7c,0x84,0xf8 },
490 { 0x77,0x77,0x99,0xee }, { 0x7b,0x7b,0x8d,0xf6 },
491 { 0xf2,0xf2,0x0d,0xff }, { 0x6b,0x6b,0xbd,0xd6 },
492 { 0x6f,0x6f,0xb1,0xde }, { 0xc5,0xc5,0x54,0x91 },
493 { 0x30,0x30,0x50,0x60 }, { 0x01,0x01,0x03,0x02 },
494 { 0x67,0x67,0xa9,0xce }, { 0x2b,0x2b,0x7d,0x56 },
495 { 0xfe,0xfe,0x19,0xe7 }, { 0xd7,0xd7,0x62,0xb5 },
496 { 0xab,0xab,0xe6,0x4d }, { 0x76,0x76,0x9a,0xec },
497 { 0xca,0xca,0x45,0x8f }, { 0x82,0x82,0x9d,0x1f },
498 { 0xc9,0xc9,0x40,0x89 }, { 0x7d,0x7d,0x87,0xfa },
499 { 0xfa,0xfa,0x15,0xef }, { 0x59,0x59,0xeb,0xb2 },
500 { 0x47,0x47,0xc9,0x8e }, { 0xf0,0xf0,0x0b,0xfb },
501 { 0xad,0xad,0xec,0x41 }, { 0xd4,0xd4,0x67,0xb3 },
502 { 0xa2,0xa2,0xfd,0x5f }, { 0xaf,0xaf,0xea,0x45 },
503 { 0x9c,0x9c,0xbf,0x23 }, { 0xa4,0xa4,0xf7,0x53 },
504 { 0x72,0x72,0x96,0xe4 }, { 0xc0,0xc0,0x5b,0x9b },
505 { 0xb7,0xb7,0xc2,0x75 }, { 0xfd,0xfd,0x1c,0xe1 },
506 { 0x93,0x93,0xae,0x3d }, { 0x26,0x26,0x6a,0x4c },
507 { 0x36,0x36,0x5a,0x6c }, { 0x3f,0x3f,0x41,0x7e },
508 { 0xf7,0xf7,0x02,0xf5 }, { 0xcc,0xcc,0x4f,0x83 },
509 { 0x34,0x34,0x5c,0x68 }, { 0xa5,0xa5,0xf4,0x51 },
510 { 0xe5,0xe5,0x34,0xd1 }, { 0xf1,0xf1,0x08,0xf9 },
511 { 0x71,0x71,0x93,0xe2 }, { 0xd8,0xd8,0x73,0xab },
512 { 0x31,0x31,0x53,0x62 }, { 0x15,0x15,0x3f,0x2a },
513 { 0x04,0x04,0x0c,0x08 }, { 0xc7,0xc7,0x52,0x95 },
514 { 0x23,0x23,0x65,0x46 }, { 0xc3,0xc3,0x5e,0x9d },
515 { 0x18,0x18,0x28,0x30 }, { 0x96,0x96,0xa1,0x37 },
516 { 0x05,0x05,0x0f,0x0a }, { 0x9a,0x9a,0xb5,0x2f },
517 { 0x07,0x07,0x09,0x0e }, { 0x12,0x12,0x36,0x24 },
518 { 0x80,0x80,0x9b,0x1b }, { 0xe2,0xe2,0x3d,0xdf },
519 { 0xeb,0xeb,0x26,0xcd }, { 0x27,0x27,0x69,0x4e },
520 { 0xb2,0xb2,0xcd,0x7f }, { 0x75,0x75,0x9f,0xea },
521 { 0x09,0x09,0x1b,0x12 }, { 0x83,0x83,0x9e,0x1d },
522 { 0x2c,0x2c,0x74,0x58 }, { 0x1a,0x1a,0x2e,0x34 },
523 { 0x1b,0x1b,0x2d,0x36 }, { 0x6e,0x6e,0xb2,0xdc },
524 { 0x5a,0x5a,0xee,0xb4 }, { 0xa0,0xa0,0xfb,0x5b },
525 { 0x52,0x52,0xf6,0xa4 }, { 0x3b,0x3b,0x4d,0x76 },
526 { 0xd6,0xd6,0x61,0xb7 }, { 0xb3,0xb3,0xce,0x7d },
527 { 0x29,0x29,0x7b,0x52 }, { 0xe3,0xe3,0x3e,0xdd },
528 { 0x2f,0x2f,0x71,0x5e }, { 0x84,0x84,0x97,0x13 },
529 { 0x53,0x53,0xf5,0xa6 }, { 0xd1,0xd1,0x68,0xb9 },
530 { 0x00,0x00,0x00,0x00 }, { 0xed,0xed,0x2c,0xc1 },
531 { 0x20,0x20,0x60,0x40 }, { 0xfc,0xfc,0x1f,0xe3 },
532 { 0xb1,0xb1,0xc8,0x79 }, { 0x5b,0x5b,0xed,0xb6 },
533 { 0x6a,0x6a,0xbe,0xd4 }, { 0xcb,0xcb,0x46,0x8d },
534 { 0xbe,0xbe,0xd9,0x67 }, { 0x39,0x39,0x4b,0x72 },
535 { 0x4a,0x4a,0xde,0x94 }, { 0x4c,0x4c,0xd4,0x98 },
536 { 0x58,0x58,0xe8,0xb0 }, { 0xcf,0xcf,0x4a,0x85 },
537 { 0xd0,0xd0,0x6b,0xbb }, { 0xef,0xef,0x2a,0xc5 },
538 { 0xaa,0xaa,0xe5,0x4f }, { 0xfb,0xfb,0x16,0xed },
539 { 0x43,0x43,0xc5,0x86 }, { 0x4d,0x4d,0xd7,0x9a },
540 { 0x33,0x33,0x55,0x66 }, { 0x85,0x85,0x94,0x11 },
541 { 0x45,0x45,0xcf,0x8a }, { 0xf9,0xf9,0x10,0xe9 },
542 { 0x02,0x02,0x06,0x04 }, { 0x7f,0x7f,0x81,0xfe },
543 { 0x50,0x50,0xf0,0xa0 }, { 0x3c,0x3c,0x44,0x78 },
544 { 0x9f,0x9f,0xba,0x25 }, { 0xa8,0xa8,0xe3,0x4b },
545 { 0x51,0x51,0xf3,0xa2 }, { 0xa3,0xa3,0xfe,0x5d },
546 { 0x40,0x40,0xc0,0x80 }, { 0x8f,0x8f,0x8a,0x05 },
547 { 0x92,0x92,0xad,0x3f }, { 0x9d,0x9d,0xbc,0x21 },
548 { 0x38,0x38,0x48,0x70 }, { 0xf5,0xf5,0x04,0xf1 },
549 { 0xbc,0xbc,0xdf,0x63 }, { 0xb6,0xb6,0xc1,0x77 },
550 { 0xda,0xda,0x75,0xaf }, { 0x21,0x21,0x63,0x42 },
551 { 0x10,0x10,0x30,0x20 }, { 0xff,0xff,0x1a,0xe5 },
552 { 0xf3,0xf3,0x0e,0xfd }, { 0xd2,0xd2,0x6d,0xbf },
553 { 0xcd,0xcd,0x4c,0x81 }, { 0x0c,0x0c,0x14,0x18 },
554 { 0x13,0x13,0x35,0x26 }, { 0xec,0xec,0x2f,0xc3 },
555 { 0x5f,0x5f,0xe1,0xbe }, { 0x97,0x97,0xa2,0x35 },
556 { 0x44,0x44,0xcc,0x88 }, { 0x17,0x17,0x39,0x2e },
557 { 0xc4,0xc4,0x57,0x93 }, { 0xa7,0xa7,0xf2,0x55 },
558 { 0x7e,0x7e,0x82,0xfc }, { 0x3d,0x3d,0x47,0x7a },
559 { 0x64,0x64,0xac,0xc8 }, { 0x5d,0x5d,0xe7,0xba },
560 { 0x19,0x19,0x2b,0x32 }, { 0x73,0x73,0x95,0xe6 },
561 { 0x60,0x60,0xa0,0xc0 }, { 0x81,0x81,0x98,0x19 },
562 { 0x4f,0x4f,0xd1,0x9e }, { 0xdc,0xdc,0x7f,0xa3 },
563 { 0x22,0x22,0x66,0x44 }, { 0x2a,0x2a,0x7e,0x54 },
564 { 0x90,0x90,0xab,0x3b }, { 0x88,0x88,0x83,0x0b },
565 { 0x46,0x46,0xca,0x8c }, { 0xee,0xee,0x29,0xc7 },
566 { 0xb8,0xb8,0xd3,0x6b }, { 0x14,0x14,0x3c,0x28 },
567 { 0xde,0xde,0x79,0xa7 }, { 0x5e,0x5e,0xe2,0xbc },
568 { 0x0b,0x0b,0x1d,0x16 }, { 0xdb,0xdb,0x76,0xad },
569 { 0xe0,0xe0,0x3b,0xdb }, { 0x32,0x32,0x56,0x64 },
570 { 0x3a,0x3a,0x4e,0x74 }, { 0x0a,0x0a,0x1e,0x14 },
571 { 0x49,0x49,0xdb,0x92 }, { 0x06,0x06,0x0a,0x0c },
572 { 0x24,0x24,0x6c,0x48 }, { 0x5c,0x5c,0xe4,0xb8 },
573 { 0xc2,0xc2,0x5d,0x9f }, { 0xd3,0xd3,0x6e,0xbd },
574 { 0xac,0xac,0xef,0x43 }, { 0x62,0x62,0xa6,0xc4 },
575 { 0x91,0x91,0xa8,0x39 }, { 0x95,0x95,0xa4,0x31 },
576 { 0xe4,0xe4,0x37,0xd3 }, { 0x79,0x79,0x8b,0xf2 },
577 { 0xe7,0xe7,0x32,0xd5 }, { 0xc8,0xc8,0x43,0x8b },
578 { 0x37,0x37,0x59,0x6e }, { 0x6d,0x6d,0xb7,0xda },
579 { 0x8d,0x8d,0x8c,0x01 }, { 0xd5,0xd5,0x64,0xb1 },
580 { 0x4e,0x4e,0xd2,0x9c }, { 0xa9,0xa9,0xe0,0x49 },
581 { 0x6c,0x6c,0xb4,0xd8 }, { 0x56,0x56,0xfa,0xac },
582 { 0xf4,0xf4,0x07,0xf3 }, { 0xea,0xea,0x25,0xcf },
583 { 0x65,0x65,0xaf,0xca }, { 0x7a,0x7a,0x8e,0xf4 },
584 { 0xae,0xae,0xe9,0x47 }, { 0x08,0x08,0x18,0x10 },
585 { 0xba,0xba,0xd5,0x6f }, { 0x78,0x78,0x88,0xf0 },
586 { 0x25,0x25,0x6f,0x4a }, { 0x2e,0x2e,0x72,0x5c },
587 { 0x1c,0x1c,0x24,0x38 }, { 0xa6,0xa6,0xf1,0x57 },
588 { 0xb4,0xb4,0xc7,0x73 }, { 0xc6,0xc6,0x51,0x97 },
589 { 0xe8,0xe8,0x23,0xcb }, { 0xdd,0xdd,0x7c,0xa1 },
590 { 0x74,0x74,0x9c,0xe8 }, { 0x1f,0x1f,0x21,0x3e },
591 { 0x4b,0x4b,0xdd,0x96 }, { 0xbd,0xbd,0xdc,0x61 },
592 { 0x8b,0x8b,0x86,0x0d }, { 0x8a,0x8a,0x85,0x0f },
593 { 0x70,0x70,0x90,0xe0 }, { 0x3e,0x3e,0x42,0x7c },
594 { 0xb5,0xb5,0xc4,0x71 }, { 0x66,0x66,0xaa,0xcc },
595 { 0x48,0x48,0xd8,0x90 }, { 0x03,0x03,0x05,0x06 },
596 { 0xf6,0xf6,0x01,0xf7 }, { 0x0e,0x0e,0x12,0x1c },
597 { 0x61,0x61,0xa3,0xc2 }, { 0x35,0x35,0x5f,0x6a },
598 { 0x57,0x57,0xf9,0xae }, { 0xb9,0xb9,0xd0,0x69 },
599 { 0x86,0x86,0x91,0x17 }, { 0xc1,0xc1,0x58,0x99 },
600 { 0x1d,0x1d,0x27,0x3a }, { 0x9e,0x9e,0xb9,0x27 },
601 { 0xe1,0xe1,0x38,0xd9 }, { 0xf8,0xf8,0x13,0xeb },
602 { 0x98,0x98,0xb3,0x2b }, { 0x11,0x11,0x33,0x22 },
603 { 0x69,0x69,0xbb,0xd2 }, { 0xd9,0xd9,0x70,0xa9 },
604 { 0x8e,0x8e,0x89,0x07 }, { 0x94,0x94,0xa7,0x33 },
605 { 0x9b,0x9b,0xb6,0x2d }, { 0x1e,0x1e,0x22,0x3c },
606 { 0x87,0x87,0x92,0x15 }, { 0xe9,0xe9,0x20,0xc9 },
607 { 0xce,0xce,0x49,0x87 }, { 0x55,0x55,0xff,0xaa },
608 { 0x28,0x28,0x78,0x50 }, { 0xdf,0xdf,0x7a,0xa5 },
609 { 0x8c,0x8c,0x8f,0x03 }, { 0xa1,0xa1,0xf8,0x59 },
610 { 0x89,0x89,0x80,0x09 }, { 0x0d,0x0d,0x17,0x1a },
611 { 0xbf,0xbf,0xda,0x65 }, { 0xe6,0xe6,0x31,0xd7 },
612 { 0x42,0x42,0xc6,0x84 }, { 0x68,0x68,0xb8,0xd0 },
613 { 0x41,0x41,0xc3,0x82 }, { 0x99,0x99,0xb0,0x29 },
614 { 0x2d,0x2d,0x77,0x5a }, { 0x0f,0x0f,0x11,0x1e },
615 { 0xb0,0xb0,0xcb,0x7b }, { 0x54,0x54,0xfc,0xa8 },
616 { 0xbb,0xbb,0xd6,0x6d }, { 0x16,0x16,0x3a,0x2c }
617};
618
619static const byte T5[256][4] = {
620 { 0x51,0xf4,0xa7,0x50 }, { 0x7e,0x41,0x65,0x53 },
621 { 0x1a,0x17,0xa4,0xc3 }, { 0x3a,0x27,0x5e,0x96 },
622 { 0x3b,0xab,0x6b,0xcb }, { 0x1f,0x9d,0x45,0xf1 },
623 { 0xac,0xfa,0x58,0xab }, { 0x4b,0xe3,0x03,0x93 },
624 { 0x20,0x30,0xfa,0x55 }, { 0xad,0x76,0x6d,0xf6 },
625 { 0x88,0xcc,0x76,0x91 }, { 0xf5,0x02,0x4c,0x25 },
626 { 0x4f,0xe5,0xd7,0xfc }, { 0xc5,0x2a,0xcb,0xd7 },
627 { 0x26,0x35,0x44,0x80 }, { 0xb5,0x62,0xa3,0x8f },
628 { 0xde,0xb1,0x5a,0x49 }, { 0x25,0xba,0x1b,0x67 },
629 { 0x45,0xea,0x0e,0x98 }, { 0x5d,0xfe,0xc0,0xe1 },
630 { 0xc3,0x2f,0x75,0x02 }, { 0x81,0x4c,0xf0,0x12 },
631 { 0x8d,0x46,0x97,0xa3 }, { 0x6b,0xd3,0xf9,0xc6 },
632 { 0x03,0x8f,0x5f,0xe7 }, { 0x15,0x92,0x9c,0x95 },
633 { 0xbf,0x6d,0x7a,0xeb }, { 0x95,0x52,0x59,0xda },
634 { 0xd4,0xbe,0x83,0x2d }, { 0x58,0x74,0x21,0xd3 },
635 { 0x49,0xe0,0x69,0x29 }, { 0x8e,0xc9,0xc8,0x44 },
636 { 0x75,0xc2,0x89,0x6a }, { 0xf4,0x8e,0x79,0x78 },
637 { 0x99,0x58,0x3e,0x6b }, { 0x27,0xb9,0x71,0xdd },
638 { 0xbe,0xe1,0x4f,0xb6 }, { 0xf0,0x88,0xad,0x17 },
639 { 0xc9,0x20,0xac,0x66 }, { 0x7d,0xce,0x3a,0xb4 },
640 { 0x63,0xdf,0x4a,0x18 }, { 0xe5,0x1a,0x31,0x82 },
641 { 0x97,0x51,0x33,0x60 }, { 0x62,0x53,0x7f,0x45 },
642 { 0xb1,0x64,0x77,0xe0 }, { 0xbb,0x6b,0xae,0x84 },
643 { 0xfe,0x81,0xa0,0x1c }, { 0xf9,0x08,0x2b,0x94 },
644 { 0x70,0x48,0x68,0x58 }, { 0x8f,0x45,0xfd,0x19 },
645 { 0x94,0xde,0x6c,0x87 }, { 0x52,0x7b,0xf8,0xb7 },
646 { 0xab,0x73,0xd3,0x23 }, { 0x72,0x4b,0x02,0xe2 },
647 { 0xe3,0x1f,0x8f,0x57 }, { 0x66,0x55,0xab,0x2a },
648 { 0xb2,0xeb,0x28,0x07 }, { 0x2f,0xb5,0xc2,0x03 },
649 { 0x86,0xc5,0x7b,0x9a }, { 0xd3,0x37,0x08,0xa5 },
650 { 0x30,0x28,0x87,0xf2 }, { 0x23,0xbf,0xa5,0xb2 },
651 { 0x02,0x03,0x6a,0xba }, { 0xed,0x16,0x82,0x5c },
652 { 0x8a,0xcf,0x1c,0x2b }, { 0xa7,0x79,0xb4,0x92 },
653 { 0xf3,0x07,0xf2,0xf0 }, { 0x4e,0x69,0xe2,0xa1 },
654 { 0x65,0xda,0xf4,0xcd }, { 0x06,0x05,0xbe,0xd5 },
655 { 0xd1,0x34,0x62,0x1f }, { 0xc4,0xa6,0xfe,0x8a },
656 { 0x34,0x2e,0x53,0x9d }, { 0xa2,0xf3,0x55,0xa0 },
657 { 0x05,0x8a,0xe1,0x32 }, { 0xa4,0xf6,0xeb,0x75 },
658 { 0x0b,0x83,0xec,0x39 }, { 0x40,0x60,0xef,0xaa },
659 { 0x5e,0x71,0x9f,0x06 }, { 0xbd,0x6e,0x10,0x51 },
660 { 0x3e,0x21,0x8a,0xf9 }, { 0x96,0xdd,0x06,0x3d },
661 { 0xdd,0x3e,0x05,0xae }, { 0x4d,0xe6,0xbd,0x46 },
662 { 0x91,0x54,0x8d,0xb5 }, { 0x71,0xc4,0x5d,0x05 },
663 { 0x04,0x06,0xd4,0x6f }, { 0x60,0x50,0x15,0xff },
664 { 0x19,0x98,0xfb,0x24 }, { 0xd6,0xbd,0xe9,0x97 },
665 { 0x89,0x40,0x43,0xcc }, { 0x67,0xd9,0x9e,0x77 },
666 { 0xb0,0xe8,0x42,0xbd }, { 0x07,0x89,0x8b,0x88 },
667 { 0xe7,0x19,0x5b,0x38 }, { 0x79,0xc8,0xee,0xdb },
668 { 0xa1,0x7c,0x0a,0x47 }, { 0x7c,0x42,0x0f,0xe9 },
669 { 0xf8,0x84,0x1e,0xc9 }, { 0x00,0x00,0x00,0x00 },
670 { 0x09,0x80,0x86,0x83 }, { 0x32,0x2b,0xed,0x48 },
671 { 0x1e,0x11,0x70,0xac }, { 0x6c,0x5a,0x72,0x4e },
672 { 0xfd,0x0e,0xff,0xfb }, { 0x0f,0x85,0x38,0x56 },
673 { 0x3d,0xae,0xd5,0x1e }, { 0x36,0x2d,0x39,0x27 },
674 { 0x0a,0x0f,0xd9,0x64 }, { 0x68,0x5c,0xa6,0x21 },
675 { 0x9b,0x5b,0x54,0xd1 }, { 0x24,0x36,0x2e,0x3a },
676 { 0x0c,0x0a,0x67,0xb1 }, { 0x93,0x57,0xe7,0x0f },
677 { 0xb4,0xee,0x96,0xd2 }, { 0x1b,0x9b,0x91,0x9e },
678 { 0x80,0xc0,0xc5,0x4f }, { 0x61,0xdc,0x20,0xa2 },
679 { 0x5a,0x77,0x4b,0x69 }, { 0x1c,0x12,0x1a,0x16 },
680 { 0xe2,0x93,0xba,0x0a }, { 0xc0,0xa0,0x2a,0xe5 },
681 { 0x3c,0x22,0xe0,0x43 }, { 0x12,0x1b,0x17,0x1d },
682 { 0x0e,0x09,0x0d,0x0b }, { 0xf2,0x8b,0xc7,0xad },
683 { 0x2d,0xb6,0xa8,0xb9 }, { 0x14,0x1e,0xa9,0xc8 },
684 { 0x57,0xf1,0x19,0x85 }, { 0xaf,0x75,0x07,0x4c },
685 { 0xee,0x99,0xdd,0xbb }, { 0xa3,0x7f,0x60,0xfd },
686 { 0xf7,0x01,0x26,0x9f }, { 0x5c,0x72,0xf5,0xbc },
687 { 0x44,0x66,0x3b,0xc5 }, { 0x5b,0xfb,0x7e,0x34 },
688 { 0x8b,0x43,0x29,0x76 }, { 0xcb,0x23,0xc6,0xdc },
689 { 0xb6,0xed,0xfc,0x68 }, { 0xb8,0xe4,0xf1,0x63 },
690 { 0xd7,0x31,0xdc,0xca }, { 0x42,0x63,0x85,0x10 },
691 { 0x13,0x97,0x22,0x40 }, { 0x84,0xc6,0x11,0x20 },
692 { 0x85,0x4a,0x24,0x7d }, { 0xd2,0xbb,0x3d,0xf8 },
693 { 0xae,0xf9,0x32,0x11 }, { 0xc7,0x29,0xa1,0x6d },
694 { 0x1d,0x9e,0x2f,0x4b }, { 0xdc,0xb2,0x30,0xf3 },
695 { 0x0d,0x86,0x52,0xec }, { 0x77,0xc1,0xe3,0xd0 },
696 { 0x2b,0xb3,0x16,0x6c }, { 0xa9,0x70,0xb9,0x99 },
697 { 0x11,0x94,0x48,0xfa }, { 0x47,0xe9,0x64,0x22 },
698 { 0xa8,0xfc,0x8c,0xc4 }, { 0xa0,0xf0,0x3f,0x1a },
699 { 0x56,0x7d,0x2c,0xd8 }, { 0x22,0x33,0x90,0xef },
700 { 0x87,0x49,0x4e,0xc7 }, { 0xd9,0x38,0xd1,0xc1 },
701 { 0x8c,0xca,0xa2,0xfe }, { 0x98,0xd4,0x0b,0x36 },
702 { 0xa6,0xf5,0x81,0xcf }, { 0xa5,0x7a,0xde,0x28 },
703 { 0xda,0xb7,0x8e,0x26 }, { 0x3f,0xad,0xbf,0xa4 },
704 { 0x2c,0x3a,0x9d,0xe4 }, { 0x50,0x78,0x92,0x0d },
705 { 0x6a,0x5f,0xcc,0x9b }, { 0x54,0x7e,0x46,0x62 },
706 { 0xf6,0x8d,0x13,0xc2 }, { 0x90,0xd8,0xb8,0xe8 },
707 { 0x2e,0x39,0xf7,0x5e }, { 0x82,0xc3,0xaf,0xf5 },
708 { 0x9f,0x5d,0x80,0xbe }, { 0x69,0xd0,0x93,0x7c },
709 { 0x6f,0xd5,0x2d,0xa9 }, { 0xcf,0x25,0x12,0xb3 },
710 { 0xc8,0xac,0x99,0x3b }, { 0x10,0x18,0x7d,0xa7 },
711 { 0xe8,0x9c,0x63,0x6e }, { 0xdb,0x3b,0xbb,0x7b },
712 { 0xcd,0x26,0x78,0x09 }, { 0x6e,0x59,0x18,0xf4 },
713 { 0xec,0x9a,0xb7,0x01 }, { 0x83,0x4f,0x9a,0xa8 },
714 { 0xe6,0x95,0x6e,0x65 }, { 0xaa,0xff,0xe6,0x7e },
715 { 0x21,0xbc,0xcf,0x08 }, { 0xef,0x15,0xe8,0xe6 },
716 { 0xba,0xe7,0x9b,0xd9 }, { 0x4a,0x6f,0x36,0xce },
717 { 0xea,0x9f,0x09,0xd4 }, { 0x29,0xb0,0x7c,0xd6 },
718 { 0x31,0xa4,0xb2,0xaf }, { 0x2a,0x3f,0x23,0x31 },
719 { 0xc6,0xa5,0x94,0x30 }, { 0x35,0xa2,0x66,0xc0 },
720 { 0x74,0x4e,0xbc,0x37 }, { 0xfc,0x82,0xca,0xa6 },
721 { 0xe0,0x90,0xd0,0xb0 }, { 0x33,0xa7,0xd8,0x15 },
722 { 0xf1,0x04,0x98,0x4a }, { 0x41,0xec,0xda,0xf7 },
723 { 0x7f,0xcd,0x50,0x0e }, { 0x17,0x91,0xf6,0x2f },
724 { 0x76,0x4d,0xd6,0x8d }, { 0x43,0xef,0xb0,0x4d },
725 { 0xcc,0xaa,0x4d,0x54 }, { 0xe4,0x96,0x04,0xdf },
726 { 0x9e,0xd1,0xb5,0xe3 }, { 0x4c,0x6a,0x88,0x1b },
727 { 0xc1,0x2c,0x1f,0xb8 }, { 0x46,0x65,0x51,0x7f },
728 { 0x9d,0x5e,0xea,0x04 }, { 0x01,0x8c,0x35,0x5d },
729 { 0xfa,0x87,0x74,0x73 }, { 0xfb,0x0b,0x41,0x2e },
730 { 0xb3,0x67,0x1d,0x5a }, { 0x92,0xdb,0xd2,0x52 },
731 { 0xe9,0x10,0x56,0x33 }, { 0x6d,0xd6,0x47,0x13 },
732 { 0x9a,0xd7,0x61,0x8c }, { 0x37,0xa1,0x0c,0x7a },
733 { 0x59,0xf8,0x14,0x8e }, { 0xeb,0x13,0x3c,0x89 },
734 { 0xce,0xa9,0x27,0xee }, { 0xb7,0x61,0xc9,0x35 },
735 { 0xe1,0x1c,0xe5,0xed }, { 0x7a,0x47,0xb1,0x3c },
736 { 0x9c,0xd2,0xdf,0x59 }, { 0x55,0xf2,0x73,0x3f },
737 { 0x18,0x14,0xce,0x79 }, { 0x73,0xc7,0x37,0xbf },
738 { 0x53,0xf7,0xcd,0xea }, { 0x5f,0xfd,0xaa,0x5b },
739 { 0xdf,0x3d,0x6f,0x14 }, { 0x78,0x44,0xdb,0x86 },
740 { 0xca,0xaf,0xf3,0x81 }, { 0xb9,0x68,0xc4,0x3e },
741 { 0x38,0x24,0x34,0x2c }, { 0xc2,0xa3,0x40,0x5f },
742 { 0x16,0x1d,0xc3,0x72 }, { 0xbc,0xe2,0x25,0x0c },
743 { 0x28,0x3c,0x49,0x8b }, { 0xff,0x0d,0x95,0x41 },
744 { 0x39,0xa8,0x01,0x71 }, { 0x08,0x0c,0xb3,0xde },
745 { 0xd8,0xb4,0xe4,0x9c }, { 0x64,0x56,0xc1,0x90 },
746 { 0x7b,0xcb,0x84,0x61 }, { 0xd5,0x32,0xb6,0x70 },
747 { 0x48,0x6c,0x5c,0x74 }, { 0xd0,0xb8,0x57,0x42 }
748};
749
750static const byte T6[256][4] = {
751 { 0x50,0x51,0xf4,0xa7 }, { 0x53,0x7e,0x41,0x65 },
752 { 0xc3,0x1a,0x17,0xa4 }, { 0x96,0x3a,0x27,0x5e },
753 { 0xcb,0x3b,0xab,0x6b }, { 0xf1,0x1f,0x9d,0x45 },
754 { 0xab,0xac,0xfa,0x58 }, { 0x93,0x4b,0xe3,0x03 },
755 { 0x55,0x20,0x30,0xfa }, { 0xf6,0xad,0x76,0x6d },
756 { 0x91,0x88,0xcc,0x76 }, { 0x25,0xf5,0x02,0x4c },
757 { 0xfc,0x4f,0xe5,0xd7 }, { 0xd7,0xc5,0x2a,0xcb },
758 { 0x80,0x26,0x35,0x44 }, { 0x8f,0xb5,0x62,0xa3 },
759 { 0x49,0xde,0xb1,0x5a }, { 0x67,0x25,0xba,0x1b },
760 { 0x98,0x45,0xea,0x0e }, { 0xe1,0x5d,0xfe,0xc0 },
761 { 0x02,0xc3,0x2f,0x75 }, { 0x12,0x81,0x4c,0xf0 },
762 { 0xa3,0x8d,0x46,0x97 }, { 0xc6,0x6b,0xd3,0xf9 },
763 { 0xe7,0x03,0x8f,0x5f }, { 0x95,0x15,0x92,0x9c },
764 { 0xeb,0xbf,0x6d,0x7a }, { 0xda,0x95,0x52,0x59 },
765 { 0x2d,0xd4,0xbe,0x83 }, { 0xd3,0x58,0x74,0x21 },
766 { 0x29,0x49,0xe0,0x69 }, { 0x44,0x8e,0xc9,0xc8 },
767 { 0x6a,0x75,0xc2,0x89 }, { 0x78,0xf4,0x8e,0x79 },
768 { 0x6b,0x99,0x58,0x3e }, { 0xdd,0x27,0xb9,0x71 },
769 { 0xb6,0xbe,0xe1,0x4f }, { 0x17,0xf0,0x88,0xad },
770 { 0x66,0xc9,0x20,0xac }, { 0xb4,0x7d,0xce,0x3a },
771 { 0x18,0x63,0xdf,0x4a }, { 0x82,0xe5,0x1a,0x31 },
772 { 0x60,0x97,0x51,0x33 }, { 0x45,0x62,0x53,0x7f },
773 { 0xe0,0xb1,0x64,0x77 }, { 0x84,0xbb,0x6b,0xae },
774 { 0x1c,0xfe,0x81,0xa0 }, { 0x94,0xf9,0x08,0x2b },
775 { 0x58,0x70,0x48,0x68 }, { 0x19,0x8f,0x45,0xfd },
776 { 0x87,0x94,0xde,0x6c }, { 0xb7,0x52,0x7b,0xf8 },
777 { 0x23,0xab,0x73,0xd3 }, { 0xe2,0x72,0x4b,0x02 },
778 { 0x57,0xe3,0x1f,0x8f }, { 0x2a,0x66,0x55,0xab },
779 { 0x07,0xb2,0xeb,0x28 }, { 0x03,0x2f,0xb5,0xc2 },
780 { 0x9a,0x86,0xc5,0x7b }, { 0xa5,0xd3,0x37,0x08 },
781 { 0xf2,0x30,0x28,0x87 }, { 0xb2,0x23,0xbf,0xa5 },
782 { 0xba,0x02,0x03,0x6a }, { 0x5c,0xed,0x16,0x82 },
783 { 0x2b,0x8a,0xcf,0x1c }, { 0x92,0xa7,0x79,0xb4 },
784 { 0xf0,0xf3,0x07,0xf2 }, { 0xa1,0x4e,0x69,0xe2 },
785 { 0xcd,0x65,0xda,0xf4 }, { 0xd5,0x06,0x05,0xbe },
786 { 0x1f,0xd1,0x34,0x62 }, { 0x8a,0xc4,0xa6,0xfe },
787 { 0x9d,0x34,0x2e,0x53 }, { 0xa0,0xa2,0xf3,0x55 },
788 { 0x32,0x05,0x8a,0xe1 }, { 0x75,0xa4,0xf6,0xeb },
789 { 0x39,0x0b,0x83,0xec }, { 0xaa,0x40,0x60,0xef },
790 { 0x06,0x5e,0x71,0x9f }, { 0x51,0xbd,0x6e,0x10 },
791 { 0xf9,0x3e,0x21,0x8a }, { 0x3d,0x96,0xdd,0x06 },
792 { 0xae,0xdd,0x3e,0x05 }, { 0x46,0x4d,0xe6,0xbd },
793 { 0xb5,0x91,0x54,0x8d }, { 0x05,0x71,0xc4,0x5d },
794 { 0x6f,0x04,0x06,0xd4 }, { 0xff,0x60,0x50,0x15 },
795 { 0x24,0x19,0x98,0xfb }, { 0x97,0xd6,0xbd,0xe9 },
796 { 0xcc,0x89,0x40,0x43 }, { 0x77,0x67,0xd9,0x9e },
797 { 0xbd,0xb0,0xe8,0x42 }, { 0x88,0x07,0x89,0x8b },
798 { 0x38,0xe7,0x19,0x5b }, { 0xdb,0x79,0xc8,0xee },
799 { 0x47,0xa1,0x7c,0x0a }, { 0xe9,0x7c,0x42,0x0f },
800 { 0xc9,0xf8,0x84,0x1e }, { 0x00,0x00,0x00,0x00 },
801 { 0x83,0x09,0x80,0x86 }, { 0x48,0x32,0x2b,0xed },
802 { 0xac,0x1e,0x11,0x70 }, { 0x4e,0x6c,0x5a,0x72 },
803 { 0xfb,0xfd,0x0e,0xff }, { 0x56,0x0f,0x85,0x38 },
804 { 0x1e,0x3d,0xae,0xd5 }, { 0x27,0x36,0x2d,0x39 },
805 { 0x64,0x0a,0x0f,0xd9 }, { 0x21,0x68,0x5c,0xa6 },
806 { 0xd1,0x9b,0x5b,0x54 }, { 0x3a,0x24,0x36,0x2e },
807 { 0xb1,0x0c,0x0a,0x67 }, { 0x0f,0x93,0x57,0xe7 },
808 { 0xd2,0xb4,0xee,0x96 }, { 0x9e,0x1b,0x9b,0x91 },
809 { 0x4f,0x80,0xc0,0xc5 }, { 0xa2,0x61,0xdc,0x20 },
810 { 0x69,0x5a,0x77,0x4b }, { 0x16,0x1c,0x12,0x1a },
811 { 0x0a,0xe2,0x93,0xba }, { 0xe5,0xc0,0xa0,0x2a },
812 { 0x43,0x3c,0x22,0xe0 }, { 0x1d,0x12,0x1b,0x17 },
813 { 0x0b,0x0e,0x09,0x0d }, { 0xad,0xf2,0x8b,0xc7 },
814 { 0xb9,0x2d,0xb6,0xa8 }, { 0xc8,0x14,0x1e,0xa9 },
815 { 0x85,0x57,0xf1,0x19 }, { 0x4c,0xaf,0x75,0x07 },
816 { 0xbb,0xee,0x99,0xdd }, { 0xfd,0xa3,0x7f,0x60 },
817 { 0x9f,0xf7,0x01,0x26 }, { 0xbc,0x5c,0x72,0xf5 },
818 { 0xc5,0x44,0x66,0x3b }, { 0x34,0x5b,0xfb,0x7e },
819 { 0x76,0x8b,0x43,0x29 }, { 0xdc,0xcb,0x23,0xc6 },
820 { 0x68,0xb6,0xed,0xfc }, { 0x63,0xb8,0xe4,0xf1 },
821 { 0xca,0xd7,0x31,0xdc }, { 0x10,0x42,0x63,0x85 },
822 { 0x40,0x13,0x97,0x22 }, { 0x20,0x84,0xc6,0x11 },
823 { 0x7d,0x85,0x4a,0x24 }, { 0xf8,0xd2,0xbb,0x3d },
824 { 0x11,0xae,0xf9,0x32 }, { 0x6d,0xc7,0x29,0xa1 },
825 { 0x4b,0x1d,0x9e,0x2f }, { 0xf3,0xdc,0xb2,0x30 },
826 { 0xec,0x0d,0x86,0x52 }, { 0xd0,0x77,0xc1,0xe3 },
827 { 0x6c,0x2b,0xb3,0x16 }, { 0x99,0xa9,0x70,0xb9 },
828 { 0xfa,0x11,0x94,0x48 }, { 0x22,0x47,0xe9,0x64 },
829 { 0xc4,0xa8,0xfc,0x8c }, { 0x1a,0xa0,0xf0,0x3f },
830 { 0xd8,0x56,0x7d,0x2c }, { 0xef,0x22,0x33,0x90 },
831 { 0xc7,0x87,0x49,0x4e }, { 0xc1,0xd9,0x38,0xd1 },
832 { 0xfe,0x8c,0xca,0xa2 }, { 0x36,0x98,0xd4,0x0b },
833 { 0xcf,0xa6,0xf5,0x81 }, { 0x28,0xa5,0x7a,0xde },
834 { 0x26,0xda,0xb7,0x8e }, { 0xa4,0x3f,0xad,0xbf },
835 { 0xe4,0x2c,0x3a,0x9d }, { 0x0d,0x50,0x78,0x92 },
836 { 0x9b,0x6a,0x5f,0xcc }, { 0x62,0x54,0x7e,0x46 },
837 { 0xc2,0xf6,0x8d,0x13 }, { 0xe8,0x90,0xd8,0xb8 },
838 { 0x5e,0x2e,0x39,0xf7 }, { 0xf5,0x82,0xc3,0xaf },
839 { 0xbe,0x9f,0x5d,0x80 }, { 0x7c,0x69,0xd0,0x93 },
840 { 0xa9,0x6f,0xd5,0x2d }, { 0xb3,0xcf,0x25,0x12 },
841 { 0x3b,0xc8,0xac,0x99 }, { 0xa7,0x10,0x18,0x7d },
842 { 0x6e,0xe8,0x9c,0x63 }, { 0x7b,0xdb,0x3b,0xbb },
843 { 0x09,0xcd,0x26,0x78 }, { 0xf4,0x6e,0x59,0x18 },
844 { 0x01,0xec,0x9a,0xb7 }, { 0xa8,0x83,0x4f,0x9a },
845 { 0x65,0xe6,0x95,0x6e }, { 0x7e,0xaa,0xff,0xe6 },
846 { 0x08,0x21,0xbc,0xcf }, { 0xe6,0xef,0x15,0xe8 },
847 { 0xd9,0xba,0xe7,0x9b }, { 0xce,0x4a,0x6f,0x36 },
848 { 0xd4,0xea,0x9f,0x09 }, { 0xd6,0x29,0xb0,0x7c },
849 { 0xaf,0x31,0xa4,0xb2 }, { 0x31,0x2a,0x3f,0x23 },
850 { 0x30,0xc6,0xa5,0x94 }, { 0xc0,0x35,0xa2,0x66 },
851 { 0x37,0x74,0x4e,0xbc }, { 0xa6,0xfc,0x82,0xca },
852 { 0xb0,0xe0,0x90,0xd0 }, { 0x15,0x33,0xa7,0xd8 },
853 { 0x4a,0xf1,0x04,0x98 }, { 0xf7,0x41,0xec,0xda },
854 { 0x0e,0x7f,0xcd,0x50 }, { 0x2f,0x17,0x91,0xf6 },
855 { 0x8d,0x76,0x4d,0xd6 }, { 0x4d,0x43,0xef,0xb0 },
856 { 0x54,0xcc,0xaa,0x4d }, { 0xdf,0xe4,0x96,0x04 },
857 { 0xe3,0x9e,0xd1,0xb5 }, { 0x1b,0x4c,0x6a,0x88 },
858 { 0xb8,0xc1,0x2c,0x1f }, { 0x7f,0x46,0x65,0x51 },
859 { 0x04,0x9d,0x5e,0xea }, { 0x5d,0x01,0x8c,0x35 },
860 { 0x73,0xfa,0x87,0x74 }, { 0x2e,0xfb,0x0b,0x41 },
861 { 0x5a,0xb3,0x67,0x1d }, { 0x52,0x92,0xdb,0xd2 },
862 { 0x33,0xe9,0x10,0x56 }, { 0x13,0x6d,0xd6,0x47 },
863 { 0x8c,0x9a,0xd7,0x61 }, { 0x7a,0x37,0xa1,0x0c },
864 { 0x8e,0x59,0xf8,0x14 }, { 0x89,0xeb,0x13,0x3c },
865 { 0xee,0xce,0xa9,0x27 }, { 0x35,0xb7,0x61,0xc9 },
866 { 0xed,0xe1,0x1c,0xe5 }, { 0x3c,0x7a,0x47,0xb1 },
867 { 0x59,0x9c,0xd2,0xdf }, { 0x3f,0x55,0xf2,0x73 },
868 { 0x79,0x18,0x14,0xce }, { 0xbf,0x73,0xc7,0x37 },
869 { 0xea,0x53,0xf7,0xcd }, { 0x5b,0x5f,0xfd,0xaa },
870 { 0x14,0xdf,0x3d,0x6f }, { 0x86,0x78,0x44,0xdb },
871 { 0x81,0xca,0xaf,0xf3 }, { 0x3e,0xb9,0x68,0xc4 },
872 { 0x2c,0x38,0x24,0x34 }, { 0x5f,0xc2,0xa3,0x40 },
873 { 0x72,0x16,0x1d,0xc3 }, { 0x0c,0xbc,0xe2,0x25 },
874 { 0x8b,0x28,0x3c,0x49 }, { 0x41,0xff,0x0d,0x95 },
875 { 0x71,0x39,0xa8,0x01 }, { 0xde,0x08,0x0c,0xb3 },
876 { 0x9c,0xd8,0xb4,0xe4 }, { 0x90,0x64,0x56,0xc1 },
877 { 0x61,0x7b,0xcb,0x84 }, { 0x70,0xd5,0x32,0xb6 },
878 { 0x74,0x48,0x6c,0x5c }, { 0x42,0xd0,0xb8,0x57 }
879};
880
881static const byte T7[256][4] = {
882 { 0xa7,0x50,0x51,0xf4 }, { 0x65,0x53,0x7e,0x41 },
883 { 0xa4,0xc3,0x1a,0x17 }, { 0x5e,0x96,0x3a,0x27 },
884 { 0x6b,0xcb,0x3b,0xab }, { 0x45,0xf1,0x1f,0x9d },
885 { 0x58,0xab,0xac,0xfa }, { 0x03,0x93,0x4b,0xe3 },
886 { 0xfa,0x55,0x20,0x30 }, { 0x6d,0xf6,0xad,0x76 },
887 { 0x76,0x91,0x88,0xcc }, { 0x4c,0x25,0xf5,0x02 },
888 { 0xd7,0xfc,0x4f,0xe5 }, { 0xcb,0xd7,0xc5,0x2a },
889 { 0x44,0x80,0x26,0x35 }, { 0xa3,0x8f,0xb5,0x62 },
890 { 0x5a,0x49,0xde,0xb1 }, { 0x1b,0x67,0x25,0xba },
891 { 0x0e,0x98,0x45,0xea }, { 0xc0,0xe1,0x5d,0xfe },
892 { 0x75,0x02,0xc3,0x2f }, { 0xf0,0x12,0x81,0x4c },
893 { 0x97,0xa3,0x8d,0x46 }, { 0xf9,0xc6,0x6b,0xd3 },
894 { 0x5f,0xe7,0x03,0x8f }, { 0x9c,0x95,0x15,0x92 },
895 { 0x7a,0xeb,0xbf,0x6d }, { 0x59,0xda,0x95,0x52 },
896 { 0x83,0x2d,0xd4,0xbe }, { 0x21,0xd3,0x58,0x74 },
897 { 0x69,0x29,0x49,0xe0 }, { 0xc8,0x44,0x8e,0xc9 },
898 { 0x89,0x6a,0x75,0xc2 }, { 0x79,0x78,0xf4,0x8e },
899 { 0x3e,0x6b,0x99,0x58 }, { 0x71,0xdd,0x27,0xb9 },
900 { 0x4f,0xb6,0xbe,0xe1 }, { 0xad,0x17,0xf0,0x88 },
901 { 0xac,0x66,0xc9,0x20 }, { 0x3a,0xb4,0x7d,0xce },
902 { 0x4a,0x18,0x63,0xdf }, { 0x31,0x82,0xe5,0x1a },
903 { 0x33,0x60,0x97,0x51 }, { 0x7f,0x45,0x62,0x53 },
904 { 0x77,0xe0,0xb1,0x64 }, { 0xae,0x84,0xbb,0x6b },
905 { 0xa0,0x1c,0xfe,0x81 }, { 0x2b,0x94,0xf9,0x08 },
906 { 0x68,0x58,0x70,0x48 }, { 0xfd,0x19,0x8f,0x45 },
907 { 0x6c,0x87,0x94,0xde }, { 0xf8,0xb7,0x52,0x7b },
908 { 0xd3,0x23,0xab,0x73 }, { 0x02,0xe2,0x72,0x4b },
909 { 0x8f,0x57,0xe3,0x1f }, { 0xab,0x2a,0x66,0x55 },
910 { 0x28,0x07,0xb2,0xeb }, { 0xc2,0x03,0x2f,0xb5 },
911 { 0x7b,0x9a,0x86,0xc5 }, { 0x08,0xa5,0xd3,0x37 },
912 { 0x87,0xf2,0x30,0x28 }, { 0xa5,0xb2,0x23,0xbf },
913 { 0x6a,0xba,0x02,0x03 }, { 0x82,0x5c,0xed,0x16 },
914 { 0x1c,0x2b,0x8a,0xcf }, { 0xb4,0x92,0xa7,0x79 },
915 { 0xf2,0xf0,0xf3,0x07 }, { 0xe2,0xa1,0x4e,0x69 },
916 { 0xf4,0xcd,0x65,0xda }, { 0xbe,0xd5,0x06,0x05 },
917 { 0x62,0x1f,0xd1,0x34 }, { 0xfe,0x8a,0xc4,0xa6 },
918 { 0x53,0x9d,0x34,0x2e }, { 0x55,0xa0,0xa2,0xf3 },
919 { 0xe1,0x32,0x05,0x8a }, { 0xeb,0x75,0xa4,0xf6 },
920 { 0xec,0x39,0x0b,0x83 }, { 0xef,0xaa,0x40,0x60 },
921 { 0x9f,0x06,0x5e,0x71 }, { 0x10,0x51,0xbd,0x6e },
922 { 0x8a,0xf9,0x3e,0x21 }, { 0x06,0x3d,0x96,0xdd },
923 { 0x05,0xae,0xdd,0x3e }, { 0xbd,0x46,0x4d,0xe6 },
924 { 0x8d,0xb5,0x91,0x54 }, { 0x5d,0x05,0x71,0xc4 },
925 { 0xd4,0x6f,0x04,0x06 }, { 0x15,0xff,0x60,0x50 },
926 { 0xfb,0x24,0x19,0x98 }, { 0xe9,0x97,0xd6,0xbd },
927 { 0x43,0xcc,0x89,0x40 }, { 0x9e,0x77,0x67,0xd9 },
928 { 0x42,0xbd,0xb0,0xe8 }, { 0x8b,0x88,0x07,0x89 },
929 { 0x5b,0x38,0xe7,0x19 }, { 0xee,0xdb,0x79,0xc8 },
930 { 0x0a,0x47,0xa1,0x7c }, { 0x0f,0xe9,0x7c,0x42 },
931 { 0x1e,0xc9,0xf8,0x84 }, { 0x00,0x00,0x00,0x00 },
932 { 0x86,0x83,0x09,0x80 }, { 0xed,0x48,0x32,0x2b },
933 { 0x70,0xac,0x1e,0x11 }, { 0x72,0x4e,0x6c,0x5a },
934 { 0xff,0xfb,0xfd,0x0e }, { 0x38,0x56,0x0f,0x85 },
935 { 0xd5,0x1e,0x3d,0xae }, { 0x39,0x27,0x36,0x2d },
936 { 0xd9,0x64,0x0a,0x0f }, { 0xa6,0x21,0x68,0x5c },
937 { 0x54,0xd1,0x9b,0x5b }, { 0x2e,0x3a,0x24,0x36 },
938 { 0x67,0xb1,0x0c,0x0a }, { 0xe7,0x0f,0x93,0x57 },
939 { 0x96,0xd2,0xb4,0xee }, { 0x91,0x9e,0x1b,0x9b },
940 { 0xc5,0x4f,0x80,0xc0 }, { 0x20,0xa2,0x61,0xdc },
941 { 0x4b,0x69,0x5a,0x77 }, { 0x1a,0x16,0x1c,0x12 },
942 { 0xba,0x0a,0xe2,0x93 }, { 0x2a,0xe5,0xc0,0xa0 },
943 { 0xe0,0x43,0x3c,0x22 }, { 0x17,0x1d,0x12,0x1b },
944 { 0x0d,0x0b,0x0e,0x09 }, { 0xc7,0xad,0xf2,0x8b },
945 { 0xa8,0xb9,0x2d,0xb6 }, { 0xa9,0xc8,0x14,0x1e },
946 { 0x19,0x85,0x57,0xf1 }, { 0x07,0x4c,0xaf,0x75 },
947 { 0xdd,0xbb,0xee,0x99 }, { 0x60,0xfd,0xa3,0x7f },
948 { 0x26,0x9f,0xf7,0x01 }, { 0xf5,0xbc,0x5c,0x72 },
949 { 0x3b,0xc5,0x44,0x66 }, { 0x7e,0x34,0x5b,0xfb },
950 { 0x29,0x76,0x8b,0x43 }, { 0xc6,0xdc,0xcb,0x23 },
951 { 0xfc,0x68,0xb6,0xed }, { 0xf1,0x63,0xb8,0xe4 },
952 { 0xdc,0xca,0xd7,0x31 }, { 0x85,0x10,0x42,0x63 },
953 { 0x22,0x40,0x13,0x97 }, { 0x11,0x20,0x84,0xc6 },
954 { 0x24,0x7d,0x85,0x4a }, { 0x3d,0xf8,0xd2,0xbb },
955 { 0x32,0x11,0xae,0xf9 }, { 0xa1,0x6d,0xc7,0x29 },
956 { 0x2f,0x4b,0x1d,0x9e }, { 0x30,0xf3,0xdc,0xb2 },
957 { 0x52,0xec,0x0d,0x86 }, { 0xe3,0xd0,0x77,0xc1 },
958 { 0x16,0x6c,0x2b,0xb3 }, { 0xb9,0x99,0xa9,0x70 },
959 { 0x48,0xfa,0x11,0x94 }, { 0x64,0x22,0x47,0xe9 },
960 { 0x8c,0xc4,0xa8,0xfc }, { 0x3f,0x1a,0xa0,0xf0 },
961 { 0x2c,0xd8,0x56,0x7d }, { 0x90,0xef,0x22,0x33 },
962 { 0x4e,0xc7,0x87,0x49 }, { 0xd1,0xc1,0xd9,0x38 },
963 { 0xa2,0xfe,0x8c,0xca }, { 0x0b,0x36,0x98,0xd4 },
964 { 0x81,0xcf,0xa6,0xf5 }, { 0xde,0x28,0xa5,0x7a },
965 { 0x8e,0x26,0xda,0xb7 }, { 0xbf,0xa4,0x3f,0xad },
966 { 0x9d,0xe4,0x2c,0x3a }, { 0x92,0x0d,0x50,0x78 },
967 { 0xcc,0x9b,0x6a,0x5f }, { 0x46,0x62,0x54,0x7e },
968 { 0x13,0xc2,0xf6,0x8d }, { 0xb8,0xe8,0x90,0xd8 },
969 { 0xf7,0x5e,0x2e,0x39 }, { 0xaf,0xf5,0x82,0xc3 },
970 { 0x80,0xbe,0x9f,0x5d }, { 0x93,0x7c,0x69,0xd0 },
971 { 0x2d,0xa9,0x6f,0xd5 }, { 0x12,0xb3,0xcf,0x25 },
972 { 0x99,0x3b,0xc8,0xac }, { 0x7d,0xa7,0x10,0x18 },
973 { 0x63,0x6e,0xe8,0x9c }, { 0xbb,0x7b,0xdb,0x3b },
974 { 0x78,0x09,0xcd,0x26 }, { 0x18,0xf4,0x6e,0x59 },
975 { 0xb7,0x01,0xec,0x9a }, { 0x9a,0xa8,0x83,0x4f },
976 { 0x6e,0x65,0xe6,0x95 }, { 0xe6,0x7e,0xaa,0xff },
977 { 0xcf,0x08,0x21,0xbc }, { 0xe8,0xe6,0xef,0x15 },
978 { 0x9b,0xd9,0xba,0xe7 }, { 0x36,0xce,0x4a,0x6f },
979 { 0x09,0xd4,0xea,0x9f }, { 0x7c,0xd6,0x29,0xb0 },
980 { 0xb2,0xaf,0x31,0xa4 }, { 0x23,0x31,0x2a,0x3f },
981 { 0x94,0x30,0xc6,0xa5 }, { 0x66,0xc0,0x35,0xa2 },
982 { 0xbc,0x37,0x74,0x4e }, { 0xca,0xa6,0xfc,0x82 },
983 { 0xd0,0xb0,0xe0,0x90 }, { 0xd8,0x15,0x33,0xa7 },
984 { 0x98,0x4a,0xf1,0x04 }, { 0xda,0xf7,0x41,0xec },
985 { 0x50,0x0e,0x7f,0xcd }, { 0xf6,0x2f,0x17,0x91 },
986 { 0xd6,0x8d,0x76,0x4d }, { 0xb0,0x4d,0x43,0xef },
987 { 0x4d,0x54,0xcc,0xaa }, { 0x04,0xdf,0xe4,0x96 },
988 { 0xb5,0xe3,0x9e,0xd1 }, { 0x88,0x1b,0x4c,0x6a },
989 { 0x1f,0xb8,0xc1,0x2c }, { 0x51,0x7f,0x46,0x65 },
990 { 0xea,0x04,0x9d,0x5e }, { 0x35,0x5d,0x01,0x8c },
991 { 0x74,0x73,0xfa,0x87 }, { 0x41,0x2e,0xfb,0x0b },
992 { 0x1d,0x5a,0xb3,0x67 }, { 0xd2,0x52,0x92,0xdb },
993 { 0x56,0x33,0xe9,0x10 }, { 0x47,0x13,0x6d,0xd6 },
994 { 0x61,0x8c,0x9a,0xd7 }, { 0x0c,0x7a,0x37,0xa1 },
995 { 0x14,0x8e,0x59,0xf8 }, { 0x3c,0x89,0xeb,0x13 },
996 { 0x27,0xee,0xce,0xa9 }, { 0xc9,0x35,0xb7,0x61 },
997 { 0xe5,0xed,0xe1,0x1c }, { 0xb1,0x3c,0x7a,0x47 },
998 { 0xdf,0x59,0x9c,0xd2 }, { 0x73,0x3f,0x55,0xf2 },
999 { 0xce,0x79,0x18,0x14 }, { 0x37,0xbf,0x73,0xc7 },
1000 { 0xcd,0xea,0x53,0xf7 }, { 0xaa,0x5b,0x5f,0xfd },
1001 { 0x6f,0x14,0xdf,0x3d }, { 0xdb,0x86,0x78,0x44 },
1002 { 0xf3,0x81,0xca,0xaf }, { 0xc4,0x3e,0xb9,0x68 },
1003 { 0x34,0x2c,0x38,0x24 }, { 0x40,0x5f,0xc2,0xa3 },
1004 { 0xc3,0x72,0x16,0x1d }, { 0x25,0x0c,0xbc,0xe2 },
1005 { 0x49,0x8b,0x28,0x3c }, { 0x95,0x41,0xff,0x0d },
1006 { 0x01,0x71,0x39,0xa8 }, { 0xb3,0xde,0x08,0x0c },
1007 { 0xe4,0x9c,0xd8,0xb4 }, { 0xc1,0x90,0x64,0x56 },
1008 { 0x84,0x61,0x7b,0xcb }, { 0xb6,0x70,0xd5,0x32 },
1009 { 0x5c,0x74,0x48,0x6c }, { 0x57,0x42,0xd0,0xb8 }
1010};
1011
1012static const byte T8[256][4] = {
1013 { 0xf4,0xa7,0x50,0x51 }, { 0x41,0x65,0x53,0x7e },
1014 { 0x17,0xa4,0xc3,0x1a }, { 0x27,0x5e,0x96,0x3a },
1015 { 0xab,0x6b,0xcb,0x3b }, { 0x9d,0x45,0xf1,0x1f },
1016 { 0xfa,0x58,0xab,0xac }, { 0xe3,0x03,0x93,0x4b },
1017 { 0x30,0xfa,0x55,0x20 }, { 0x76,0x6d,0xf6,0xad },
1018 { 0xcc,0x76,0x91,0x88 }, { 0x02,0x4c,0x25,0xf5 },
1019 { 0xe5,0xd7,0xfc,0x4f }, { 0x2a,0xcb,0xd7,0xc5 },
1020 { 0x35,0x44,0x80,0x26 }, { 0x62,0xa3,0x8f,0xb5 },
1021 { 0xb1,0x5a,0x49,0xde }, { 0xba,0x1b,0x67,0x25 },
1022 { 0xea,0x0e,0x98,0x45 }, { 0xfe,0xc0,0xe1,0x5d },
1023 { 0x2f,0x75,0x02,0xc3 }, { 0x4c,0xf0,0x12,0x81 },
1024 { 0x46,0x97,0xa3,0x8d }, { 0xd3,0xf9,0xc6,0x6b },
1025 { 0x8f,0x5f,0xe7,0x03 }, { 0x92,0x9c,0x95,0x15 },
1026 { 0x6d,0x7a,0xeb,0xbf }, { 0x52,0x59,0xda,0x95 },
1027 { 0xbe,0x83,0x2d,0xd4 }, { 0x74,0x21,0xd3,0x58 },
1028 { 0xe0,0x69,0x29,0x49 }, { 0xc9,0xc8,0x44,0x8e },
1029 { 0xc2,0x89,0x6a,0x75 }, { 0x8e,0x79,0x78,0xf4 },
1030 { 0x58,0x3e,0x6b,0x99 }, { 0xb9,0x71,0xdd,0x27 },
1031 { 0xe1,0x4f,0xb6,0xbe }, { 0x88,0xad,0x17,0xf0 },
1032 { 0x20,0xac,0x66,0xc9 }, { 0xce,0x3a,0xb4,0x7d },
1033 { 0xdf,0x4a,0x18,0x63 }, { 0x1a,0x31,0x82,0xe5 },
1034 { 0x51,0x33,0x60,0x97 }, { 0x53,0x7f,0x45,0x62 },
1035 { 0x64,0x77,0xe0,0xb1 }, { 0x6b,0xae,0x84,0xbb },
1036 { 0x81,0xa0,0x1c,0xfe }, { 0x08,0x2b,0x94,0xf9 },
1037 { 0x48,0x68,0x58,0x70 }, { 0x45,0xfd,0x19,0x8f },
1038 { 0xde,0x6c,0x87,0x94 }, { 0x7b,0xf8,0xb7,0x52 },
1039 { 0x73,0xd3,0x23,0xab }, { 0x4b,0x02,0xe2,0x72 },
1040 { 0x1f,0x8f,0x57,0xe3 }, { 0x55,0xab,0x2a,0x66 },
1041 { 0xeb,0x28,0x07,0xb2 }, { 0xb5,0xc2,0x03,0x2f },
1042 { 0xc5,0x7b,0x9a,0x86 }, { 0x37,0x08,0xa5,0xd3 },
1043 { 0x28,0x87,0xf2,0x30 }, { 0xbf,0xa5,0xb2,0x23 },
1044 { 0x03,0x6a,0xba,0x02 }, { 0x16,0x82,0x5c,0xed },
1045 { 0xcf,0x1c,0x2b,0x8a }, { 0x79,0xb4,0x92,0xa7 },
1046 { 0x07,0xf2,0xf0,0xf3 }, { 0x69,0xe2,0xa1,0x4e },
1047 { 0xda,0xf4,0xcd,0x65 }, { 0x05,0xbe,0xd5,0x06 },
1048 { 0x34,0x62,0x1f,0xd1 }, { 0xa6,0xfe,0x8a,0xc4 },
1049 { 0x2e,0x53,0x9d,0x34 }, { 0xf3,0x55,0xa0,0xa2 },
1050 { 0x8a,0xe1,0x32,0x05 }, { 0xf6,0xeb,0x75,0xa4 },
1051 { 0x83,0xec,0x39,0x0b }, { 0x60,0xef,0xaa,0x40 },
1052 { 0x71,0x9f,0x06,0x5e }, { 0x6e,0x10,0x51,0xbd },
1053 { 0x21,0x8a,0xf9,0x3e }, { 0xdd,0x06,0x3d,0x96 },
1054 { 0x3e,0x05,0xae,0xdd }, { 0xe6,0xbd,0x46,0x4d },
1055 { 0x54,0x8d,0xb5,0x91 }, { 0xc4,0x5d,0x05,0x71 },
1056 { 0x06,0xd4,0x6f,0x04 }, { 0x50,0x15,0xff,0x60 },
1057 { 0x98,0xfb,0x24,0x19 }, { 0xbd,0xe9,0x97,0xd6 },
1058 { 0x40,0x43,0xcc,0x89 }, { 0xd9,0x9e,0x77,0x67 },
1059 { 0xe8,0x42,0xbd,0xb0 }, { 0x89,0x8b,0x88,0x07 },
1060 { 0x19,0x5b,0x38,0xe7 }, { 0xc8,0xee,0xdb,0x79 },
1061 { 0x7c,0x0a,0x47,0xa1 }, { 0x42,0x0f,0xe9,0x7c },
1062 { 0x84,0x1e,0xc9,0xf8 }, { 0x00,0x00,0x00,0x00 },
1063 { 0x80,0x86,0x83,0x09 }, { 0x2b,0xed,0x48,0x32 },
1064 { 0x11,0x70,0xac,0x1e }, { 0x5a,0x72,0x4e,0x6c },
1065 { 0x0e,0xff,0xfb,0xfd }, { 0x85,0x38,0x56,0x0f },
1066 { 0xae,0xd5,0x1e,0x3d }, { 0x2d,0x39,0x27,0x36 },
1067 { 0x0f,0xd9,0x64,0x0a }, { 0x5c,0xa6,0x21,0x68 },
1068 { 0x5b,0x54,0xd1,0x9b }, { 0x36,0x2e,0x3a,0x24 },
1069 { 0x0a,0x67,0xb1,0x0c }, { 0x57,0xe7,0x0f,0x93 },
1070 { 0xee,0x96,0xd2,0xb4 }, { 0x9b,0x91,0x9e,0x1b },
1071 { 0xc0,0xc5,0x4f,0x80 }, { 0xdc,0x20,0xa2,0x61 },
1072 { 0x77,0x4b,0x69,0x5a }, { 0x12,0x1a,0x16,0x1c },
1073 { 0x93,0xba,0x0a,0xe2 }, { 0xa0,0x2a,0xe5,0xc0 },
1074 { 0x22,0xe0,0x43,0x3c }, { 0x1b,0x17,0x1d,0x12 },
1075 { 0x09,0x0d,0x0b,0x0e }, { 0x8b,0xc7,0xad,0xf2 },
1076 { 0xb6,0xa8,0xb9,0x2d }, { 0x1e,0xa9,0xc8,0x14 },
1077 { 0xf1,0x19,0x85,0x57 }, { 0x75,0x07,0x4c,0xaf },
1078 { 0x99,0xdd,0xbb,0xee }, { 0x7f,0x60,0xfd,0xa3 },
1079 { 0x01,0x26,0x9f,0xf7 }, { 0x72,0xf5,0xbc,0x5c },
1080 { 0x66,0x3b,0xc5,0x44 }, { 0xfb,0x7e,0x34,0x5b },
1081 { 0x43,0x29,0x76,0x8b }, { 0x23,0xc6,0xdc,0xcb },
1082 { 0xed,0xfc,0x68,0xb6 }, { 0xe4,0xf1,0x63,0xb8 },
1083 { 0x31,0xdc,0xca,0xd7 }, { 0x63,0x85,0x10,0x42 },
1084 { 0x97,0x22,0x40,0x13 }, { 0xc6,0x11,0x20,0x84 },
1085 { 0x4a,0x24,0x7d,0x85 }, { 0xbb,0x3d,0xf8,0xd2 },
1086 { 0xf9,0x32,0x11,0xae }, { 0x29,0xa1,0x6d,0xc7 },
1087 { 0x9e,0x2f,0x4b,0x1d }, { 0xb2,0x30,0xf3,0xdc },
1088 { 0x86,0x52,0xec,0x0d }, { 0xc1,0xe3,0xd0,0x77 },
1089 { 0xb3,0x16,0x6c,0x2b }, { 0x70,0xb9,0x99,0xa9 },
1090 { 0x94,0x48,0xfa,0x11 }, { 0xe9,0x64,0x22,0x47 },
1091 { 0xfc,0x8c,0xc4,0xa8 }, { 0xf0,0x3f,0x1a,0xa0 },
1092 { 0x7d,0x2c,0xd8,0x56 }, { 0x33,0x90,0xef,0x22 },
1093 { 0x49,0x4e,0xc7,0x87 }, { 0x38,0xd1,0xc1,0xd9 },
1094 { 0xca,0xa2,0xfe,0x8c }, { 0xd4,0x0b,0x36,0x98 },
1095 { 0xf5,0x81,0xcf,0xa6 }, { 0x7a,0xde,0x28,0xa5 },
1096 { 0xb7,0x8e,0x26,0xda }, { 0xad,0xbf,0xa4,0x3f },
1097 { 0x3a,0x9d,0xe4,0x2c }, { 0x78,0x92,0x0d,0x50 },
1098 { 0x5f,0xcc,0x9b,0x6a }, { 0x7e,0x46,0x62,0x54 },
1099 { 0x8d,0x13,0xc2,0xf6 }, { 0xd8,0xb8,0xe8,0x90 },
1100 { 0x39,0xf7,0x5e,0x2e }, { 0xc3,0xaf,0xf5,0x82 },
1101 { 0x5d,0x80,0xbe,0x9f }, { 0xd0,0x93,0x7c,0x69 },
1102 { 0xd5,0x2d,0xa9,0x6f }, { 0x25,0x12,0xb3,0xcf },
1103 { 0xac,0x99,0x3b,0xc8 }, { 0x18,0x7d,0xa7,0x10 },
1104 { 0x9c,0x63,0x6e,0xe8 }, { 0x3b,0xbb,0x7b,0xdb },
1105 { 0x26,0x78,0x09,0xcd }, { 0x59,0x18,0xf4,0x6e },
1106 { 0x9a,0xb7,0x01,0xec }, { 0x4f,0x9a,0xa8,0x83 },
1107 { 0x95,0x6e,0x65,0xe6 }, { 0xff,0xe6,0x7e,0xaa },
1108 { 0xbc,0xcf,0x08,0x21 }, { 0x15,0xe8,0xe6,0xef },
1109 { 0xe7,0x9b,0xd9,0xba }, { 0x6f,0x36,0xce,0x4a },
1110 { 0x9f,0x09,0xd4,0xea }, { 0xb0,0x7c,0xd6,0x29 },
1111 { 0xa4,0xb2,0xaf,0x31 }, { 0x3f,0x23,0x31,0x2a },
1112 { 0xa5,0x94,0x30,0xc6 }, { 0xa2,0x66,0xc0,0x35 },
1113 { 0x4e,0xbc,0x37,0x74 }, { 0x82,0xca,0xa6,0xfc },
1114 { 0x90,0xd0,0xb0,0xe0 }, { 0xa7,0xd8,0x15,0x33 },
1115 { 0x04,0x98,0x4a,0xf1 }, { 0xec,0xda,0xf7,0x41 },
1116 { 0xcd,0x50,0x0e,0x7f }, { 0x91,0xf6,0x2f,0x17 },
1117 { 0x4d,0xd6,0x8d,0x76 }, { 0xef,0xb0,0x4d,0x43 },
1118 { 0xaa,0x4d,0x54,0xcc }, { 0x96,0x04,0xdf,0xe4 },
1119 { 0xd1,0xb5,0xe3,0x9e }, { 0x6a,0x88,0x1b,0x4c },
1120 { 0x2c,0x1f,0xb8,0xc1 }, { 0x65,0x51,0x7f,0x46 },
1121 { 0x5e,0xea,0x04,0x9d }, { 0x8c,0x35,0x5d,0x01 },
1122 { 0x87,0x74,0x73,0xfa }, { 0x0b,0x41,0x2e,0xfb },
1123 { 0x67,0x1d,0x5a,0xb3 }, { 0xdb,0xd2,0x52,0x92 },
1124 { 0x10,0x56,0x33,0xe9 }, { 0xd6,0x47,0x13,0x6d },
1125 { 0xd7,0x61,0x8c,0x9a }, { 0xa1,0x0c,0x7a,0x37 },
1126 { 0xf8,0x14,0x8e,0x59 }, { 0x13,0x3c,0x89,0xeb },
1127 { 0xa9,0x27,0xee,0xce }, { 0x61,0xc9,0x35,0xb7 },
1128 { 0x1c,0xe5,0xed,0xe1 }, { 0x47,0xb1,0x3c,0x7a },
1129 { 0xd2,0xdf,0x59,0x9c }, { 0xf2,0x73,0x3f,0x55 },
1130 { 0x14,0xce,0x79,0x18 }, { 0xc7,0x37,0xbf,0x73 },
1131 { 0xf7,0xcd,0xea,0x53 }, { 0xfd,0xaa,0x5b,0x5f },
1132 { 0x3d,0x6f,0x14,0xdf }, { 0x44,0xdb,0x86,0x78 },
1133 { 0xaf,0xf3,0x81,0xca }, { 0x68,0xc4,0x3e,0xb9 },
1134 { 0x24,0x34,0x2c,0x38 }, { 0xa3,0x40,0x5f,0xc2 },
1135 { 0x1d,0xc3,0x72,0x16 }, { 0xe2,0x25,0x0c,0xbc },
1136 { 0x3c,0x49,0x8b,0x28 }, { 0x0d,0x95,0x41,0xff },
1137 { 0xa8,0x01,0x71,0x39 }, { 0x0c,0xb3,0xde,0x08 },
1138 { 0xb4,0xe4,0x9c,0xd8 }, { 0x56,0xc1,0x90,0x64 },
1139 { 0xcb,0x84,0x61,0x7b }, { 0x32,0xb6,0x70,0xd5 },
1140 { 0x6c,0x5c,0x74,0x48 }, { 0xb8,0x57,0x42,0xd0 }
1141};
1142
1143static const byte S5[256] = {
1144 0x52,0x09,0x6a,0xd5,0x30,0x36,0xa5,0x38,
1145 0xbf,0x40,0xa3,0x9e,0x81,0xf3,0xd7,0xfb,
1146 0x7c,0xe3,0x39,0x82,0x9b,0x2f,0xff,0x87,
1147 0x34,0x8e,0x43,0x44,0xc4,0xde,0xe9,0xcb,
1148 0x54,0x7b,0x94,0x32,0xa6,0xc2,0x23,0x3d,
1149 0xee,0x4c,0x95,0x0b,0x42,0xfa,0xc3,0x4e,
1150 0x08,0x2e,0xa1,0x66,0x28,0xd9,0x24,0xb2,
1151 0x76,0x5b,0xa2,0x49,0x6d,0x8b,0xd1,0x25,
1152 0x72,0xf8,0xf6,0x64,0x86,0x68,0x98,0x16,
1153 0xd4,0xa4,0x5c,0xcc,0x5d,0x65,0xb6,0x92,
1154 0x6c,0x70,0x48,0x50,0xfd,0xed,0xb9,0xda,
1155 0x5e,0x15,0x46,0x57,0xa7,0x8d,0x9d,0x84,
1156 0x90,0xd8,0xab,0x00,0x8c,0xbc,0xd3,0x0a,
1157 0xf7,0xe4,0x58,0x05,0xb8,0xb3,0x45,0x06,
1158 0xd0,0x2c,0x1e,0x8f,0xca,0x3f,0x0f,0x02,
1159 0xc1,0xaf,0xbd,0x03,0x01,0x13,0x8a,0x6b,
1160 0x3a,0x91,0x11,0x41,0x4f,0x67,0xdc,0xea,
1161 0x97,0xf2,0xcf,0xce,0xf0,0xb4,0xe6,0x73,
1162 0x96,0xac,0x74,0x22,0xe7,0xad,0x35,0x85,
1163 0xe2,0xf9,0x37,0xe8,0x1c,0x75,0xdf,0x6e,
1164 0x47,0xf1,0x1a,0x71,0x1d,0x29,0xc5,0x89,
1165 0x6f,0xb7,0x62,0x0e,0xaa,0x18,0xbe,0x1b,
1166 0xfc,0x56,0x3e,0x4b,0xc6,0xd2,0x79,0x20,
1167 0x9a,0xdb,0xc0,0xfe,0x78,0xcd,0x5a,0xf4,
1168 0x1f,0xdd,0xa8,0x33,0x88,0x07,0xc7,0x31,
1169 0xb1,0x12,0x10,0x59,0x27,0x80,0xec,0x5f,
1170 0x60,0x51,0x7f,0xa9,0x19,0xb5,0x4a,0x0d,
1171 0x2d,0xe5,0x7a,0x9f,0x93,0xc9,0x9c,0xef,
1172 0xa0,0xe0,0x3b,0x4d,0xae,0x2a,0xf5,0xb0,
1173 0xc8,0xeb,0xbb,0x3c,0x83,0x53,0x99,0x61,
1174 0x17,0x2b,0x04,0x7e,0xba,0x77,0xd6,0x26,
1175 0xe1,0x69,0x14,0x63,0x55,0x21,0x0c,0x7d
1176};
1177
1178static const byte U1[256][4] = {
1179 { 0x00,0x00,0x00,0x00 }, { 0x0e,0x09,0x0d,0x0b },
1180 { 0x1c,0x12,0x1a,0x16 }, { 0x12,0x1b,0x17,0x1d },
1181 { 0x38,0x24,0x34,0x2c }, { 0x36,0x2d,0x39,0x27 },
1182 { 0x24,0x36,0x2e,0x3a }, { 0x2a,0x3f,0x23,0x31 },
1183 { 0x70,0x48,0x68,0x58 }, { 0x7e,0x41,0x65,0x53 },
1184 { 0x6c,0x5a,0x72,0x4e }, { 0x62,0x53,0x7f,0x45 },
1185 { 0x48,0x6c,0x5c,0x74 }, { 0x46,0x65,0x51,0x7f },
1186 { 0x54,0x7e,0x46,0x62 }, { 0x5a,0x77,0x4b,0x69 },
1187 { 0xe0,0x90,0xd0,0xb0 }, { 0xee,0x99,0xdd,0xbb },
1188 { 0xfc,0x82,0xca,0xa6 }, { 0xf2,0x8b,0xc7,0xad },
1189 { 0xd8,0xb4,0xe4,0x9c }, { 0xd6,0xbd,0xe9,0x97 },
1190 { 0xc4,0xa6,0xfe,0x8a }, { 0xca,0xaf,0xf3,0x81 },
1191 { 0x90,0xd8,0xb8,0xe8 }, { 0x9e,0xd1,0xb5,0xe3 },
1192 { 0x8c,0xca,0xa2,0xfe }, { 0x82,0xc3,0xaf,0xf5 },
1193 { 0xa8,0xfc,0x8c,0xc4 }, { 0xa6,0xf5,0x81,0xcf },
1194 { 0xb4,0xee,0x96,0xd2 }, { 0xba,0xe7,0x9b,0xd9 },
1195 { 0xdb,0x3b,0xbb,0x7b }, { 0xd5,0x32,0xb6,0x70 },
1196 { 0xc7,0x29,0xa1,0x6d }, { 0xc9,0x20,0xac,0x66 },
1197 { 0xe3,0x1f,0x8f,0x57 }, { 0xed,0x16,0x82,0x5c },
1198 { 0xff,0x0d,0x95,0x41 }, { 0xf1,0x04,0x98,0x4a },
1199 { 0xab,0x73,0xd3,0x23 }, { 0xa5,0x7a,0xde,0x28 },
1200 { 0xb7,0x61,0xc9,0x35 }, { 0xb9,0x68,0xc4,0x3e },
1201 { 0x93,0x57,0xe7,0x0f }, { 0x9d,0x5e,0xea,0x04 },
1202 { 0x8f,0x45,0xfd,0x19 }, { 0x81,0x4c,0xf0,0x12 },
1203 { 0x3b,0xab,0x6b,0xcb }, { 0x35,0xa2,0x66,0xc0 },
1204 { 0x27,0xb9,0x71,0xdd }, { 0x29,0xb0,0x7c,0xd6 },
1205 { 0x03,0x8f,0x5f,0xe7 }, { 0x0d,0x86,0x52,0xec },
1206 { 0x1f,0x9d,0x45,0xf1 }, { 0x11,0x94,0x48,0xfa },
1207 { 0x4b,0xe3,0x03,0x93 }, { 0x45,0xea,0x0e,0x98 },
1208 { 0x57,0xf1,0x19,0x85 }, { 0x59,0xf8,0x14,0x8e },
1209 { 0x73,0xc7,0x37,0xbf }, { 0x7d,0xce,0x3a,0xb4 },
1210 { 0x6f,0xd5,0x2d,0xa9 }, { 0x61,0xdc,0x20,0xa2 },
1211 { 0xad,0x76,0x6d,0xf6 }, { 0xa3,0x7f,0x60,0xfd },
1212 { 0xb1,0x64,0x77,0xe0 }, { 0xbf,0x6d,0x7a,0xeb },
1213 { 0x95,0x52,0x59,0xda }, { 0x9b,0x5b,0x54,0xd1 },
1214 { 0x89,0x40,0x43,0xcc }, { 0x87,0x49,0x4e,0xc7 },
1215 { 0xdd,0x3e,0x05,0xae }, { 0xd3,0x37,0x08,0xa5 },
1216 { 0xc1,0x2c,0x1f,0xb8 }, { 0xcf,0x25,0x12,0xb3 },
1217 { 0xe5,0x1a,0x31,0x82 }, { 0xeb,0x13,0x3c,0x89 },
1218 { 0xf9,0x08,0x2b,0x94 }, { 0xf7,0x01,0x26,0x9f },
1219 { 0x4d,0xe6,0xbd,0x46 }, { 0x43,0xef,0xb0,0x4d },
1220 { 0x51,0xf4,0xa7,0x50 }, { 0x5f,0xfd,0xaa,0x5b },
1221 { 0x75,0xc2,0x89,0x6a }, { 0x7b,0xcb,0x84,0x61 },
1222 { 0x69,0xd0,0x93,0x7c }, { 0x67,0xd9,0x9e,0x77 },
1223 { 0x3d,0xae,0xd5,0x1e }, { 0x33,0xa7,0xd8,0x15 },
1224 { 0x21,0xbc,0xcf,0x08 }, { 0x2f,0xb5,0xc2,0x03 },
1225 { 0x05,0x8a,0xe1,0x32 }, { 0x0b,0x83,0xec,0x39 },
1226 { 0x19,0x98,0xfb,0x24 }, { 0x17,0x91,0xf6,0x2f },
1227 { 0x76,0x4d,0xd6,0x8d }, { 0x78,0x44,0xdb,0x86 },
1228 { 0x6a,0x5f,0xcc,0x9b }, { 0x64,0x56,0xc1,0x90 },
1229 { 0x4e,0x69,0xe2,0xa1 }, { 0x40,0x60,0xef,0xaa },
1230 { 0x52,0x7b,0xf8,0xb7 }, { 0x5c,0x72,0xf5,0xbc },
1231 { 0x06,0x05,0xbe,0xd5 }, { 0x08,0x0c,0xb3,0xde },
1232 { 0x1a,0x17,0xa4,0xc3 }, { 0x14,0x1e,0xa9,0xc8 },
1233 { 0x3e,0x21,0x8a,0xf9 }, { 0x30,0x28,0x87,0xf2 },
1234 { 0x22,0x33,0x90,0xef }, { 0x2c,0x3a,0x9d,0xe4 },
1235 { 0x96,0xdd,0x06,0x3d }, { 0x98,0xd4,0x0b,0x36 },
1236 { 0x8a,0xcf,0x1c,0x2b }, { 0x84,0xc6,0x11,0x20 },
1237 { 0xae,0xf9,0x32,0x11 }, { 0xa0,0xf0,0x3f,0x1a },
1238 { 0xb2,0xeb,0x28,0x07 }, { 0xbc,0xe2,0x25,0x0c },
1239 { 0xe6,0x95,0x6e,0x65 }, { 0xe8,0x9c,0x63,0x6e },
1240 { 0xfa,0x87,0x74,0x73 }, { 0xf4,0x8e,0x79,0x78 },
1241 { 0xde,0xb1,0x5a,0x49 }, { 0xd0,0xb8,0x57,0x42 },
1242 { 0xc2,0xa3,0x40,0x5f }, { 0xcc,0xaa,0x4d,0x54 },
1243 { 0x41,0xec,0xda,0xf7 }, { 0x4f,0xe5,0xd7,0xfc },
1244 { 0x5d,0xfe,0xc0,0xe1 }, { 0x53,0xf7,0xcd,0xea },
1245 { 0x79,0xc8,0xee,0xdb }, { 0x77,0xc1,0xe3,0xd0 },
1246 { 0x65,0xda,0xf4,0xcd }, { 0x6b,0xd3,0xf9,0xc6 },
1247 { 0x31,0xa4,0xb2,0xaf }, { 0x3f,0xad,0xbf,0xa4 },
1248 { 0x2d,0xb6,0xa8,0xb9 }, { 0x23,0xbf,0xa5,0xb2 },
1249 { 0x09,0x80,0x86,0x83 }, { 0x07,0x89,0x8b,0x88 },
1250 { 0x15,0x92,0x9c,0x95 }, { 0x1b,0x9b,0x91,0x9e },
1251 { 0xa1,0x7c,0x0a,0x47 }, { 0xaf,0x75,0x07,0x4c },
1252 { 0xbd,0x6e,0x10,0x51 }, { 0xb3,0x67,0x1d,0x5a },
1253 { 0x99,0x58,0x3e,0x6b }, { 0x97,0x51,0x33,0x60 },
1254 { 0x85,0x4a,0x24,0x7d }, { 0x8b,0x43,0x29,0x76 },
1255 { 0xd1,0x34,0x62,0x1f }, { 0xdf,0x3d,0x6f,0x14 },
1256 { 0xcd,0x26,0x78,0x09 }, { 0xc3,0x2f,0x75,0x02 },
1257 { 0xe9,0x10,0x56,0x33 }, { 0xe7,0x19,0x5b,0x38 },
1258 { 0xf5,0x02,0x4c,0x25 }, { 0xfb,0x0b,0x41,0x2e },
1259 { 0x9a,0xd7,0x61,0x8c }, { 0x94,0xde,0x6c,0x87 },
1260 { 0x86,0xc5,0x7b,0x9a }, { 0x88,0xcc,0x76,0x91 },
1261 { 0xa2,0xf3,0x55,0xa0 }, { 0xac,0xfa,0x58,0xab },
1262 { 0xbe,0xe1,0x4f,0xb6 }, { 0xb0,0xe8,0x42,0xbd },
1263 { 0xea,0x9f,0x09,0xd4 }, { 0xe4,0x96,0x04,0xdf },
1264 { 0xf6,0x8d,0x13,0xc2 }, { 0xf8,0x84,0x1e,0xc9 },
1265 { 0xd2,0xbb,0x3d,0xf8 }, { 0xdc,0xb2,0x30,0xf3 },
1266 { 0xce,0xa9,0x27,0xee }, { 0xc0,0xa0,0x2a,0xe5 },
1267 { 0x7a,0x47,0xb1,0x3c }, { 0x74,0x4e,0xbc,0x37 },
1268 { 0x66,0x55,0xab,0x2a }, { 0x68,0x5c,0xa6,0x21 },
1269 { 0x42,0x63,0x85,0x10 }, { 0x4c,0x6a,0x88,0x1b },
1270 { 0x5e,0x71,0x9f,0x06 }, { 0x50,0x78,0x92,0x0d },
1271 { 0x0a,0x0f,0xd9,0x64 }, { 0x04,0x06,0xd4,0x6f },
1272 { 0x16,0x1d,0xc3,0x72 }, { 0x18,0x14,0xce,0x79 },
1273 { 0x32,0x2b,0xed,0x48 }, { 0x3c,0x22,0xe0,0x43 },
1274 { 0x2e,0x39,0xf7,0x5e }, { 0x20,0x30,0xfa,0x55 },
1275 { 0xec,0x9a,0xb7,0x01 }, { 0xe2,0x93,0xba,0x0a },
1276 { 0xf0,0x88,0xad,0x17 }, { 0xfe,0x81,0xa0,0x1c },
1277 { 0xd4,0xbe,0x83,0x2d }, { 0xda,0xb7,0x8e,0x26 },
1278 { 0xc8,0xac,0x99,0x3b }, { 0xc6,0xa5,0x94,0x30 },
1279 { 0x9c,0xd2,0xdf,0x59 }, { 0x92,0xdb,0xd2,0x52 },
1280 { 0x80,0xc0,0xc5,0x4f }, { 0x8e,0xc9,0xc8,0x44 },
1281 { 0xa4,0xf6,0xeb,0x75 }, { 0xaa,0xff,0xe6,0x7e },
1282 { 0xb8,0xe4,0xf1,0x63 }, { 0xb6,0xed,0xfc,0x68 },
1283 { 0x0c,0x0a,0x67,0xb1 }, { 0x02,0x03,0x6a,0xba },
1284 { 0x10,0x18,0x7d,0xa7 }, { 0x1e,0x11,0x70,0xac },
1285 { 0x34,0x2e,0x53,0x9d }, { 0x3a,0x27,0x5e,0x96 },
1286 { 0x28,0x3c,0x49,0x8b }, { 0x26,0x35,0x44,0x80 },
1287 { 0x7c,0x42,0x0f,0xe9 }, { 0x72,0x4b,0x02,0xe2 },
1288 { 0x60,0x50,0x15,0xff }, { 0x6e,0x59,0x18,0xf4 },
1289 { 0x44,0x66,0x3b,0xc5 }, { 0x4a,0x6f,0x36,0xce },
1290 { 0x58,0x74,0x21,0xd3 }, { 0x56,0x7d,0x2c,0xd8 },
1291 { 0x37,0xa1,0x0c,0x7a }, { 0x39,0xa8,0x01,0x71 },
1292 { 0x2b,0xb3,0x16,0x6c }, { 0x25,0xba,0x1b,0x67 },
1293 { 0x0f,0x85,0x38,0x56 }, { 0x01,0x8c,0x35,0x5d },
1294 { 0x13,0x97,0x22,0x40 }, { 0x1d,0x9e,0x2f,0x4b },
1295 { 0x47,0xe9,0x64,0x22 }, { 0x49,0xe0,0x69,0x29 },
1296 { 0x5b,0xfb,0x7e,0x34 }, { 0x55,0xf2,0x73,0x3f },
1297 { 0x7f,0xcd,0x50,0x0e }, { 0x71,0xc4,0x5d,0x05 },
1298 { 0x63,0xdf,0x4a,0x18 }, { 0x6d,0xd6,0x47,0x13 },
1299 { 0xd7,0x31,0xdc,0xca }, { 0xd9,0x38,0xd1,0xc1 },
1300 { 0xcb,0x23,0xc6,0xdc }, { 0xc5,0x2a,0xcb,0xd7 },
1301 { 0xef,0x15,0xe8,0xe6 }, { 0xe1,0x1c,0xe5,0xed },
1302 { 0xf3,0x07,0xf2,0xf0 }, { 0xfd,0x0e,0xff,0xfb },
1303 { 0xa7,0x79,0xb4,0x92 }, { 0xa9,0x70,0xb9,0x99 },
1304 { 0xbb,0x6b,0xae,0x84 }, { 0xb5,0x62,0xa3,0x8f },
1305 { 0x9f,0x5d,0x80,0xbe }, { 0x91,0x54,0x8d,0xb5 },
1306 { 0x83,0x4f,0x9a,0xa8 }, { 0x8d,0x46,0x97,0xa3 }
1307};
1308
1309static const byte U2[256][4] = {
1310 { 0x00,0x00,0x00,0x00 }, { 0x0b,0x0e,0x09,0x0d },
1311 { 0x16,0x1c,0x12,0x1a }, { 0x1d,0x12,0x1b,0x17 },
1312 { 0x2c,0x38,0x24,0x34 }, { 0x27,0x36,0x2d,0x39 },
1313 { 0x3a,0x24,0x36,0x2e }, { 0x31,0x2a,0x3f,0x23 },
1314 { 0x58,0x70,0x48,0x68 }, { 0x53,0x7e,0x41,0x65 },
1315 { 0x4e,0x6c,0x5a,0x72 }, { 0x45,0x62,0x53,0x7f },
1316 { 0x74,0x48,0x6c,0x5c }, { 0x7f,0x46,0x65,0x51 },
1317 { 0x62,0x54,0x7e,0x46 }, { 0x69,0x5a,0x77,0x4b },
1318 { 0xb0,0xe0,0x90,0xd0 }, { 0xbb,0xee,0x99,0xdd },
1319 { 0xa6,0xfc,0x82,0xca }, { 0xad,0xf2,0x8b,0xc7 },
1320 { 0x9c,0xd8,0xb4,0xe4 }, { 0x97,0xd6,0xbd,0xe9 },
1321 { 0x8a,0xc4,0xa6,0xfe }, { 0x81,0xca,0xaf,0xf3 },
1322 { 0xe8,0x90,0xd8,0xb8 }, { 0xe3,0x9e,0xd1,0xb5 },
1323 { 0xfe,0x8c,0xca,0xa2 }, { 0xf5,0x82,0xc3,0xaf },
1324 { 0xc4,0xa8,0xfc,0x8c }, { 0xcf,0xa6,0xf5,0x81 },
1325 { 0xd2,0xb4,0xee,0x96 }, { 0xd9,0xba,0xe7,0x9b },
1326 { 0x7b,0xdb,0x3b,0xbb }, { 0x70,0xd5,0x32,0xb6 },
1327 { 0x6d,0xc7,0x29,0xa1 }, { 0x66,0xc9,0x20,0xac },
1328 { 0x57,0xe3,0x1f,0x8f }, { 0x5c,0xed,0x16,0x82 },
1329 { 0x41,0xff,0x0d,0x95 }, { 0x4a,0xf1,0x04,0x98 },
1330 { 0x23,0xab,0x73,0xd3 }, { 0x28,0xa5,0x7a,0xde },
1331 { 0x35,0xb7,0x61,0xc9 }, { 0x3e,0xb9,0x68,0xc4 },
1332 { 0x0f,0x93,0x57,0xe7 }, { 0x04,0x9d,0x5e,0xea },
1333 { 0x19,0x8f,0x45,0xfd }, { 0x12,0x81,0x4c,0xf0 },
1334 { 0xcb,0x3b,0xab,0x6b }, { 0xc0,0x35,0xa2,0x66 },
1335 { 0xdd,0x27,0xb9,0x71 }, { 0xd6,0x29,0xb0,0x7c },
1336 { 0xe7,0x03,0x8f,0x5f }, { 0xec,0x0d,0x86,0x52 },
1337 { 0xf1,0x1f,0x9d,0x45 }, { 0xfa,0x11,0x94,0x48 },
1338 { 0x93,0x4b,0xe3,0x03 }, { 0x98,0x45,0xea,0x0e },
1339 { 0x85,0x57,0xf1,0x19 }, { 0x8e,0x59,0xf8,0x14 },
1340 { 0xbf,0x73,0xc7,0x37 }, { 0xb4,0x7d,0xce,0x3a },
1341 { 0xa9,0x6f,0xd5,0x2d }, { 0xa2,0x61,0xdc,0x20 },
1342 { 0xf6,0xad,0x76,0x6d }, { 0xfd,0xa3,0x7f,0x60 },
1343 { 0xe0,0xb1,0x64,0x77 }, { 0xeb,0xbf,0x6d,0x7a },
1344 { 0xda,0x95,0x52,0x59 }, { 0xd1,0x9b,0x5b,0x54 },
1345 { 0xcc,0x89,0x40,0x43 }, { 0xc7,0x87,0x49,0x4e },
1346 { 0xae,0xdd,0x3e,0x05 }, { 0xa5,0xd3,0x37,0x08 },
1347 { 0xb8,0xc1,0x2c,0x1f }, { 0xb3,0xcf,0x25,0x12 },
1348 { 0x82,0xe5,0x1a,0x31 }, { 0x89,0xeb,0x13,0x3c },
1349 { 0x94,0xf9,0x08,0x2b }, { 0x9f,0xf7,0x01,0x26 },
1350 { 0x46,0x4d,0xe6,0xbd }, { 0x4d,0x43,0xef,0xb0 },
1351 { 0x50,0x51,0xf4,0xa7 }, { 0x5b,0x5f,0xfd,0xaa },
1352 { 0x6a,0x75,0xc2,0x89 }, { 0x61,0x7b,0xcb,0x84 },
1353 { 0x7c,0x69,0xd0,0x93 }, { 0x77,0x67,0xd9,0x9e },
1354 { 0x1e,0x3d,0xae,0xd5 }, { 0x15,0x33,0xa7,0xd8 },
1355 { 0x08,0x21,0xbc,0xcf }, { 0x03,0x2f,0xb5,0xc2 },
1356 { 0x32,0x05,0x8a,0xe1 }, { 0x39,0x0b,0x83,0xec },
1357 { 0x24,0x19,0x98,0xfb }, { 0x2f,0x17,0x91,0xf6 },
1358 { 0x8d,0x76,0x4d,0xd6 }, { 0x86,0x78,0x44,0xdb },
1359 { 0x9b,0x6a,0x5f,0xcc }, { 0x90,0x64,0x56,0xc1 },
1360 { 0xa1,0x4e,0x69,0xe2 }, { 0xaa,0x40,0x60,0xef },
1361 { 0xb7,0x52,0x7b,0xf8 }, { 0xbc,0x5c,0x72,0xf5 },
1362 { 0xd5,0x06,0x05,0xbe }, { 0xde,0x08,0x0c,0xb3 },
1363 { 0xc3,0x1a,0x17,0xa4 }, { 0xc8,0x14,0x1e,0xa9 },
1364 { 0xf9,0x3e,0x21,0x8a }, { 0xf2,0x30,0x28,0x87 },
1365 { 0xef,0x22,0x33,0x90 }, { 0xe4,0x2c,0x3a,0x9d },
1366 { 0x3d,0x96,0xdd,0x06 }, { 0x36,0x98,0xd4,0x0b },
1367 { 0x2b,0x8a,0xcf,0x1c }, { 0x20,0x84,0xc6,0x11 },
1368 { 0x11,0xae,0xf9,0x32 }, { 0x1a,0xa0,0xf0,0x3f },
1369 { 0x07,0xb2,0xeb,0x28 }, { 0x0c,0xbc,0xe2,0x25 },
1370 { 0x65,0xe6,0x95,0x6e }, { 0x6e,0xe8,0x9c,0x63 },
1371 { 0x73,0xfa,0x87,0x74 }, { 0x78,0xf4,0x8e,0x79 },
1372 { 0x49,0xde,0xb1,0x5a }, { 0x42,0xd0,0xb8,0x57 },
1373 { 0x5f,0xc2,0xa3,0x40 }, { 0x54,0xcc,0xaa,0x4d },
1374 { 0xf7,0x41,0xec,0xda }, { 0xfc,0x4f,0xe5,0xd7 },
1375 { 0xe1,0x5d,0xfe,0xc0 }, { 0xea,0x53,0xf7,0xcd },
1376 { 0xdb,0x79,0xc8,0xee }, { 0xd0,0x77,0xc1,0xe3 },
1377 { 0xcd,0x65,0xda,0xf4 }, { 0xc6,0x6b,0xd3,0xf9 },
1378 { 0xaf,0x31,0xa4,0xb2 }, { 0xa4,0x3f,0xad,0xbf },
1379 { 0xb9,0x2d,0xb6,0xa8 }, { 0xb2,0x23,0xbf,0xa5 },
1380 { 0x83,0x09,0x80,0x86 }, { 0x88,0x07,0x89,0x8b },
1381 { 0x95,0x15,0x92,0x9c }, { 0x9e,0x1b,0x9b,0x91 },
1382 { 0x47,0xa1,0x7c,0x0a }, { 0x4c,0xaf,0x75,0x07 },
1383 { 0x51,0xbd,0x6e,0x10 }, { 0x5a,0xb3,0x67,0x1d },
1384 { 0x6b,0x99,0x58,0x3e }, { 0x60,0x97,0x51,0x33 },
1385 { 0x7d,0x85,0x4a,0x24 }, { 0x76,0x8b,0x43,0x29 },
1386 { 0x1f,0xd1,0x34,0x62 }, { 0x14,0xdf,0x3d,0x6f },
1387 { 0x09,0xcd,0x26,0x78 }, { 0x02,0xc3,0x2f,0x75 },
1388 { 0x33,0xe9,0x10,0x56 }, { 0x38,0xe7,0x19,0x5b },
1389 { 0x25,0xf5,0x02,0x4c }, { 0x2e,0xfb,0x0b,0x41 },
1390 { 0x8c,0x9a,0xd7,0x61 }, { 0x87,0x94,0xde,0x6c },
1391 { 0x9a,0x86,0xc5,0x7b }, { 0x91,0x88,0xcc,0x76 },
1392 { 0xa0,0xa2,0xf3,0x55 }, { 0xab,0xac,0xfa,0x58 },
1393 { 0xb6,0xbe,0xe1,0x4f }, { 0xbd,0xb0,0xe8,0x42 },
1394 { 0xd4,0xea,0x9f,0x09 }, { 0xdf,0xe4,0x96,0x04 },
1395 { 0xc2,0xf6,0x8d,0x13 }, { 0xc9,0xf8,0x84,0x1e },
1396 { 0xf8,0xd2,0xbb,0x3d }, { 0xf3,0xdc,0xb2,0x30 },
1397 { 0xee,0xce,0xa9,0x27 }, { 0xe5,0xc0,0xa0,0x2a },
1398 { 0x3c,0x7a,0x47,0xb1 }, { 0x37,0x74,0x4e,0xbc },
1399 { 0x2a,0x66,0x55,0xab }, { 0x21,0x68,0x5c,0xa6 },
1400 { 0x10,0x42,0x63,0x85 }, { 0x1b,0x4c,0x6a,0x88 },
1401 { 0x06,0x5e,0x71,0x9f }, { 0x0d,0x50,0x78,0x92 },
1402 { 0x64,0x0a,0x0f,0xd9 }, { 0x6f,0x04,0x06,0xd4 },
1403 { 0x72,0x16,0x1d,0xc3 }, { 0x79,0x18,0x14,0xce },
1404 { 0x48,0x32,0x2b,0xed }, { 0x43,0x3c,0x22,0xe0 },
1405 { 0x5e,0x2e,0x39,0xf7 }, { 0x55,0x20,0x30,0xfa },
1406 { 0x01,0xec,0x9a,0xb7 }, { 0x0a,0xe2,0x93,0xba },
1407 { 0x17,0xf0,0x88,0xad }, { 0x1c,0xfe,0x81,0xa0 },
1408 { 0x2d,0xd4,0xbe,0x83 }, { 0x26,0xda,0xb7,0x8e },
1409 { 0x3b,0xc8,0xac,0x99 }, { 0x30,0xc6,0xa5,0x94 },
1410 { 0x59,0x9c,0xd2,0xdf }, { 0x52,0x92,0xdb,0xd2 },
1411 { 0x4f,0x80,0xc0,0xc5 }, { 0x44,0x8e,0xc9,0xc8 },
1412 { 0x75,0xa4,0xf6,0xeb }, { 0x7e,0xaa,0xff,0xe6 },
1413 { 0x63,0xb8,0xe4,0xf1 }, { 0x68,0xb6,0xed,0xfc },
1414 { 0xb1,0x0c,0x0a,0x67 }, { 0xba,0x02,0x03,0x6a },
1415 { 0xa7,0x10,0x18,0x7d }, { 0xac,0x1e,0x11,0x70 },
1416 { 0x9d,0x34,0x2e,0x53 }, { 0x96,0x3a,0x27,0x5e },
1417 { 0x8b,0x28,0x3c,0x49 }, { 0x80,0x26,0x35,0x44 },
1418 { 0xe9,0x7c,0x42,0x0f }, { 0xe2,0x72,0x4b,0x02 },
1419 { 0xff,0x60,0x50,0x15 }, { 0xf4,0x6e,0x59,0x18 },
1420 { 0xc5,0x44,0x66,0x3b }, { 0xce,0x4a,0x6f,0x36 },
1421 { 0xd3,0x58,0x74,0x21 }, { 0xd8,0x56,0x7d,0x2c },
1422 { 0x7a,0x37,0xa1,0x0c }, { 0x71,0x39,0xa8,0x01 },
1423 { 0x6c,0x2b,0xb3,0x16 }, { 0x67,0x25,0xba,0x1b },
1424 { 0x56,0x0f,0x85,0x38 }, { 0x5d,0x01,0x8c,0x35 },
1425 { 0x40,0x13,0x97,0x22 }, { 0x4b,0x1d,0x9e,0x2f },
1426 { 0x22,0x47,0xe9,0x64 }, { 0x29,0x49,0xe0,0x69 },
1427 { 0x34,0x5b,0xfb,0x7e }, { 0x3f,0x55,0xf2,0x73 },
1428 { 0x0e,0x7f,0xcd,0x50 }, { 0x05,0x71,0xc4,0x5d },
1429 { 0x18,0x63,0xdf,0x4a }, { 0x13,0x6d,0xd6,0x47 },
1430 { 0xca,0xd7,0x31,0xdc }, { 0xc1,0xd9,0x38,0xd1 },
1431 { 0xdc,0xcb,0x23,0xc6 }, { 0xd7,0xc5,0x2a,0xcb },
1432 { 0xe6,0xef,0x15,0xe8 }, { 0xed,0xe1,0x1c,0xe5 },
1433 { 0xf0,0xf3,0x07,0xf2 }, { 0xfb,0xfd,0x0e,0xff },
1434 { 0x92,0xa7,0x79,0xb4 }, { 0x99,0xa9,0x70,0xb9 },
1435 { 0x84,0xbb,0x6b,0xae }, { 0x8f,0xb5,0x62,0xa3 },
1436 { 0xbe,0x9f,0x5d,0x80 }, { 0xb5,0x91,0x54,0x8d },
1437 { 0xa8,0x83,0x4f,0x9a }, { 0xa3,0x8d,0x46,0x97 }
1438};
1439
1440static const byte U3[256][4] = {
1441 { 0x00,0x00,0x00,0x00 }, { 0x0d,0x0b,0x0e,0x09 },
1442 { 0x1a,0x16,0x1c,0x12 }, { 0x17,0x1d,0x12,0x1b },
1443 { 0x34,0x2c,0x38,0x24 }, { 0x39,0x27,0x36,0x2d },
1444 { 0x2e,0x3a,0x24,0x36 }, { 0x23,0x31,0x2a,0x3f },
1445 { 0x68,0x58,0x70,0x48 }, { 0x65,0x53,0x7e,0x41 },
1446 { 0x72,0x4e,0x6c,0x5a }, { 0x7f,0x45,0x62,0x53 },
1447 { 0x5c,0x74,0x48,0x6c }, { 0x51,0x7f,0x46,0x65 },
1448 { 0x46,0x62,0x54,0x7e }, { 0x4b,0x69,0x5a,0x77 },
1449 { 0xd0,0xb0,0xe0,0x90 }, { 0xdd,0xbb,0xee,0x99 },
1450 { 0xca,0xa6,0xfc,0x82 }, { 0xc7,0xad,0xf2,0x8b },
1451 { 0xe4,0x9c,0xd8,0xb4 }, { 0xe9,0x97,0xd6,0xbd },
1452 { 0xfe,0x8a,0xc4,0xa6 }, { 0xf3,0x81,0xca,0xaf },
1453 { 0xb8,0xe8,0x90,0xd8 }, { 0xb5,0xe3,0x9e,0xd1 },
1454 { 0xa2,0xfe,0x8c,0xca }, { 0xaf,0xf5,0x82,0xc3 },
1455 { 0x8c,0xc4,0xa8,0xfc }, { 0x81,0xcf,0xa6,0xf5 },
1456 { 0x96,0xd2,0xb4,0xee }, { 0x9b,0xd9,0xba,0xe7 },
1457 { 0xbb,0x7b,0xdb,0x3b }, { 0xb6,0x70,0xd5,0x32 },
1458 { 0xa1,0x6d,0xc7,0x29 }, { 0xac,0x66,0xc9,0x20 },
1459 { 0x8f,0x57,0xe3,0x1f }, { 0x82,0x5c,0xed,0x16 },
1460 { 0x95,0x41,0xff,0x0d }, { 0x98,0x4a,0xf1,0x04 },
1461 { 0xd3,0x23,0xab,0x73 }, { 0xde,0x28,0xa5,0x7a },
1462 { 0xc9,0x35,0xb7,0x61 }, { 0xc4,0x3e,0xb9,0x68 },
1463 { 0xe7,0x0f,0x93,0x57 }, { 0xea,0x04,0x9d,0x5e },
1464 { 0xfd,0x19,0x8f,0x45 }, { 0xf0,0x12,0x81,0x4c },
1465 { 0x6b,0xcb,0x3b,0xab }, { 0x66,0xc0,0x35,0xa2 },
1466 { 0x71,0xdd,0x27,0xb9 }, { 0x7c,0xd6,0x29,0xb0 },
1467 { 0x5f,0xe7,0x03,0x8f }, { 0x52,0xec,0x0d,0x86 },
1468 { 0x45,0xf1,0x1f,0x9d }, { 0x48,0xfa,0x11,0x94 },
1469 { 0x03,0x93,0x4b,0xe3 }, { 0x0e,0x98,0x45,0xea },
1470 { 0x19,0x85,0x57,0xf1 }, { 0x14,0x8e,0x59,0xf8 },
1471 { 0x37,0xbf,0x73,0xc7 }, { 0x3a,0xb4,0x7d,0xce },
1472 { 0x2d,0xa9,0x6f,0xd5 }, { 0x20,0xa2,0x61,0xdc },
1473 { 0x6d,0xf6,0xad,0x76 }, { 0x60,0xfd,0xa3,0x7f },
1474 { 0x77,0xe0,0xb1,0x64 }, { 0x7a,0xeb,0xbf,0x6d },
1475 { 0x59,0xda,0x95,0x52 }, { 0x54,0xd1,0x9b,0x5b },
1476 { 0x43,0xcc,0x89,0x40 }, { 0x4e,0xc7,0x87,0x49 },
1477 { 0x05,0xae,0xdd,0x3e }, { 0x08,0xa5,0xd3,0x37 },
1478 { 0x1f,0xb8,0xc1,0x2c }, { 0x12,0xb3,0xcf,0x25 },
1479 { 0x31,0x82,0xe5,0x1a }, { 0x3c,0x89,0xeb,0x13 },
1480 { 0x2b,0x94,0xf9,0x08 }, { 0x26,0x9f,0xf7,0x01 },
1481 { 0xbd,0x46,0x4d,0xe6 }, { 0xb0,0x4d,0x43,0xef },
1482 { 0xa7,0x50,0x51,0xf4 }, { 0xaa,0x5b,0x5f,0xfd },
1483 { 0x89,0x6a,0x75,0xc2 }, { 0x84,0x61,0x7b,0xcb },
1484 { 0x93,0x7c,0x69,0xd0 }, { 0x9e,0x77,0x67,0xd9 },
1485 { 0xd5,0x1e,0x3d,0xae }, { 0xd8,0x15,0x33,0xa7 },
1486 { 0xcf,0x08,0x21,0xbc }, { 0xc2,0x03,0x2f,0xb5 },
1487 { 0xe1,0x32,0x05,0x8a }, { 0xec,0x39,0x0b,0x83 },
1488 { 0xfb,0x24,0x19,0x98 }, { 0xf6,0x2f,0x17,0x91 },
1489 { 0xd6,0x8d,0x76,0x4d }, { 0xdb,0x86,0x78,0x44 },
1490 { 0xcc,0x9b,0x6a,0x5f }, { 0xc1,0x90,0x64,0x56 },
1491 { 0xe2,0xa1,0x4e,0x69 }, { 0xef,0xaa,0x40,0x60 },
1492 { 0xf8,0xb7,0x52,0x7b }, { 0xf5,0xbc,0x5c,0x72 },
1493 { 0xbe,0xd5,0x06,0x05 }, { 0xb3,0xde,0x08,0x0c },
1494 { 0xa4,0xc3,0x1a,0x17 }, { 0xa9,0xc8,0x14,0x1e },
1495 { 0x8a,0xf9,0x3e,0x21 }, { 0x87,0xf2,0x30,0x28 },
1496 { 0x90,0xef,0x22,0x33 }, { 0x9d,0xe4,0x2c,0x3a },
1497 { 0x06,0x3d,0x96,0xdd }, { 0x0b,0x36,0x98,0xd4 },
1498 { 0x1c,0x2b,0x8a,0xcf }, { 0x11,0x20,0x84,0xc6 },
1499 { 0x32,0x11,0xae,0xf9 }, { 0x3f,0x1a,0xa0,0xf0 },
1500 { 0x28,0x07,0xb2,0xeb }, { 0x25,0x0c,0xbc,0xe2 },
1501 { 0x6e,0x65,0xe6,0x95 }, { 0x63,0x6e,0xe8,0x9c },
1502 { 0x74,0x73,0xfa,0x87 }, { 0x79,0x78,0xf4,0x8e },
1503 { 0x5a,0x49,0xde,0xb1 }, { 0x57,0x42,0xd0,0xb8 },
1504 { 0x40,0x5f,0xc2,0xa3 }, { 0x4d,0x54,0xcc,0xaa },
1505 { 0xda,0xf7,0x41,0xec }, { 0xd7,0xfc,0x4f,0xe5 },
1506 { 0xc0,0xe1,0x5d,0xfe }, { 0xcd,0xea,0x53,0xf7 },
1507 { 0xee,0xdb,0x79,0xc8 }, { 0xe3,0xd0,0x77,0xc1 },
1508 { 0xf4,0xcd,0x65,0xda }, { 0xf9,0xc6,0x6b,0xd3 },
1509 { 0xb2,0xaf,0x31,0xa4 }, { 0xbf,0xa4,0x3f,0xad },
1510 { 0xa8,0xb9,0x2d,0xb6 }, { 0xa5,0xb2,0x23,0xbf },
1511 { 0x86,0x83,0x09,0x80 }, { 0x8b,0x88,0x07,0x89 },
1512 { 0x9c,0x95,0x15,0x92 }, { 0x91,0x9e,0x1b,0x9b },
1513 { 0x0a,0x47,0xa1,0x7c }, { 0x07,0x4c,0xaf,0x75 },
1514 { 0x10,0x51,0xbd,0x6e }, { 0x1d,0x5a,0xb3,0x67 },
1515 { 0x3e,0x6b,0x99,0x58 }, { 0x33,0x60,0x97,0x51 },
1516 { 0x24,0x7d,0x85,0x4a }, { 0x29,0x76,0x8b,0x43 },
1517 { 0x62,0x1f,0xd1,0x34 }, { 0x6f,0x14,0xdf,0x3d },
1518 { 0x78,0x09,0xcd,0x26 }, { 0x75,0x02,0xc3,0x2f },
1519 { 0x56,0x33,0xe9,0x10 }, { 0x5b,0x38,0xe7,0x19 },
1520 { 0x4c,0x25,0xf5,0x02 }, { 0x41,0x2e,0xfb,0x0b },
1521 { 0x61,0x8c,0x9a,0xd7 }, { 0x6c,0x87,0x94,0xde },
1522 { 0x7b,0x9a,0x86,0xc5 }, { 0x76,0x91,0x88,0xcc },
1523 { 0x55,0xa0,0xa2,0xf3 }, { 0x58,0xab,0xac,0xfa },
1524 { 0x4f,0xb6,0xbe,0xe1 }, { 0x42,0xbd,0xb0,0xe8 },
1525 { 0x09,0xd4,0xea,0x9f }, { 0x04,0xdf,0xe4,0x96 },
1526 { 0x13,0xc2,0xf6,0x8d }, { 0x1e,0xc9,0xf8,0x84 },
1527 { 0x3d,0xf8,0xd2,0xbb }, { 0x30,0xf3,0xdc,0xb2 },
1528 { 0x27,0xee,0xce,0xa9 }, { 0x2a,0xe5,0xc0,0xa0 },
1529 { 0xb1,0x3c,0x7a,0x47 }, { 0xbc,0x37,0x74,0x4e },
1530 { 0xab,0x2a,0x66,0x55 }, { 0xa6,0x21,0x68,0x5c },
1531 { 0x85,0x10,0x42,0x63 }, { 0x88,0x1b,0x4c,0x6a },
1532 { 0x9f,0x06,0x5e,0x71 }, { 0x92,0x0d,0x50,0x78 },
1533 { 0xd9,0x64,0x0a,0x0f }, { 0xd4,0x6f,0x04,0x06 },
1534 { 0xc3,0x72,0x16,0x1d }, { 0xce,0x79,0x18,0x14 },
1535 { 0xed,0x48,0x32,0x2b }, { 0xe0,0x43,0x3c,0x22 },
1536 { 0xf7,0x5e,0x2e,0x39 }, { 0xfa,0x55,0x20,0x30 },
1537 { 0xb7,0x01,0xec,0x9a }, { 0xba,0x0a,0xe2,0x93 },
1538 { 0xad,0x17,0xf0,0x88 }, { 0xa0,0x1c,0xfe,0x81 },
1539 { 0x83,0x2d,0xd4,0xbe }, { 0x8e,0x26,0xda,0xb7 },
1540 { 0x99,0x3b,0xc8,0xac }, { 0x94,0x30,0xc6,0xa5 },
1541 { 0xdf,0x59,0x9c,0xd2 }, { 0xd2,0x52,0x92,0xdb },
1542 { 0xc5,0x4f,0x80,0xc0 }, { 0xc8,0x44,0x8e,0xc9 },
1543 { 0xeb,0x75,0xa4,0xf6 }, { 0xe6,0x7e,0xaa,0xff },
1544 { 0xf1,0x63,0xb8,0xe4 }, { 0xfc,0x68,0xb6,0xed },
1545 { 0x67,0xb1,0x0c,0x0a }, { 0x6a,0xba,0x02,0x03 },
1546 { 0x7d,0xa7,0x10,0x18 }, { 0x70,0xac,0x1e,0x11 },
1547 { 0x53,0x9d,0x34,0x2e }, { 0x5e,0x96,0x3a,0x27 },
1548 { 0x49,0x8b,0x28,0x3c }, { 0x44,0x80,0x26,0x35 },
1549 { 0x0f,0xe9,0x7c,0x42 }, { 0x02,0xe2,0x72,0x4b },
1550 { 0x15,0xff,0x60,0x50 }, { 0x18,0xf4,0x6e,0x59 },
1551 { 0x3b,0xc5,0x44,0x66 }, { 0x36,0xce,0x4a,0x6f },
1552 { 0x21,0xd3,0x58,0x74 }, { 0x2c,0xd8,0x56,0x7d },
1553 { 0x0c,0x7a,0x37,0xa1 }, { 0x01,0x71,0x39,0xa8 },
1554 { 0x16,0x6c,0x2b,0xb3 }, { 0x1b,0x67,0x25,0xba },
1555 { 0x38,0x56,0x0f,0x85 }, { 0x35,0x5d,0x01,0x8c },
1556 { 0x22,0x40,0x13,0x97 }, { 0x2f,0x4b,0x1d,0x9e },
1557 { 0x64,0x22,0x47,0xe9 }, { 0x69,0x29,0x49,0xe0 },
1558 { 0x7e,0x34,0x5b,0xfb }, { 0x73,0x3f,0x55,0xf2 },
1559 { 0x50,0x0e,0x7f,0xcd }, { 0x5d,0x05,0x71,0xc4 },
1560 { 0x4a,0x18,0x63,0xdf }, { 0x47,0x13,0x6d,0xd6 },
1561 { 0xdc,0xca,0xd7,0x31 }, { 0xd1,0xc1,0xd9,0x38 },
1562 { 0xc6,0xdc,0xcb,0x23 }, { 0xcb,0xd7,0xc5,0x2a },
1563 { 0xe8,0xe6,0xef,0x15 }, { 0xe5,0xed,0xe1,0x1c },
1564 { 0xf2,0xf0,0xf3,0x07 }, { 0xff,0xfb,0xfd,0x0e },
1565 { 0xb4,0x92,0xa7,0x79 }, { 0xb9,0x99,0xa9,0x70 },
1566 { 0xae,0x84,0xbb,0x6b }, { 0xa3,0x8f,0xb5,0x62 },
1567 { 0x80,0xbe,0x9f,0x5d }, { 0x8d,0xb5,0x91,0x54 },
1568 { 0x9a,0xa8,0x83,0x4f }, { 0x97,0xa3,0x8d,0x46 }
1569};
1570
1571static const byte U4[256][4] = {
1572 { 0x00,0x00,0x00,0x00 }, { 0x09,0x0d,0x0b,0x0e },
1573 { 0x12,0x1a,0x16,0x1c }, { 0x1b,0x17,0x1d,0x12 },
1574 { 0x24,0x34,0x2c,0x38 }, { 0x2d,0x39,0x27,0x36 },
1575 { 0x36,0x2e,0x3a,0x24 }, { 0x3f,0x23,0x31,0x2a },
1576 { 0x48,0x68,0x58,0x70 }, { 0x41,0x65,0x53,0x7e },
1577 { 0x5a,0x72,0x4e,0x6c }, { 0x53,0x7f,0x45,0x62 },
1578 { 0x6c,0x5c,0x74,0x48 }, { 0x65,0x51,0x7f,0x46 },
1579 { 0x7e,0x46,0x62,0x54 }, { 0x77,0x4b,0x69,0x5a },
1580 { 0x90,0xd0,0xb0,0xe0 }, { 0x99,0xdd,0xbb,0xee },
1581 { 0x82,0xca,0xa6,0xfc }, { 0x8b,0xc7,0xad,0xf2 },
1582 { 0xb4,0xe4,0x9c,0xd8 }, { 0xbd,0xe9,0x97,0xd6 },
1583 { 0xa6,0xfe,0x8a,0xc4 }, { 0xaf,0xf3,0x81,0xca },
1584 { 0xd8,0xb8,0xe8,0x90 }, { 0xd1,0xb5,0xe3,0x9e },
1585 { 0xca,0xa2,0xfe,0x8c }, { 0xc3,0xaf,0xf5,0x82 },
1586 { 0xfc,0x8c,0xc4,0xa8 }, { 0xf5,0x81,0xcf,0xa6 },
1587 { 0xee,0x96,0xd2,0xb4 }, { 0xe7,0x9b,0xd9,0xba },
1588 { 0x3b,0xbb,0x7b,0xdb }, { 0x32,0xb6,0x70,0xd5 },
1589 { 0x29,0xa1,0x6d,0xc7 }, { 0x20,0xac,0x66,0xc9 },
1590 { 0x1f,0x8f,0x57,0xe3 }, { 0x16,0x82,0x5c,0xed },
1591 { 0x0d,0x95,0x41,0xff }, { 0x04,0x98,0x4a,0xf1 },
1592 { 0x73,0xd3,0x23,0xab }, { 0x7a,0xde,0x28,0xa5 },
1593 { 0x61,0xc9,0x35,0xb7 }, { 0x68,0xc4,0x3e,0xb9 },
1594 { 0x57,0xe7,0x0f,0x93 }, { 0x5e,0xea,0x04,0x9d },
1595 { 0x45,0xfd,0x19,0x8f }, { 0x4c,0xf0,0x12,0x81 },
1596 { 0xab,0x6b,0xcb,0x3b }, { 0xa2,0x66,0xc0,0x35 },
1597 { 0xb9,0x71,0xdd,0x27 }, { 0xb0,0x7c,0xd6,0x29 },
1598 { 0x8f,0x5f,0xe7,0x03 }, { 0x86,0x52,0xec,0x0d },
1599 { 0x9d,0x45,0xf1,0x1f }, { 0x94,0x48,0xfa,0x11 },
1600 { 0xe3,0x03,0x93,0x4b }, { 0xea,0x0e,0x98,0x45 },
1601 { 0xf1,0x19,0x85,0x57 }, { 0xf8,0x14,0x8e,0x59 },
1602 { 0xc7,0x37,0xbf,0x73 }, { 0xce,0x3a,0xb4,0x7d },
1603 { 0xd5,0x2d,0xa9,0x6f }, { 0xdc,0x20,0xa2,0x61 },
1604 { 0x76,0x6d,0xf6,0xad }, { 0x7f,0x60,0xfd,0xa3 },
1605 { 0x64,0x77,0xe0,0xb1 }, { 0x6d,0x7a,0xeb,0xbf },
1606 { 0x52,0x59,0xda,0x95 }, { 0x5b,0x54,0xd1,0x9b },
1607 { 0x40,0x43,0xcc,0x89 }, { 0x49,0x4e,0xc7,0x87 },
1608 { 0x3e,0x05,0xae,0xdd }, { 0x37,0x08,0xa5,0xd3 },
1609 { 0x2c,0x1f,0xb8,0xc1 }, { 0x25,0x12,0xb3,0xcf },
1610 { 0x1a,0x31,0x82,0xe5 }, { 0x13,0x3c,0x89,0xeb },
1611 { 0x08,0x2b,0x94,0xf9 }, { 0x01,0x26,0x9f,0xf7 },
1612 { 0xe6,0xbd,0x46,0x4d }, { 0xef,0xb0,0x4d,0x43 },
1613 { 0xf4,0xa7,0x50,0x51 }, { 0xfd,0xaa,0x5b,0x5f },
1614 { 0xc2,0x89,0x6a,0x75 }, { 0xcb,0x84,0x61,0x7b },
1615 { 0xd0,0x93,0x7c,0x69 }, { 0xd9,0x9e,0x77,0x67 },
1616 { 0xae,0xd5,0x1e,0x3d }, { 0xa7,0xd8,0x15,0x33 },
1617 { 0xbc,0xcf,0x08,0x21 }, { 0xb5,0xc2,0x03,0x2f },
1618 { 0x8a,0xe1,0x32,0x05 }, { 0x83,0xec,0x39,0x0b },
1619 { 0x98,0xfb,0x24,0x19 }, { 0x91,0xf6,0x2f,0x17 },
1620 { 0x4d,0xd6,0x8d,0x76 }, { 0x44,0xdb,0x86,0x78 },
1621 { 0x5f,0xcc,0x9b,0x6a }, { 0x56,0xc1,0x90,0x64 },
1622 { 0x69,0xe2,0xa1,0x4e }, { 0x60,0xef,0xaa,0x40 },
1623 { 0x7b,0xf8,0xb7,0x52 }, { 0x72,0xf5,0xbc,0x5c },
1624 { 0x05,0xbe,0xd5,0x06 }, { 0x0c,0xb3,0xde,0x08 },
1625 { 0x17,0xa4,0xc3,0x1a }, { 0x1e,0xa9,0xc8,0x14 },
1626 { 0x21,0x8a,0xf9,0x3e }, { 0x28,0x87,0xf2,0x30 },
1627 { 0x33,0x90,0xef,0x22 }, { 0x3a,0x9d,0xe4,0x2c },
1628 { 0xdd,0x06,0x3d,0x96 }, { 0xd4,0x0b,0x36,0x98 },
1629 { 0xcf,0x1c,0x2b,0x8a }, { 0xc6,0x11,0x20,0x84 },
1630 { 0xf9,0x32,0x11,0xae }, { 0xf0,0x3f,0x1a,0xa0 },
1631 { 0xeb,0x28,0x07,0xb2 }, { 0xe2,0x25,0x0c,0xbc },
1632 { 0x95,0x6e,0x65,0xe6 }, { 0x9c,0x63,0x6e,0xe8 },
1633 { 0x87,0x74,0x73,0xfa }, { 0x8e,0x79,0x78,0xf4 },
1634 { 0xb1,0x5a,0x49,0xde }, { 0xb8,0x57,0x42,0xd0 },
1635 { 0xa3,0x40,0x5f,0xc2 }, { 0xaa,0x4d,0x54,0xcc },
1636 { 0xec,0xda,0xf7,0x41 }, { 0xe5,0xd7,0xfc,0x4f },
1637 { 0xfe,0xc0,0xe1,0x5d }, { 0xf7,0xcd,0xea,0x53 },
1638 { 0xc8,0xee,0xdb,0x79 }, { 0xc1,0xe3,0xd0,0x77 },
1639 { 0xda,0xf4,0xcd,0x65 }, { 0xd3,0xf9,0xc6,0x6b },
1640 { 0xa4,0xb2,0xaf,0x31 }, { 0xad,0xbf,0xa4,0x3f },
1641 { 0xb6,0xa8,0xb9,0x2d }, { 0xbf,0xa5,0xb2,0x23 },
1642 { 0x80,0x86,0x83,0x09 }, { 0x89,0x8b,0x88,0x07 },
1643 { 0x92,0x9c,0x95,0x15 }, { 0x9b,0x91,0x9e,0x1b },
1644 { 0x7c,0x0a,0x47,0xa1 }, { 0x75,0x07,0x4c,0xaf },
1645 { 0x6e,0x10,0x51,0xbd }, { 0x67,0x1d,0x5a,0xb3 },
1646 { 0x58,0x3e,0x6b,0x99 }, { 0x51,0x33,0x60,0x97 },
1647 { 0x4a,0x24,0x7d,0x85 }, { 0x43,0x29,0x76,0x8b },
1648 { 0x34,0x62,0x1f,0xd1 }, { 0x3d,0x6f,0x14,0xdf },
1649 { 0x26,0x78,0x09,0xcd }, { 0x2f,0x75,0x02,0xc3 },
1650 { 0x10,0x56,0x33,0xe9 }, { 0x19,0x5b,0x38,0xe7 },
1651 { 0x02,0x4c,0x25,0xf5 }, { 0x0b,0x41,0x2e,0xfb },
1652 { 0xd7,0x61,0x8c,0x9a }, { 0xde,0x6c,0x87,0x94 },
1653 { 0xc5,0x7b,0x9a,0x86 }, { 0xcc,0x76,0x91,0x88 },
1654 { 0xf3,0x55,0xa0,0xa2 }, { 0xfa,0x58,0xab,0xac },
1655 { 0xe1,0x4f,0xb6,0xbe }, { 0xe8,0x42,0xbd,0xb0 },
1656 { 0x9f,0x09,0xd4,0xea }, { 0x96,0x04,0xdf,0xe4 },
1657 { 0x8d,0x13,0xc2,0xf6 }, { 0x84,0x1e,0xc9,0xf8 },
1658 { 0xbb,0x3d,0xf8,0xd2 }, { 0xb2,0x30,0xf3,0xdc },
1659 { 0xa9,0x27,0xee,0xce }, { 0xa0,0x2a,0xe5,0xc0 },
1660 { 0x47,0xb1,0x3c,0x7a }, { 0x4e,0xbc,0x37,0x74 },
1661 { 0x55,0xab,0x2a,0x66 }, { 0x5c,0xa6,0x21,0x68 },
1662 { 0x63,0x85,0x10,0x42 }, { 0x6a,0x88,0x1b,0x4c },
1663 { 0x71,0x9f,0x06,0x5e }, { 0x78,0x92,0x0d,0x50 },
1664 { 0x0f,0xd9,0x64,0x0a }, { 0x06,0xd4,0x6f,0x04 },
1665 { 0x1d,0xc3,0x72,0x16 }, { 0x14,0xce,0x79,0x18 },
1666 { 0x2b,0xed,0x48,0x32 }, { 0x22,0xe0,0x43,0x3c },
1667 { 0x39,0xf7,0x5e,0x2e }, { 0x30,0xfa,0x55,0x20 },
1668 { 0x9a,0xb7,0x01,0xec }, { 0x93,0xba,0x0a,0xe2 },
1669 { 0x88,0xad,0x17,0xf0 }, { 0x81,0xa0,0x1c,0xfe },
1670 { 0xbe,0x83,0x2d,0xd4 }, { 0xb7,0x8e,0x26,0xda },
1671 { 0xac,0x99,0x3b,0xc8 }, { 0xa5,0x94,0x30,0xc6 },
1672 { 0xd2,0xdf,0x59,0x9c }, { 0xdb,0xd2,0x52,0x92 },
1673 { 0xc0,0xc5,0x4f,0x80 }, { 0xc9,0xc8,0x44,0x8e },
1674 { 0xf6,0xeb,0x75,0xa4 }, { 0xff,0xe6,0x7e,0xaa },
1675 { 0xe4,0xf1,0x63,0xb8 }, { 0xed,0xfc,0x68,0xb6 },
1676 { 0x0a,0x67,0xb1,0x0c }, { 0x03,0x6a,0xba,0x02 },
1677 { 0x18,0x7d,0xa7,0x10 }, { 0x11,0x70,0xac,0x1e },
1678 { 0x2e,0x53,0x9d,0x34 }, { 0x27,0x5e,0x96,0x3a },
1679 { 0x3c,0x49,0x8b,0x28 }, { 0x35,0x44,0x80,0x26 },
1680 { 0x42,0x0f,0xe9,0x7c }, { 0x4b,0x02,0xe2,0x72 },
1681 { 0x50,0x15,0xff,0x60 }, { 0x59,0x18,0xf4,0x6e },
1682 { 0x66,0x3b,0xc5,0x44 }, { 0x6f,0x36,0xce,0x4a },
1683 { 0x74,0x21,0xd3,0x58 }, { 0x7d,0x2c,0xd8,0x56 },
1684 { 0xa1,0x0c,0x7a,0x37 }, { 0xa8,0x01,0x71,0x39 },
1685 { 0xb3,0x16,0x6c,0x2b }, { 0xba,0x1b,0x67,0x25 },
1686 { 0x85,0x38,0x56,0x0f }, { 0x8c,0x35,0x5d,0x01 },
1687 { 0x97,0x22,0x40,0x13 }, { 0x9e,0x2f,0x4b,0x1d },
1688 { 0xe9,0x64,0x22,0x47 }, { 0xe0,0x69,0x29,0x49 },
1689 { 0xfb,0x7e,0x34,0x5b }, { 0xf2,0x73,0x3f,0x55 },
1690 { 0xcd,0x50,0x0e,0x7f }, { 0xc4,0x5d,0x05,0x71 },
1691 { 0xdf,0x4a,0x18,0x63 }, { 0xd6,0x47,0x13,0x6d },
1692 { 0x31,0xdc,0xca,0xd7 }, { 0x38,0xd1,0xc1,0xd9 },
1693 { 0x23,0xc6,0xdc,0xcb }, { 0x2a,0xcb,0xd7,0xc5 },
1694 { 0x15,0xe8,0xe6,0xef }, { 0x1c,0xe5,0xed,0xe1 },
1695 { 0x07,0xf2,0xf0,0xf3 }, { 0x0e,0xff,0xfb,0xfd },
1696 { 0x79,0xb4,0x92,0xa7 }, { 0x70,0xb9,0x99,0xa9 },
1697 { 0x6b,0xae,0x84,0xbb }, { 0x62,0xa3,0x8f,0xb5 },
1698 { 0x5d,0x80,0xbe,0x9f }, { 0x54,0x8d,0xb5,0x91 },
1699 { 0x4f,0x9a,0xa8,0x83 }, { 0x46,0x97,0xa3,0x8d }
1700};
1701
1702static const u32 rcon[30] = {
1703 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c,
1704 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35,
1705 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91
1706};
1707
1708
1709
1710
1711/* Perform the key setup.
1712 */
1713static gcry_err_code_t
1714do_setkey (RIJNDAEL_context *ctx, const byte *key, const unsigned keylen)
1715{
1716 static int initialized = 0;
1717 static const char *selftest_failed=0;
1718 int ROUNDS;
1719 byte k[MAXKC][4];
1720 int i,j, r, t, rconpointer = 0;
1721 byte tk[MAXKC][4];
1722 int KC;
1723
1724 if (!initialized)
1725 {
1726 initialized = 1;
1727 selftest_failed = selftest ();
1728 if( selftest_failed )
1729 log_error ("%s\n", selftest_failed );
1730 }
1731 if( selftest_failed )
1732 return GPG_ERR_SELFTEST_FAILED;
1733
1734 if( keylen == 128/8 )
1735 {
1736 ROUNDS = 10;
1737 KC = 4;
1738 }
1739 else if ( keylen == 192/8 )
1740 {
1741 ROUNDS = 12;
1742 KC = 6;
1743 }
1744 else if ( keylen == 256/8 )
1745 {
1746 ROUNDS = 14;
1747 KC = 8;
1748 }
1749 else
1750 return GPG_ERR_INV_KEYLEN;
1751
1752 ctx->ROUNDS = ROUNDS;
1753 ctx->decryption_prepared = 0;
1754
1755 for (i = 0; i < keylen; i++)
1756 {
1757 k[i >> 2][i & 3] = key[i];
1758 }
1759#define W (ctx->keySched)
1760
1761 for (j = KC-1; j >= 0; j--)
1762 {
1763 *((u32*)tk[j]) = *((u32*)k[j]);
1764 }
1765 r = 0;
1766 t = 0;
1767 /* copy values into round key array */
1768 for (j = 0; (j < KC) && (r < ROUNDS + 1); )
1769 {
1770 for (; (j < KC) && (t < 4); j++, t++)
1771 {
1772 *((u32*)W[r][t]) = *((u32*)tk[j]);
1773 }
1774 if (t == 4)
1775 {
1776 r++;
1777 t = 0;
1778 }
1779 }
1780
1781 while (r < ROUNDS + 1)
1782 {
1783 /* While not enough round key material calculated */
1784 /* calculate new values. */
1785 tk[0][0] ^= S[tk[KC-1][1]];
1786 tk[0][1] ^= S[tk[KC-1][2]];
1787 tk[0][2] ^= S[tk[KC-1][3]];
1788 tk[0][3] ^= S[tk[KC-1][0]];
1789 tk[0][0] ^= rcon[rconpointer++];
1790
1791 if (KC != 8)
1792 {
1793 for (j = 1; j < KC; j++)
1794 {
1795 *((u32*)tk[j]) ^= *((u32*)tk[j-1]);
1796 }
1797 }
1798 else
1799 {
1800 for (j = 1; j < KC/2; j++)
1801 {
1802 *((u32*)tk[j]) ^= *((u32*)tk[j-1]);
1803 }
1804 tk[KC/2][0] ^= S[tk[KC/2 - 1][0]];
1805 tk[KC/2][1] ^= S[tk[KC/2 - 1][1]];
1806 tk[KC/2][2] ^= S[tk[KC/2 - 1][2]];
1807 tk[KC/2][3] ^= S[tk[KC/2 - 1][3]];
1808 for (j = KC/2 + 1; j < KC; j++)
1809 {
1810 *((u32*)tk[j]) ^= *((u32*)tk[j-1]);
1811 }
1812 }
1813
1814 /* Copy values into round key array. */
1815 for (j = 0; (j < KC) && (r < ROUNDS + 1); )
1816 {
1817 for (; (j < KC) && (t < 4); j++, t++)
1818 {
1819 *((u32*)W[r][t]) = *((u32*)tk[j]);
1820 }
1821 if (t == 4)
1822 {
1823 r++;
1824 t = 0;
1825 }
1826 }
1827 }
1828
1829#undef W
1830 return 0;
1831}
1832
1833static gcry_err_code_t
1834rijndael_setkey (void *context, const byte *key, const unsigned keylen)
1835{
1836 RIJNDAEL_context *ctx = context;
1837
1838 int rc = do_setkey (ctx, key, keylen);
1839 _gcry_burn_stack ( 100 + 16*sizeof(int));
1840 return rc;
1841}
1842
1843
1844/* Make a decryption key from an encryption key. */
1845static void
1846prepare_decryption( RIJNDAEL_context *ctx )
1847{
1848 int r;
1849 byte *w;
1850
1851 for (r=0; r < MAXROUNDS+1; r++ )
1852 {
1853 *((u32*)ctx->keySched2[r][0]) = *((u32*)ctx->keySched[r][0]);
1854 *((u32*)ctx->keySched2[r][1]) = *((u32*)ctx->keySched[r][1]);
1855 *((u32*)ctx->keySched2[r][2]) = *((u32*)ctx->keySched[r][2]);
1856 *((u32*)ctx->keySched2[r][3]) = *((u32*)ctx->keySched[r][3]);
1857 }
1858#define W (ctx->keySched2)
1859 for (r = 1; r < ctx->ROUNDS; r++)
1860 {
1861 w = W[r][0];
1862 *((u32*)w) = *((u32*)U1[w[0]]) ^ *((u32*)U2[w[1]])
1863 ^ *((u32*)U3[w[2]]) ^ *((u32*)U4[w[3]]);
1864
1865 w = W[r][1];
1866 *((u32*)w) = *((u32*)U1[w[0]]) ^ *((u32*)U2[w[1]])
1867 ^ *((u32*)U3[w[2]]) ^ *((u32*)U4[w[3]]);
1868
1869 w = W[r][2];
1870 *((u32*)w) = *((u32*)U1[w[0]]) ^ *((u32*)U2[w[1]])
1871 ^ *((u32*)U3[w[2]]) ^ *((u32*)U4[w[3]]);
1872
1873 w = W[r][3];
1874 *((u32*)w) = *((u32*)U1[w[0]]) ^ *((u32*)U2[w[1]])
1875 ^ *((u32*)U3[w[2]]) ^ *((u32*)U4[w[3]]);
1876 }
1877#undef W
1878 }
1879
1880
1881
1882/* Encrypt one block. A and B may be the same. */
1883static void
1884do_encrypt (const RIJNDAEL_context *ctx, byte *b, const byte *a)
1885{
1886 /* FIXME: Ugly code, replace by straighter implementaion and use
1887 optimized assembler for common CPUs. */
1888
1889 int r;
1890 union {
1891 u32 tempu32[4]; /* Force correct alignment. */
1892 byte temp[4][4];
1893 } u;
1894 int ROUNDS = ctx->ROUNDS;
1895#define rk (ctx->keySched)
1896
1897 *((u32*)u.temp[0]) = *((u32*)(a )) ^ *((u32*)rk[0][0]);
1898 *((u32*)u.temp[1]) = *((u32*)(a+ 4)) ^ *((u32*)rk[0][1]);
1899 *((u32*)u.temp[2]) = *((u32*)(a+ 8)) ^ *((u32*)rk[0][2]);
1900 *((u32*)u.temp[3]) = *((u32*)(a+12)) ^ *((u32*)rk[0][3]);
1901 *((u32*)(b )) = (*((u32*)T1[u.temp[0][0]])
1902 ^ *((u32*)T2[u.temp[1][1]])
1903 ^ *((u32*)T3[u.temp[2][2]])
1904 ^ *((u32*)T4[u.temp[3][3]]));
1905 *((u32*)(b + 4)) = (*((u32*)T1[u.temp[1][0]])
1906 ^ *((u32*)T2[u.temp[2][1]])
1907 ^ *((u32*)T3[u.temp[3][2]])
1908 ^ *((u32*)T4[u.temp[0][3]]));
1909 *((u32*)(b + 8)) = (*((u32*)T1[u.temp[2][0]])
1910 ^ *((u32*)T2[u.temp[3][1]])
1911 ^ *((u32*)T3[u.temp[0][2]])
1912 ^ *((u32*)T4[u.temp[1][3]]));
1913 *((u32*)(b +12)) = (*((u32*)T1[u.temp[3][0]])
1914 ^ *((u32*)T2[u.temp[0][1]])
1915 ^ *((u32*)T3[u.temp[1][2]])
1916 ^ *((u32*)T4[u.temp[2][3]]));
1917
1918 for (r = 1; r < ROUNDS-1; r++)
1919 {
1920 *((u32*)u.temp[0]) = *((u32*)(b )) ^ *((u32*)rk[r][0]);
1921 *((u32*)u.temp[1]) = *((u32*)(b+ 4)) ^ *((u32*)rk[r][1]);
1922 *((u32*)u.temp[2]) = *((u32*)(b+ 8)) ^ *((u32*)rk[r][2]);
1923 *((u32*)u.temp[3]) = *((u32*)(b+12)) ^ *((u32*)rk[r][3]);
1924
1925 *((u32*)(b )) = (*((u32*)T1[u.temp[0][0]])
1926 ^ *((u32*)T2[u.temp[1][1]])
1927 ^ *((u32*)T3[u.temp[2][2]])
1928 ^ *((u32*)T4[u.temp[3][3]]));
1929 *((u32*)(b + 4)) = (*((u32*)T1[u.temp[1][0]])
1930 ^ *((u32*)T2[u.temp[2][1]])
1931 ^ *((u32*)T3[u.temp[3][2]])
1932 ^ *((u32*)T4[u.temp[0][3]]));
1933 *((u32*)(b + 8)) = (*((u32*)T1[u.temp[2][0]])
1934 ^ *((u32*)T2[u.temp[3][1]])
1935 ^ *((u32*)T3[u.temp[0][2]])
1936 ^ *((u32*)T4[u.temp[1][3]]));
1937 *((u32*)(b +12)) = (*((u32*)T1[u.temp[3][0]])
1938 ^ *((u32*)T2[u.temp[0][1]])
1939 ^ *((u32*)T3[u.temp[1][2]])
1940 ^ *((u32*)T4[u.temp[2][3]]));
1941 }
1942
1943 /* Last round is special. */
1944 *((u32*)u.temp[0]) = *((u32*)(b )) ^ *((u32*)rk[ROUNDS-1][0]);
1945 *((u32*)u.temp[1]) = *((u32*)(b+ 4)) ^ *((u32*)rk[ROUNDS-1][1]);
1946 *((u32*)u.temp[2]) = *((u32*)(b+ 8)) ^ *((u32*)rk[ROUNDS-1][2]);
1947 *((u32*)u.temp[3]) = *((u32*)(b+12)) ^ *((u32*)rk[ROUNDS-1][3]);
1948 b[ 0] = T1[u.temp[0][0]][1];
1949 b[ 1] = T1[u.temp[1][1]][1];
1950 b[ 2] = T1[u.temp[2][2]][1];
1951 b[ 3] = T1[u.temp[3][3]][1];
1952 b[ 4] = T1[u.temp[1][0]][1];
1953 b[ 5] = T1[u.temp[2][1]][1];
1954 b[ 6] = T1[u.temp[3][2]][1];
1955 b[ 7] = T1[u.temp[0][3]][1];
1956 b[ 8] = T1[u.temp[2][0]][1];
1957 b[ 9] = T1[u.temp[3][1]][1];
1958 b[10] = T1[u.temp[0][2]][1];
1959 b[11] = T1[u.temp[1][3]][1];
1960 b[12] = T1[u.temp[3][0]][1];
1961 b[13] = T1[u.temp[0][1]][1];
1962 b[14] = T1[u.temp[1][2]][1];
1963 b[15] = T1[u.temp[2][3]][1];
1964 *((u32*)(b )) ^= *((u32*)rk[ROUNDS][0]);
1965 *((u32*)(b+ 4)) ^= *((u32*)rk[ROUNDS][1]);
1966 *((u32*)(b+ 8)) ^= *((u32*)rk[ROUNDS][2]);
1967 *((u32*)(b+12)) ^= *((u32*)rk[ROUNDS][3]);
1968#undef rk
1969}
1970
1971static void
1972rijndael_encrypt (void *context, byte *b, const byte *a)
1973{
1974 RIJNDAEL_context *ctx = context;
1975
1976 do_encrypt (ctx, b, a);
1977 _gcry_burn_stack (16 + 2*sizeof(int));
1978}
1979
1980
1981
1982/* Decrypt one block. a and b may be the same. */
1983static void
1984do_decrypt (RIJNDAEL_context *ctx, byte *b, const byte *a)
1985{
1986#define rk (ctx->keySched2)
1987 int ROUNDS = ctx->ROUNDS;
1988 int r;
1989 union {
1990 u32 tempu32[4]; /* Force correct alignment. */
1991 byte temp[4][4];
1992 } u;
1993
1994 if ( !ctx->decryption_prepared )
1995 {
1996 prepare_decryption ( ctx );
1997 _gcry_burn_stack (64);
1998 ctx->decryption_prepared = 1;
1999 }
2000
2001 *((u32*)u.temp[0]) = *((u32*)(a )) ^ *((u32*)rk[ROUNDS][0]);
2002 *((u32*)u.temp[1]) = *((u32*)(a+ 4)) ^ *((u32*)rk[ROUNDS][1]);
2003 *((u32*)u.temp[2]) = *((u32*)(a+ 8)) ^ *((u32*)rk[ROUNDS][2]);
2004 *((u32*)u.temp[3]) = *((u32*)(a+12)) ^ *((u32*)rk[ROUNDS][3]);
2005
2006 *((u32*)(b )) = (*((u32*)T5[u.temp[0][0]])
2007 ^ *((u32*)T6[u.temp[3][1]])
2008 ^ *((u32*)T7[u.temp[2][2]])
2009 ^ *((u32*)T8[u.temp[1][3]]));
2010 *((u32*)(b+ 4)) = (*((u32*)T5[u.temp[1][0]])
2011 ^ *((u32*)T6[u.temp[0][1]])
2012 ^ *((u32*)T7[u.temp[3][2]])
2013 ^ *((u32*)T8[u.temp[2][3]]));
2014 *((u32*)(b+ 8)) = (*((u32*)T5[u.temp[2][0]])
2015 ^ *((u32*)T6[u.temp[1][1]])
2016 ^ *((u32*)T7[u.temp[0][2]])
2017 ^ *((u32*)T8[u.temp[3][3]]));
2018 *((u32*)(b+12)) = (*((u32*)T5[u.temp[3][0]])
2019 ^ *((u32*)T6[u.temp[2][1]])
2020 ^ *((u32*)T7[u.temp[1][2]])
2021 ^ *((u32*)T8[u.temp[0][3]]));
2022
2023 for (r = ROUNDS-1; r > 1; r--)
2024 {
2025 *((u32*)u.temp[0]) = *((u32*)(b )) ^ *((u32*)rk[r][0]);
2026 *((u32*)u.temp[1]) = *((u32*)(b+ 4)) ^ *((u32*)rk[r][1]);
2027 *((u32*)u.temp[2]) = *((u32*)(b+ 8)) ^ *((u32*)rk[r][2]);
2028 *((u32*)u.temp[3]) = *((u32*)(b+12)) ^ *((u32*)rk[r][3]);
2029 *((u32*)(b )) = (*((u32*)T5[u.temp[0][0]])
2030 ^ *((u32*)T6[u.temp[3][1]])
2031 ^ *((u32*)T7[u.temp[2][2]])
2032 ^ *((u32*)T8[u.temp[1][3]]));
2033 *((u32*)(b+ 4)) = (*((u32*)T5[u.temp[1][0]])
2034 ^ *((u32*)T6[u.temp[0][1]])
2035 ^ *((u32*)T7[u.temp[3][2]])
2036 ^ *((u32*)T8[u.temp[2][3]]));
2037 *((u32*)(b+ 8)) = (*((u32*)T5[u.temp[2][0]])
2038 ^ *((u32*)T6[u.temp[1][1]])
2039 ^ *((u32*)T7[u.temp[0][2]])
2040 ^ *((u32*)T8[u.temp[3][3]]));
2041 *((u32*)(b+12)) = (*((u32*)T5[u.temp[3][0]])
2042 ^ *((u32*)T6[u.temp[2][1]])
2043 ^ *((u32*)T7[u.temp[1][2]])
2044 ^ *((u32*)T8[u.temp[0][3]]));
2045 }
2046
2047 /* Last round is special. */
2048 *((u32*)u.temp[0]) = *((u32*)(b )) ^ *((u32*)rk[1][0]);
2049 *((u32*)u.temp[1]) = *((u32*)(b+ 4)) ^ *((u32*)rk[1][1]);
2050 *((u32*)u.temp[2]) = *((u32*)(b+ 8)) ^ *((u32*)rk[1][2]);
2051 *((u32*)u.temp[3]) = *((u32*)(b+12)) ^ *((u32*)rk[1][3]);
2052 b[ 0] = S5[u.temp[0][0]];
2053 b[ 1] = S5[u.temp[3][1]];
2054 b[ 2] = S5[u.temp[2][2]];
2055 b[ 3] = S5[u.temp[1][3]];
2056 b[ 4] = S5[u.temp[1][0]];
2057 b[ 5] = S5[u.temp[0][1]];
2058 b[ 6] = S5[u.temp[3][2]];
2059 b[ 7] = S5[u.temp[2][3]];
2060 b[ 8] = S5[u.temp[2][0]];
2061 b[ 9] = S5[u.temp[1][1]];
2062 b[10] = S5[u.temp[0][2]];
2063 b[11] = S5[u.temp[3][3]];
2064 b[12] = S5[u.temp[3][0]];
2065 b[13] = S5[u.temp[2][1]];
2066 b[14] = S5[u.temp[1][2]];
2067 b[15] = S5[u.temp[0][3]];
2068 *((u32*)(b )) ^= *((u32*)rk[0][0]);
2069 *((u32*)(b+ 4)) ^= *((u32*)rk[0][1]);
2070 *((u32*)(b+ 8)) ^= *((u32*)rk[0][2]);
2071 *((u32*)(b+12)) ^= *((u32*)rk[0][3]);
2072#undef rk
2073}
2074
2075static void
2076rijndael_decrypt (void *context, byte *b, const byte *a)
2077{
2078 RIJNDAEL_context *ctx = context;
2079
2080 do_decrypt (ctx, b, a);
2081 _gcry_burn_stack (16+2*sizeof(int));
2082}
2083
2084
2085/* Test a single encryption and decryption with each key size. */
2086static const char*
2087selftest (void)
2088{
2089 RIJNDAEL_context ctx;
2090 byte scratch[16];
2091
2092 /* The test vectors are from the AES supplied ones; more or less
2093 * randomly taken from ecb_tbl.txt (I=42,81,14)
2094 */
2095 static byte plaintext[16] = {
2096 0x01,0x4B,0xAF,0x22,0x78,0xA6,0x9D,0x33,
2097 0x1D,0x51,0x80,0x10,0x36,0x43,0xE9,0x9A
2098 };
2099 static byte key[16] = {
2100 0xE8,0xE9,0xEA,0xEB,0xED,0xEE,0xEF,0xF0,
2101 0xF2,0xF3,0xF4,0xF5,0xF7,0xF8,0xF9,0xFA
2102 };
2103 static const byte ciphertext[16] = {
2104 0x67,0x43,0xC3,0xD1,0x51,0x9A,0xB4,0xF2,
2105 0xCD,0x9A,0x78,0xAB,0x09,0xA5,0x11,0xBD
2106 };
2107
2108 static byte plaintext_192[16] = {
2109 0x76,0x77,0x74,0x75,0xF1,0xF2,0xF3,0xF4,
2110 0xF8,0xF9,0xE6,0xE7,0x77,0x70,0x71,0x72
2111 };
2112 static byte key_192[24] = {
2113 0x04,0x05,0x06,0x07,0x09,0x0A,0x0B,0x0C,
2114 0x0E,0x0F,0x10,0x11,0x13,0x14,0x15,0x16,
2115 0x18,0x19,0x1A,0x1B,0x1D,0x1E,0x1F,0x20
2116 };
2117 static const byte ciphertext_192[16] = {
2118 0x5D,0x1E,0xF2,0x0D,0xCE,0xD6,0xBC,0xBC,
2119 0x12,0x13,0x1A,0xC7,0xC5,0x47,0x88,0xAA
2120 };
2121
2122 static byte plaintext_256[16] = {
2123 0x06,0x9A,0x00,0x7F,0xC7,0x6A,0x45,0x9F,
2124 0x98,0xBA,0xF9,0x17,0xFE,0xDF,0x95,0x21
2125 };
2126 static byte key_256[32] = {
2127 0x08,0x09,0x0A,0x0B,0x0D,0x0E,0x0F,0x10,
2128 0x12,0x13,0x14,0x15,0x17,0x18,0x19,0x1A,
2129 0x1C,0x1D,0x1E,0x1F,0x21,0x22,0x23,0x24,
2130 0x26,0x27,0x28,0x29,0x2B,0x2C,0x2D,0x2E
2131 };
2132 static const byte ciphertext_256[16] = {
2133 0x08,0x0E,0x95,0x17,0xEB,0x16,0x77,0x71,
2134 0x9A,0xCF,0x72,0x80,0x86,0x04,0x0A,0xE3
2135 };
2136
2137 rijndael_setkey (&ctx, key, sizeof(key));
2138 rijndael_encrypt (&ctx, scratch, plaintext);
2139 if (memcmp (scratch, ciphertext, sizeof (ciphertext)))
2140 return "Rijndael-128 test encryption failed.";
2141 rijndael_decrypt (&ctx, scratch, scratch);
2142 if (memcmp (scratch, plaintext, sizeof (plaintext)))
2143 return "Rijndael-128 test decryption failed.";
2144
2145 rijndael_setkey (&ctx, key_192, sizeof(key_192));
2146 rijndael_encrypt (&ctx, scratch, plaintext_192);
2147 if (memcmp (scratch, ciphertext_192, sizeof (ciphertext_192)))
2148 return "Rijndael-192 test encryption failed.";
2149 rijndael_decrypt (&ctx, scratch, scratch);
2150 if (memcmp (scratch, plaintext_192, sizeof (plaintext_192)))
2151 return "Rijndael-192 test decryption failed.";
2152
2153 rijndael_setkey (&ctx, key_256, sizeof(key_256));
2154 rijndael_encrypt (&ctx, scratch, plaintext_256);
2155 if (memcmp (scratch, ciphertext_256, sizeof (ciphertext_256)))
2156 return "Rijndael-256 test encryption failed.";
2157 rijndael_decrypt (&ctx, scratch, scratch);
2158 if (memcmp (scratch, plaintext_256, sizeof (plaintext_256)))
2159 return "Rijndael-256 test decryption failed.";
2160
2161 return NULL;
2162}
2163
2164
2165
2166static const char *rijndael_names[] =
2167 {
2168 "RIJNDAEL",
2169 NULL,
2170 };
2171
2172static gcry_cipher_oid_spec_t rijndael_oids[] =
2173 {
2174 { "2.16.840.1.101.3.4.1.1", GCRY_CIPHER_MODE_ECB },
2175 { "2.16.840.1.101.3.4.1.2", GCRY_CIPHER_MODE_CBC },
2176 { "2.16.840.1.101.3.4.1.3", GCRY_CIPHER_MODE_OFB },
2177 { "2.16.840.1.101.3.4.1.4", GCRY_CIPHER_MODE_CFB },
2178 { NULL }
2179 };
2180
2181gcry_cipher_spec_t _gcry_cipher_spec_aes =
2182 {
2183 "AES", rijndael_names, rijndael_oids, 16, 128, sizeof (RIJNDAEL_context),
2184 rijndael_setkey, rijndael_encrypt, rijndael_decrypt,
2185 };
2186
2187static const char *rijndael192_names[] =
2188 {
2189 "RIJNDAEL192",
2190 NULL,
2191 };
2192
2193static gcry_cipher_oid_spec_t rijndael192_oids[] =
2194 {
2195 { "2.16.840.1.101.3.4.1.21", GCRY_CIPHER_MODE_ECB },
2196 { "2.16.840.1.101.3.4.1.22", GCRY_CIPHER_MODE_CBC },
2197 { "2.16.840.1.101.3.4.1.23", GCRY_CIPHER_MODE_OFB },
2198 { "2.16.840.1.101.3.4.1.24", GCRY_CIPHER_MODE_CFB },
2199 { NULL }
2200 };
2201
2202gcry_cipher_spec_t _gcry_cipher_spec_aes192 =
2203 {
2204 "AES192", rijndael192_names, rijndael192_oids, 16, 192, sizeof (RIJNDAEL_context),
2205 rijndael_setkey, rijndael_encrypt, rijndael_decrypt,
2206 };
2207
2208static const char *rijndael256_names[] =
2209 {
2210 "RIJNDAEL256",
2211 NULL,
2212 };
2213
2214static gcry_cipher_oid_spec_t rijndael256_oids[] =
2215 {
2216 { "2.16.840.1.101.3.4.1.41", GCRY_CIPHER_MODE_ECB },
2217 { "2.16.840.1.101.3.4.1.42", GCRY_CIPHER_MODE_CBC },
2218 { "2.16.840.1.101.3.4.1.43", GCRY_CIPHER_MODE_OFB },
2219 { "2.16.840.1.101.3.4.1.44", GCRY_CIPHER_MODE_CFB },
2220 { NULL }
2221 };
2222
2223gcry_cipher_spec_t _gcry_cipher_spec_aes256 =
2224 {
2225 "AES256", rijndael256_names, rijndael256_oids, 16, 256,
2226 sizeof (RIJNDAEL_context),
2227 rijndael_setkey, rijndael_encrypt, rijndael_decrypt,
2228 };
diff --git a/pwmanager/libcrypt/cipher/rmd.h b/pwmanager/libcrypt/cipher/rmd.h
new file mode 100644
index 0000000..6c44017
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/rmd.h
@@ -0,0 +1,36 @@
1/* rmd.h - RIPE-MD hash functions
2 *Copyright (C) 1998, 2001, 2002 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20#ifndef G10_RMD_H
21#define G10_RMD_H
22
23
24/* we need this here because random.c must have direct access */
25typedef struct {
26 u32 h0,h1,h2,h3,h4;
27 u32 nblocks;
28 byte buf[64];
29 int count;
30} RMD160_CONTEXT;
31
32void _gcry_rmd160_init( void *context );
33void _gcry_rmd160_mixblock( RMD160_CONTEXT *hd, char *buffer );
34
35#endif /*G10_RMD_H*/
36
diff --git a/pwmanager/libcrypt/cipher/rmd160.c b/pwmanager/libcrypt/cipher/rmd160.c
new file mode 100644
index 0000000..f450f2b
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/rmd160.c
@@ -0,0 +1,569 @@
1 /* rmd160.c -RIPE-MD160
2 * Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <config.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <assert.h>
26#include "g10lib.h"
27#include "memory.h"
28#include "rmd.h"
29#include "cipher.h" /* only used for the rmd160_hash_buffer() prototype */
30
31#include "bithelp.h"
32
33/*********************************
34 * RIPEMD-160 is not patented, see (as of 25.10.97)
35 * http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html
36 * Note that the code uses Little Endian byteorder, which is good for
37 * 386 etc, but we must add some conversion when used on a big endian box.
38 *
39 *
40 * Pseudo-code for RIPEMD-160
41 *
42 * RIPEMD-160 is an iterative hash function that operates on 32-bit words.
43 * The round function takes as input a 5-word chaining variable and a 16-word
44 * message block and maps this to a new chaining variable. All operations are
45 * defined on 32-bit words. Padding is identical to that of MD4.
46 *
47 *
48 * RIPEMD-160: definitions
49 *
50 *
51 * nonlinear functions at bit level: exor, mux, -, mux, -
52 *
53 * f(j, x, y, z) = x XOR y XOR z (0 <= j <= 15)
54 * f(j, x, y, z) = (x AND y) OR (NOT(x) AND z) (16 <= j <= 31)
55 * f(j, x, y, z) = (x OR NOT(y)) XOR z (32 <= j <= 47)
56 * f(j, x, y, z) = (x AND z) OR (y AND NOT(z)) (48 <= j <= 63)
57 * f(j, x, y, z) = x XOR (y OR NOT(z)) (64 <= j <= 79)
58 *
59 *
60 * added constants (hexadecimal)
61 *
62 * K(j) = 0x00000000 (0 <= j <= 15)
63 * K(j) = 0x5A827999 (16 <= j <= 31)int(2**30 x sqrt(2))
64 * K(j) = 0x6ED9EBA1 (32 <= j <= 47)int(2**30 x sqrt(3))
65 * K(j) = 0x8F1BBCDC (48 <= j <= 63)int(2**30 x sqrt(5))
66 * K(j) = 0xA953FD4E (64 <= j <= 79)int(2**30 x sqrt(7))
67 * K'(j) = 0x50A28BE6 (0 <= j <= 15) int(2**30 x cbrt(2))
68 * K'(j) = 0x5C4DD124 (16 <= j <= 31) int(2**30 x cbrt(3))
69 * K'(j) = 0x6D703EF3 (32 <= j <= 47) int(2**30 x cbrt(5))
70 * K'(j) = 0x7A6D76E9 (48 <= j <= 63) int(2**30 x cbrt(7))
71 * K'(j) = 0x00000000 (64 <= j <= 79)
72 *
73 *
74 * selection of message word
75 *
76 * r(j) = j (0 <= j <= 15)
77 * r(16..31) = 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8
78 * r(32..47) = 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12
79 * r(48..63) = 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2
80 * r(64..79) = 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
81 * r0(0..15) = 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12
82 * r0(16..31)= 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2
83 * r0(32..47)= 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13
84 * r0(48..63)= 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14
85 * r0(64..79)= 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
86 *
87 *
88 * amount for rotate left (rol)
89 *
90 * s(0..15) = 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8
91 * s(16..31) = 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12
92 * s(32..47) = 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5
93 * s(48..63) = 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12
94 * s(64..79) = 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
95 * s'(0..15) = 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6
96 * s'(16..31)= 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11
97 * s'(32..47)= 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5
98 * s'(48..63)= 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8
99 * s'(64..79)= 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
100 *
101 *
102 * initial value (hexadecimal)
103 *
104 * h0 = 0x67452301; h1 = 0xEFCDAB89; h2 = 0x98BADCFE; h3 = 0x10325476;
105 * h4 = 0xC3D2E1F0;
106 *
107 *
108 * RIPEMD-160: pseudo-code
109 *
110 * It is assumed that the message after padding consists of t 16-word blocks
111 * that will be denoted with X[i][j], with 0 <= i <= t-1 and 0 <= j <= 15.
112 * The symbol [+] denotes addition modulo 2**32 and rol_s denotes cyclic left
113 * shift (rotate) over s positions.
114 *
115 *
116 * for i := 0 to t-1 {
117 * A := h0; B := h1; C := h2; D = h3; E = h4;
118 * A' := h0; B' := h1; C' := h2; D' = h3; E' = h4;
119 * for j := 0 to 79 {
120 * T := rol_s(j)(A [+] f(j, B, C, D) [+] X[i][r(j)] [+] K(j)) [+] E;
121 * A := E; E := D; D := rol_10(C); C := B; B := T;
122 * T := rol_s'(j)(A' [+] f(79-j, B', C', D') [+] X[i][r'(j)]
123 [+] K'(j)) [+] E';
124 * A' := E'; E' := D'; D' := rol_10(C'); C' := B'; B' := T;
125 * }
126 * T := h1 [+] C [+] D'; h1 := h2 [+] D [+] E'; h2 := h3 [+] E [+] A';
127 * h3 := h4 [+] A [+] B'; h4 := h0 [+] B [+] C'; h0 := T;
128 * }
129 */
130
131/* Some examples:
132 * "" 9c1185a5c5e9fc54612808977ee8f548b2258d31
133 * "a" 0bdc9d2d256b3ee9daae347be6f4dc835a467ffe
134 * "abc" 8eb208f7e05d987a9b044a8e98c6b087f15a0bfc
135 * "message digest" 5d0689ef49d2fae572b881b123a85ffa21595f36
136 * "a...z" f71c27109c692c1b56bbdceb5b9d2865b3708dbc
137 * "abcdbcde...nopq" 12a053384a9c0c88e405a06c27dcf49ada62eb2b
138 * "A...Za...z0...9" b0e20b6e3116640286ed3a87a5713079b21f5189
139 * 8 times "1234567890" 9b752e45573d4b39f4dbd3323cab82bf63326bfb
140 * 1 million times "a" 52783243c1697bdbe16d37f97f68f08325dc1528
141 */
142
143
144void
145_gcry_rmd160_init (void *context)
146{
147 RMD160_CONTEXT *hd = context;
148
149 hd->h0 = 0x67452301;
150 hd->h1 = 0xEFCDAB89;
151 hd->h2 = 0x98BADCFE;
152 hd->h3 = 0x10325476;
153 hd->h4 = 0xC3D2E1F0;
154 hd->nblocks = 0;
155 hd->count = 0;
156}
157
158
159
160/****************
161 * Transform the message X which consists of 16 32-bit-words
162 */
163static void
164transform( RMD160_CONTEXT *hd, byte *data )
165{
166 register u32 a,b,c,d,e;
167 u32 aa,bb,cc,dd,ee,t;
168#ifdef WORDS_BIGENDIAN
169 u32 x[16];
170 {
171 int i;
172 byte *p2, *p1;
173 for (i=0, p1=data, p2=(byte*)x; i < 16; i++, p2 += 4 )
174 {
175 p2[3] = *p1++;
176 p2[2] = *p1++;
177 p2[1] = *p1++;
178 p2[0] = *p1++;
179 }
180 }
181#else
182 /* This version is better because it is always aligned;
183 * The performance penalty on a 586-100 is about 6% which
184 * is acceptable - because the data is more local it might
185 * also be possible that this is faster on some machines.
186 * This function (when compiled with -02 on gcc 2.7.2)
187 * executes on a 586-100 (39.73 bogomips) at about 1900kb/sec;
188 * [measured with a 4MB data and "gpgm --print-md rmd160"] */
189 u32 x[16];
190 memcpy( x, data, 64 );
191#endif
192
193
194#define K0 0x00000000
195#define K1 0x5A827999
196#define K2 0x6ED9EBA1
197#define K3 0x8F1BBCDC
198#define K4 0xA953FD4E
199#define KK0 0x50A28BE6
200#define KK1 0x5C4DD124
201#define KK2 0x6D703EF3
202#define KK3 0x7A6D76E9
203#define KK4 0x00000000
204#define F0(x,y,z) ( (x) ^ (y) ^ (z) )
205#define F1(x,y,z) ( ((x) & (y)) | (~(x) & (z)) )
206#define F2(x,y,z) ( ((x) | ~(y)) ^ (z) )
207#define F3(x,y,z) ( ((x) & (z)) | ((y) & ~(z)) )
208#define F4(x,y,z) ( (x) ^ ((y) | ~(z)) )
209#define R(a,b,c,d,e,f,k,r,s) do { t = a + f(b,c,d) + k + x[r]; \
210 a = rol(t,s) + e; \
211 c = rol(c,10); \
212 } while(0)
213
214 /* left lane */
215 a = hd->h0;
216 b = hd->h1;
217 c = hd->h2;
218 d = hd->h3;
219 e = hd->h4;
220 R( a, b, c, d, e, F0, K0, 0, 11 );
221 R( e, a, b, c, d, F0, K0, 1, 14 );
222 R( d, e, a, b, c, F0, K0, 2, 15 );
223 R( c, d, e, a, b, F0, K0, 3, 12 );
224 R( b, c, d, e, a, F0, K0, 4, 5 );
225 R( a, b, c, d, e, F0, K0, 5, 8 );
226 R( e, a, b, c, d, F0, K0, 6, 7 );
227 R( d, e, a, b, c, F0, K0, 7, 9 );
228 R( c, d, e, a, b, F0, K0, 8, 11 );
229 R( b, c, d, e, a, F0, K0, 9, 13 );
230 R( a, b, c, d, e, F0, K0, 10, 14 );
231 R( e, a, b, c, d, F0, K0, 11, 15 );
232 R( d, e, a, b, c, F0, K0, 12, 6 );
233 R( c, d, e, a, b, F0, K0, 13, 7 );
234 R( b, c, d, e, a, F0, K0, 14, 9 );
235 R( a, b, c, d, e, F0, K0, 15, 8 );
236 R( e, a, b, c, d, F1, K1, 7, 7 );
237 R( d, e, a, b, c, F1, K1, 4, 6 );
238 R( c, d, e, a, b, F1, K1, 13, 8 );
239 R( b, c, d, e, a, F1, K1, 1, 13 );
240 R( a, b, c, d, e, F1, K1, 10, 11 );
241 R( e, a, b, c, d, F1, K1, 6, 9 );
242 R( d, e, a, b, c, F1, K1, 15, 7 );
243 R( c, d, e, a, b, F1, K1, 3, 15 );
244 R( b, c, d, e, a, F1, K1, 12, 7 );
245 R( a, b, c, d, e, F1, K1, 0, 12 );
246 R( e, a, b, c, d, F1, K1, 9, 15 );
247 R( d, e, a, b, c, F1, K1, 5, 9 );
248 R( c, d, e, a, b, F1, K1, 2, 11 );
249 R( b, c, d, e, a, F1, K1, 14, 7 );
250 R( a, b, c, d, e, F1, K1, 11, 13 );
251 R( e, a, b, c, d, F1, K1, 8, 12 );
252 R( d, e, a, b, c, F2, K2, 3, 11 );
253 R( c, d, e, a, b, F2, K2, 10, 13 );
254 R( b, c, d, e, a, F2, K2, 14, 6 );
255 R( a, b, c, d, e, F2, K2, 4, 7 );
256 R( e, a, b, c, d, F2, K2, 9, 14 );
257 R( d, e, a, b, c, F2, K2, 15, 9 );
258 R( c, d, e, a, b, F2, K2, 8, 13 );
259 R( b, c, d, e, a, F2, K2, 1, 15 );
260 R( a, b, c, d, e, F2, K2, 2, 14 );
261 R( e, a, b, c, d, F2, K2, 7, 8 );
262 R( d, e, a, b, c, F2, K2, 0, 13 );
263 R( c, d, e, a, b, F2, K2, 6, 6 );
264 R( b, c, d, e, a, F2, K2, 13, 5 );
265 R( a, b, c, d, e, F2, K2, 11, 12 );
266 R( e, a, b, c, d, F2, K2, 5, 7 );
267 R( d, e, a, b, c, F2, K2, 12, 5 );
268 R( c, d, e, a, b, F3, K3, 1, 11 );
269 R( b, c, d, e, a, F3, K3, 9, 12 );
270 R( a, b, c, d, e, F3, K3, 11, 14 );
271 R( e, a, b, c, d, F3, K3, 10, 15 );
272 R( d, e, a, b, c, F3, K3, 0, 14 );
273 R( c, d, e, a, b, F3, K3, 8, 15 );
274 R( b, c, d, e, a, F3, K3, 12, 9 );
275 R( a, b, c, d, e, F3, K3, 4, 8 );
276 R( e, a, b, c, d, F3, K3, 13, 9 );
277 R( d, e, a, b, c, F3, K3, 3, 14 );
278 R( c, d, e, a, b, F3, K3, 7, 5 );
279 R( b, c, d, e, a, F3, K3, 15, 6 );
280 R( a, b, c, d, e, F3, K3, 14, 8 );
281 R( e, a, b, c, d, F3, K3, 5, 6 );
282 R( d, e, a, b, c, F3, K3, 6, 5 );
283 R( c, d, e, a, b, F3, K3, 2, 12 );
284 R( b, c, d, e, a, F4, K4, 4, 9 );
285 R( a, b, c, d, e, F4, K4, 0, 15 );
286 R( e, a, b, c, d, F4, K4, 5, 5 );
287 R( d, e, a, b, c, F4, K4, 9, 11 );
288 R( c, d, e, a, b, F4, K4, 7, 6 );
289 R( b, c, d, e, a, F4, K4, 12, 8 );
290 R( a, b, c, d, e, F4, K4, 2, 13 );
291 R( e, a, b, c, d, F4, K4, 10, 12 );
292 R( d, e, a, b, c, F4, K4, 14, 5 );
293 R( c, d, e, a, b, F4, K4, 1, 12 );
294 R( b, c, d, e, a, F4, K4, 3, 13 );
295 R( a, b, c, d, e, F4, K4, 8, 14 );
296 R( e, a, b, c, d, F4, K4, 11, 11 );
297 R( d, e, a, b, c, F4, K4, 6, 8 );
298 R( c, d, e, a, b, F4, K4, 15, 5 );
299 R( b, c, d, e, a, F4, K4, 13, 6 );
300
301 aa = a; bb = b; cc = c; dd = d; ee = e;
302
303 /* right lane */
304 a = hd->h0;
305 b = hd->h1;
306 c = hd->h2;
307 d = hd->h3;
308 e = hd->h4;
309 R( a, b, c, d, e, F4, KK0,5, 8);
310 R( e, a, b, c, d, F4, KK0, 14, 9);
311 R( d, e, a, b, c, F4, KK0,7, 9);
312 R( c, d, e, a, b, F4, KK0,0, 11);
313 R( b, c, d, e, a, F4, KK0,9, 13);
314 R( a, b, c, d, e, F4, KK0,2, 15);
315 R( e, a, b, c, d, F4, KK0, 11, 15);
316 R( d, e, a, b, c, F4, KK0,4, 5);
317 R( c, d, e, a, b, F4, KK0, 13, 7);
318 R( b, c, d, e, a, F4, KK0,6, 7);
319 R( a, b, c, d, e, F4, KK0, 15, 8);
320 R( e, a, b, c, d, F4, KK0,8, 11);
321 R( d, e, a, b, c, F4, KK0,1, 14);
322 R( c, d, e, a, b, F4, KK0, 10, 14);
323 R( b, c, d, e, a, F4, KK0,3, 12);
324 R( a, b, c, d, e, F4, KK0, 12, 6);
325 R( e, a, b, c, d, F3, KK1,6, 9);
326 R( d, e, a, b, c, F3, KK1, 11, 13);
327 R( c, d, e, a, b, F3, KK1,3, 15);
328 R( b, c, d, e, a, F3, KK1,7, 7);
329 R( a, b, c, d, e, F3, KK1,0, 12);
330 R( e, a, b, c, d, F3, KK1, 13, 8);
331 R( d, e, a, b, c, F3, KK1,5, 9);
332 R( c, d, e, a, b, F3, KK1, 10, 11);
333 R( b, c, d, e, a, F3, KK1, 14, 7);
334 R( a, b, c, d, e, F3, KK1, 15, 7);
335 R( e, a, b, c, d, F3, KK1,8, 12);
336 R( d, e, a, b, c, F3, KK1, 12, 7);
337 R( c, d, e, a, b, F3, KK1,4, 6);
338 R( b, c, d, e, a, F3, KK1,9, 15);
339 R( a, b, c, d, e, F3, KK1,1, 13);
340 R( e, a, b, c, d, F3, KK1,2, 11);
341 R( d, e, a, b, c, F2, KK2, 15, 9);
342 R( c, d, e, a, b, F2, KK2,5, 7);
343 R( b, c, d, e, a, F2, KK2,1, 15);
344 R( a, b, c, d, e, F2, KK2,3, 11);
345 R( e, a, b, c, d, F2, KK2,7, 8);
346 R( d, e, a, b, c, F2, KK2, 14, 6);
347 R( c, d, e, a, b, F2, KK2,6, 6);
348 R( b, c, d, e, a, F2, KK2,9, 14);
349 R( a, b, c, d, e, F2, KK2, 11, 12);
350 R( e, a, b, c, d, F2, KK2,8, 13);
351 R( d, e, a, b, c, F2, KK2, 12, 5);
352 R( c, d, e, a, b, F2, KK2,2, 14);
353 R( b, c, d, e, a, F2, KK2, 10, 13);
354 R( a, b, c, d, e, F2, KK2,0, 13);
355 R( e, a, b, c, d, F2, KK2,4, 7);
356 R( d, e, a, b, c, F2, KK2, 13, 5);
357 R( c, d, e, a, b, F1, KK3,8, 15);
358 R( b, c, d, e, a, F1, KK3,6, 5);
359 R( a, b, c, d, e, F1, KK3,4, 8);
360 R( e, a, b, c, d, F1, KK3,1, 11);
361 R( d, e, a, b, c, F1, KK3,3, 14);
362 R( c, d, e, a, b, F1, KK3, 11, 14);
363 R( b, c, d, e, a, F1, KK3, 15, 6);
364 R( a, b, c, d, e, F1, KK3,0, 14);
365 R( e, a, b, c, d, F1, KK3,5, 6);
366 R( d, e, a, b, c, F1, KK3, 12, 9);
367 R( c, d, e, a, b, F1, KK3,2, 12);
368 R( b, c, d, e, a, F1, KK3, 13, 9);
369 R( a, b, c, d, e, F1, KK3,9, 12);
370 R( e, a, b, c, d, F1, KK3,7, 5);
371 R( d, e, a, b, c, F1, KK3, 10, 15);
372 R( c, d, e, a, b, F1, KK3, 14, 8);
373 R( b, c, d, e, a, F0, KK4, 12, 8);
374 R( a, b, c, d, e, F0, KK4, 15, 5);
375 R( e, a, b, c, d, F0, KK4, 10, 12);
376 R( d, e, a, b, c, F0, KK4,4, 9);
377 R( c, d, e, a, b, F0, KK4,1, 12);
378 R( b, c, d, e, a, F0, KK4,5, 5);
379 R( a, b, c, d, e, F0, KK4,8, 14);
380 R( e, a, b, c, d, F0, KK4,7, 6);
381 R( d, e, a, b, c, F0, KK4,6, 8);
382 R( c, d, e, a, b, F0, KK4,2, 13);
383 R( b, c, d, e, a, F0, KK4, 13, 6);
384 R( a, b, c, d, e, F0, KK4, 14, 5);
385 R( e, a, b, c, d, F0, KK4,0, 15);
386 R( d, e, a, b, c, F0, KK4,3, 13);
387 R( c, d, e, a, b, F0, KK4,9, 11);
388 R( b, c, d, e, a, F0, KK4, 11, 11);
389
390
391 t = hd->h1 + d + cc;
392 hd->h1 = hd->h2 + e + dd;
393 hd->h2 = hd->h3 + a + ee;
394 hd->h3 = hd->h4 + b + aa;
395 hd->h4 = hd->h0 + c + bb;
396 hd->h0 = t;
397}
398
399
400/* Update the message digest with the contents
401 * of INBUF with length INLEN.
402 */
403static void
404rmd160_write( void *context, byte *inbuf, size_t inlen)
405{
406 RMD160_CONTEXT *hd = context;
407
408 if( hd->count == 64 ) /* flush the buffer */
409 {
410 transform( hd, hd->buf );
411 _gcry_burn_stack (108+5*sizeof(void*));
412 hd->count = 0;
413 hd->nblocks++;
414 }
415 if( !inbuf )
416 return;
417 if( hd->count )
418 {
419 for( ; inlen && hd->count < 64; inlen-- )
420 hd->buf[hd->count++] = *inbuf++;
421 rmd160_write( hd, NULL, 0 );
422 if( !inlen )
423 return;
424 }
425
426 while( inlen >= 64 )
427 {
428 transform( hd, inbuf );
429 hd->count = 0;
430 hd->nblocks++;
431 inlen -= 64;
432 inbuf += 64;
433 }
434 _gcry_burn_stack (108+5*sizeof(void*));
435 for( ; inlen && hd->count < 64; inlen-- )
436 hd->buf[hd->count++] = *inbuf++;
437}
438
439/****************
440 * Apply the rmd160 transform function on the buffer which must have
441 * a length 64 bytes. Do not use this function together with the
442 * other functions, use rmd160_init to initialize internal variables.
443 * Returns: 16 bytes in buffer with the mixed contentes of buffer.
444 */
445void
446_gcry_rmd160_mixblock( RMD160_CONTEXT *hd, char *buffer )
447{
448 char *p = buffer;
449
450 transform( hd, buffer );
451#define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
452 X(0);
453 X(1);
454 X(2);
455 X(3);
456 X(4);
457#undef X
458}
459
460
461/* The routine terminates the computation
462 */
463
464static void
465rmd160_final( void *context )
466{
467 RMD160_CONTEXT *hd = context;
468 u32 t, msb, lsb;
469 byte *p;
470
471 rmd160_write(hd, NULL, 0); /* flush */;
472
473 t = hd->nblocks;
474 /* multiply by 64 to make a byte count */
475 lsb = t << 6;
476 msb = t >> 26;
477 /* add the count */
478 t = lsb;
479 if( (lsb += hd->count) < t )
480 msb++;
481 /* multiply by 8 to make a bit count */
482 t = lsb;
483 lsb <<= 3;
484 msb <<= 3;
485 msb |= t >> 29;
486
487 if( hd->count < 56 ) /* enough room */
488 {
489 hd->buf[hd->count++] = 0x80; /* pad */
490 while( hd->count < 56 )
491 hd->buf[hd->count++] = 0; /* pad */
492 }
493 else /* need one extra block */
494 {
495 hd->buf[hd->count++] = 0x80; /* pad character */
496 while( hd->count < 64 )
497 hd->buf[hd->count++] = 0;
498 rmd160_write(hd, NULL, 0); /* flush */;
499 memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
500 }
501 /* append the 64 bit count */
502 hd->buf[56] = lsb ;
503 hd->buf[57] = lsb >> 8;
504 hd->buf[58] = lsb >> 16;
505 hd->buf[59] = lsb >> 24;
506 hd->buf[60] = msb ;
507 hd->buf[61] = msb >> 8;
508 hd->buf[62] = msb >> 16;
509 hd->buf[63] = msb >> 24;
510 transform( hd, hd->buf );
511 _gcry_burn_stack (108+5*sizeof(void*));
512
513 p = hd->buf;
514#ifdef WORDS_BIGENDIAN
515 #define X(a) do { *p++ = hd->h##a ; *p++ = hd->h##a >> 8;\
516 *p++ = hd->h##a >> 16; *p++ = hd->h##a >> 24; } while(0)
517#else /* little endian */
518#define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
519#endif
520 X(0);
521 X(1);
522 X(2);
523 X(3);
524 X(4);
525#undef X
526}
527
528static byte *
529rmd160_read( void *context )
530{
531 RMD160_CONTEXT *hd = context;
532
533 return hd->buf;
534}
535
536
537
538/****************
539 * Shortcut functions which puts the hash value of the supplied buffer
540 * into outbuf which must have a size of 20 bytes.
541 */
542void
543_gcry_rmd160_hash_buffer( char *outbuf, const char *buffer, size_t length )
544{
545 RMD160_CONTEXT hd;
546
547 _gcry_rmd160_init( &hd );
548 rmd160_write( &hd, (byte*)buffer, length );
549 rmd160_final( &hd );
550 memcpy( outbuf, hd.buf, 20 );
551}
552
553static byte asn[15] = /* Object ID is 1.3.36.3.2.1 */
554 { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x24, 0x03,
555 0x02, 0x01, 0x05, 0x00, 0x04, 0x14 };
556
557static gcry_md_oid_spec_t oid_spec_rmd160[] =
558 {
559 /* rsaSignatureWithripemd160 */
560 { "1.3.36.3.3.1.2" },
561 { NULL },
562 };
563
564gcry_md_spec_t _gcry_digest_spec_rmd160 =
565 {
566 "RIPEMD160", asn, DIM (asn), oid_spec_rmd160, 20,
567 _gcry_rmd160_init, rmd160_write, rmd160_final, rmd160_read,
568 sizeof (RMD160_CONTEXT)
569 };
diff --git a/pwmanager/libcrypt/cipher/rndegd.c b/pwmanager/libcrypt/cipher/rndegd.c
new file mode 100644
index 0000000..29b48e8
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/rndegd.c
@@ -0,0 +1,256 @@
1 /* rndegd.c -interface to the EGD
2 *Copyright (C) 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <config.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <assert.h>
25#include <errno.h>
26#include <sys/time.h>
27#include <sys/stat.h>
28#include <string.h>
29#include <unistd.h>
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <sys/un.h>
33#include "types.h"
34#include "g10lib.h"
35#include "cipher.h"
36#include "rand-internal.h"
37
38#ifndef offsetof
39#define offsetof(type, member) ((size_t) &((type *)0)->member)
40#endif
41
42static int egd_socket = -1;
43
44/* Allocate a new filename from FIRST_PART and SECOND_PART and to
45 tilde expansion for first_part. SECOND_PART might be NULL.
46 */
47static char *
48my_make_filename (const char *first_part, const char *second_part)
49{
50 size_t n;
51 char *name, *home, *p;
52
53 n = strlen(first_part)+1;
54 if (second_part)
55 n += strlen (second_part) + 1;
56
57 home = NULL;
58 if( *first_part == '~' && first_part[1] == '/'
59 && (home = getenv("HOME")) && *home )
60 n += strlen(home);
61
62 name = gcry_xmalloc(n);
63 p = (home
64 ? stpcpy (stpcpy (name, home), first_part+1 )
65 : stpcpy (name, first_part) );
66
67 if (second_part)
68 strcpy (stpcpy(p,"/"), second_part);
69
70 return name;
71}
72
73
74static int
75do_write( int fd, void *buf, size_t nbytes )
76{
77 size_t nleft = nbytes;
78 int nwritten;
79
80 while( nleft > 0 )
81 {
82 nwritten = write( fd, buf, nleft);
83 if( nwritten < 0 )
84 {
85 if( errno == EINTR )
86 continue;
87 return -1;
88 }
89 nleft -= nwritten;
90 buf = (char*)buf + nwritten;
91 }
92 return 0;
93}
94
95static int
96do_read( int fd, void *buf, size_t nbytes )
97{
98 int n, nread = 0;
99
100 do
101 {
102 do
103 {
104 n = read(fd, (char*)buf + nread, nbytes );
105 }
106 while( n == -1 && errno == EINTR );
107 if( n == -1)
108 return nread? nread:-1;
109 if( n == 0)
110 return -1;
111 nread += n;
112 nbytes -= n;
113 }
114 while( nread < nbytes );
115 return nread;
116}
117
118
119/* Connect to the EGD and return the file descriptor. Return -1 on
120 error. With NOFAIL set to true, silently fail and return the
121 error, otherwise print an error message and die. */
122int
123_gcry_rndegd_connect_socket (int nofail)
124{
125 int fd;
126 const char *bname = NULL;
127 char *name;
128 struct sockaddr_un addr;
129 int addr_len;
130
131 if (egd_socket != -1)
132 {
133 close (egd_socket);
134 egd_socket = -1;
135 }
136
137#ifdef EGD_SOCKET_NAME
138 bname = EGD_SOCKET_NAME;
139#endif
140 if ( !bname || !*bname )
141 name = my_make_filename ("~/.gnupg", "entropy");
142 else
143 name = my_make_filename (bname, NULL);
144
145 if (strlen(name)+1 >= sizeof addr.sun_path)
146 log_fatal ("EGD socketname is too long\n");
147
148 memset( &addr, 0, sizeof addr );
149 addr.sun_family = AF_UNIX;
150 strcpy( addr.sun_path, name );
151 addr_len = (offsetof( struct sockaddr_un, sun_path )
152 + strlen( addr.sun_path ));
153
154 fd = socket(AF_UNIX, SOCK_STREAM, 0);
155 if (fd == -1 && !nofail)
156 log_fatal("can't create unix domain socket: %s\n", strerror(errno) );
157 else if (connect (fd, (struct sockaddr*)&addr, addr_len) == -1)
158 {
159 if (!nofail)
160 log_fatal("can't connect to EGD socket `%s': %s\n",
161 name, strerror(errno) );
162 close (fd);
163 fd = -1;
164 }
165 gcry_free(name);
166 if (fd != -1)
167 egd_socket = fd;
168 return fd;
169}
170
171/****************
172 * Note: We always use the highest level.
173 * To boost the performance we may want to add some
174 * additional code for level 1
175 *
176 * Using a level of 0 should never block and better add nothing
177 * to the pool. So this is just a dummy for EGD.
178 */
179int
180_gcry_rndegd_gather_random (void (*add)(const void*, size_t, int),
181 int requester,
182 size_t length, int level )
183{
184 int fd = egd_socket;
185 int n;
186 byte buffer[256+2];
187 int nbytes;
188 int do_restart = 0;
189
190 if( !length )
191 return 0;
192 if( !level )
193 return 0;
194
195 restart:
196 if (fd == -1 || do_restart)
197 fd = _gcry_rndegd_connect_socket (0);
198
199 do_restart = 0;
200
201 nbytes = length < 255? length : 255;
202 /* First time we do it with a non blocking request */
203 buffer[0] = 1; /* non blocking */
204 buffer[1] = nbytes;
205 if( do_write( fd, buffer, 2 ) == -1 )
206 log_fatal("can't write to the EGD: %s\n", strerror(errno) );
207 n = do_read( fd, buffer, 1 );
208 if( n == -1 )
209 {
210 log_error("read error on EGD: %s\n", strerror(errno));
211 do_restart = 1;
212 goto restart;
213 }
214 n = buffer[0];
215 if( n )
216 {
217 n = do_read( fd, buffer, n );
218 if( n == -1 )
219 {
220 log_error("read error on EGD: %s\n", strerror(errno));
221 do_restart = 1;
222 goto restart;
223 }
224 (*add)( buffer, n, requester );
225 length -= n;
226 }
227
228 if( length )
229 {
230 log_info (
231 _("Please wait, entropy is being gathered. Do some work if it would\n"
232 "keep you from getting bored, because it will improve the quality\n"
233 "of the entropy.\n") );
234 }
235 while( length )
236 {
237 nbytes = length < 255? length : 255;
238
239 buffer[0] = 2; /* blocking */
240 buffer[1] = nbytes;
241 if( do_write( fd, buffer, 2 ) == -1 )
242 log_fatal("can't write to the EGD: %s\n", strerror(errno) );
243 n = do_read( fd, buffer, nbytes );
244 if( n == -1 )
245 {
246 log_error("read error on EGD: %s\n", strerror(errno));
247 do_restart = 1;
248 goto restart;
249 }
250 (*add)( buffer, n, requester );
251 length -= n;
252 }
253 memset(buffer, 0, sizeof(buffer) );
254
255 return 0; /* success */
256}
diff --git a/pwmanager/libcrypt/cipher/rndlinux.c b/pwmanager/libcrypt/cipher/rndlinux.c
new file mode 100644
index 0000000..fc608bc
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/rndlinux.c
@@ -0,0 +1,141 @@
1/* rndlinux.c - raw random number for OSes with /dev/random
2 * Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21
22#include <config.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <assert.h>
26#include <errno.h>
27#include <sys/time.h>
28#include <sys/types.h>
29#include <sys/stat.h>
30#ifdef HAVE_GETTIMEOFDAY
31# include <sys/times.h>
32#endif
33#include <string.h>
34#include <unistd.h>
35#include <fcntl.h>
36#include "types.h"
37#include "g10lib.h"
38#include "rand-internal.h"
39
40static int open_device( const char *name, int minor );
41int _gcry_rndlinux_gather_random (void (*add)(const void*, size_t, int),
42 int requester,
43 size_t length, int level );
44
45/*
46 * Used to open the /dev/random devices (Linux, xBSD, Solaris (if it exists)).
47 */
48static int
49open_device( const char *name, int minor )
50{
51 int fd;
52
53 fd = open( name, O_RDONLY );
54 if( fd == -1 )
55 log_fatal ("can't open %s: %s\n", name, strerror(errno) );
56
57 /* We used to do the follwing check, however it turned out that this
58 is not portable since more OSes provide a random device which is
59 sometimes implemented as anoteher device type.
60
61 struct stat sb;
62
63 if( fstat( fd, &sb ) )
64 log_fatal("stat() off %s failed: %s\n", name, strerror(errno) );
65 if( (!S_ISCHR(sb.st_mode)) && (!S_ISFIFO(sb.st_mode)) )
66 log_fatal("invalid random device!\n" );
67 */
68 return fd;
69}
70
71
72int
73_gcry_rndlinux_gather_random (void (*add)(const void*, size_t, int),
74 int requester,
75 size_t length, int level )
76{
77 static int fd_urandom = -1;
78 static int fd_random = -1;
79 int fd;
80 int n;
81 int warn=0;
82 byte buffer[768];
83
84 if( level >= 2 )
85 {
86 if( fd_random == -1 )
87 fd_random = open_device( NAME_OF_DEV_RANDOM, 8 );
88 fd = fd_random;
89 }
90 else
91 {
92 if( fd_urandom == -1 )
93 fd_urandom = open_device( NAME_OF_DEV_URANDOM, 9 );
94 fd = fd_urandom;
95 }
96
97 while (length)
98 {
99 fd_set rfds;
100 struct timeval tv;
101 int rc;
102
103 FD_ZERO(&rfds);
104 FD_SET(fd, &rfds);
105 tv.tv_sec = 3;
106 tv.tv_usec = 0;
107 if( !(rc=select(fd+1, &rfds, NULL, NULL, &tv)) )
108 {
109 if( !warn )
110 {
111 _gcry_random_progress ("need_entropy", 'X', 0, (int)length);
112 warn = 1;
113 }
114 continue;
115 }
116 else if( rc == -1 )
117 {
118 log_error ("select() error: %s\n", strerror(errno));
119 continue;
120 }
121
122 do
123 {
124 int nbytes = length < sizeof(buffer)? length : sizeof(buffer);
125 n = read(fd, buffer, nbytes );
126 if( n >= 0 && n > nbytes )
127 {
128 log_error("bogus read from random device (n=%d)\n", n );
129 n = nbytes;
130 }
131 }
132 while( n == -1 && errno == EINTR );
133 if( n == -1 )
134 log_fatal("read error on random device: %s\n", strerror(errno));
135 (*add)( buffer, n, requester );
136 length -= n;
137 }
138 memset(buffer, 0, sizeof(buffer) );
139
140 return 0; /* success */
141}
diff --git a/pwmanager/libcrypt/cipher/rndunix.c b/pwmanager/libcrypt/cipher/rndunix.c
new file mode 100644
index 0000000..3ebb823
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/rndunix.c
@@ -0,0 +1,848 @@
1/****************************************************************************
2 * *
3 * *
4 * Unix Randomness-Gathering Code *
5 * *
6 * Copyright Peter Gutmann, Paul Kendall, and Chris Wedgwood 1996-1999. *
7 * Heavily modified for GnuPG by Werner Koch *
8 * *
9 * *
10 ****************************************************************************/
11
12/* This module is part of the cryptlib continuously seeded pseudorandom
13 number generator. For usage conditions, see lib_rand.c
14
15 [Here is the notice from lib_rand.c:]
16
17 This module and the misc/rnd*.c modules represent the cryptlib
18 continuously seeded pseudorandom number generator (CSPRNG) as described in
19 my 1998 Usenix Security Symposium paper "The generation of random numbers
20 for cryptographic purposes".
21
22 The CSPRNG code is copyright Peter Gutmann (and various others) 1996,
23 1997, 1998, 1999, all rights reserved. Redistribution of the CSPRNG
24 modules and use in source and binary forms, with or without modification,
25 are permitted provided that the following conditions are met:
26
27 1. Redistributions of source code must retain the above copyright notice
28 and this permission notice in its entirety.
29
30 2. Redistributions in binary form must reproduce the copyright notice in
31 the documentation and/or other materials provided with the distribution.
32
33 3. A copy of any bugfixes or enhancements made must be provided to the
34 author, <pgut001@cs.auckland.ac.nz> to allow them to be added to the
35 baseline version of the code.
36
37 ALTERNATIVELY, the code may be distributed under the terms of the GNU
38 General Public License, version 2 or any later version published by the
39 Free Software Foundation, in which case the provisions of the GNU GPL are
40 required INSTEAD OF the above restrictions.
41
42 Although not required under the terms of the GPL, it would still be nice if
43 you could make any changes available to the author to allow a consistent
44 code base to be maintained */
45
46
47/* Fixme: We use plain mallocs here beucase it may be used as a module
48 * should be changed. */
49
50/* General includes */
51
52#include <config.h>
53#include <stdlib.h>
54#include <stdio.h>
55#include <string.h>
56#include <assert.h>
57
58/* OS-specific includes */
59
60#ifdef __osf__
61 /* Somewhere in the morass of system-specific cruft which OSF/1 pulls in
62 * via the following includes are various endianness defines, so we
63 * undefine the cryptlib ones, which aren't really needed for this module
64 * anyway */
65#undef BIG_ENDIAN
66#undef LITTLE_ENDIAN
67 #endif /* __osf__ */
68
69#include <unistd.h>
70#include <fcntl.h>
71#include <pwd.h>
72#ifndef __QNX__
73#include <sys/errno.h>
74#include <sys/ipc.h>
75 #endif /* __QNX__ */
76 #include <sys/time.h> /* SCO and SunOS need this before resource.h */
77#ifndef __QNX__
78#include <sys/resource.h>
79 #endif /* __QNX__ */
80#if defined( _AIX ) || defined( __QNX__ )
81#include <sys/select.h>
82 #endif /* _AIX */
83#ifndef __QNX__
84#include <sys/shm.h>
85#include <signal.h>
86#include <sys/signal.h>
87 #endif /* __QNX__ */
88#include <sys/stat.h>
89 #include <sys/types.h> /* Verschiedene komische Typen */
90#if defined( __hpux ) && ( OS_VERSION == 9 )
91#include <vfork.h>
92 #endif /* __hpux 9.x, after that it's in unistd.h */
93#include <sys/wait.h>
94/* #include <kitchensink.h> */
95#ifdef __QNX__
96#include <signal.h>
97#include <process.h>
98 #endif /* __QNX__ */
99#include <errno.h>
100
101#include "types.h" /* for byte and u32 typedefs */
102#include "g10lib.h"
103#include "rand-internal.h"
104
105#ifndef EAGAIN
106 #define EAGAINEWOULDBLOCK
107#endif
108#ifndef STDIN_FILENO
109#define STDIN_FILENO 0
110#endif
111#ifndef STDOUT_FILENO
112#define STDOUT_FILENO 1
113#endif
114
115 #define GATHER_BUFSIZE 49152/* Usually about 25K are filled */
116
117/* The structure containing information on random-data sources. Each
118 * record contains the source and a relative estimate of its usefulness
119 * (weighting) which is used to scale the number of kB of output from the
120 * source (total = data_bytes / usefulness). Usually the weighting is in the
121 * range 1-3 (or 0 for especially useless sources), resulting in a usefulness
122 * rating of 1...3 for each kB of source output (or 0 for the useless
123 * sources).
124 *
125 * If the source is constantly changing (certain types of network statistics
126 * have this characteristic) but the amount of output is small, the weighting
127 * is given as a negative value to indicate that the output should be treated
128 * as if a minimum of 1K of output had been obtained. If the source produces
129 * a lot of output then the scale factor is fractional, resulting in a
130 * usefulness rating of < 1 for each kB of source output.
131 *
132 * In order to provide enough randomness to satisfy the requirements for a
133 * slow poll, we need to accumulate at least 20 points of usefulness (a
134 * typical system should get about 30 points).
135 *
136 * Some potential options are missed out because of special considerations.
137 * pstat -i and pstat -f can produce amazing amounts of output (the record
138 * is 600K on an Oracle server) which floods the buffer and doesn't yield
139 * anything useful (apart from perhaps increasing the entropy of the vmstat
140 * output a bit), so we don't bother with this. pstat in general produces
141 * quite a bit of output, but it doesn't change much over time, so it gets
142 * very low weightings. netstat -s produces constantly-changing output but
143 * also produces quite a bit of it, so it only gets a weighting of 2 rather
144 * than 3. The same holds for netstat -in, which gets 1 rather than 2.
145 *
146 * Some binaries are stored in different locations on different systems so
147 * alternative paths are given for them. The code sorts out which one to
148 * run by itself, once it finds an exectable somewhere it moves on to the
149 * next source. The sources are arranged roughly in their order of
150 * usefulness, occasionally sources which provide a tiny amount of
151 * relatively useless data are placed ahead of ones which provide a large
152 * amount of possibly useful data because another 100 bytes can't hurt, and
153 * it means the buffer won't be swamped by one or two high-output sources.
154 * All the high-output sources are clustered towards the end of the list
155 * for this reason. Some binaries are checked for in a certain order, for
156 * example under Slowaris /usr/ucb/ps understands aux as an arg, but the
157 * others don't. Some systems have conditional defines enabling alternatives
158 * to commands which don't understand the usual options but will provide
159 * enough output (in the form of error messages) to look like they're the
160 * real thing, causing alternative options to be skipped (we can't check the
161 * return either because some commands return peculiar, non-zero status even
162 * when they're working correctly).
163 *
164 * In order to maximise use of the buffer, the code performs a form of run-
165 * length compression on its input where a repeated sequence of bytes is
166 * replaced by the occurrence count mod 256. Some commands output an awful
167 * lot of whitespace, this measure greatly increases the amount of data we
168 * can fit in the buffer.
169 *
170 * When we scale the weighting using the SC() macro, some preprocessors may
171 * give a division by zero warning for the most obvious expression
172 * 'weight ? 1024 / weight : 0' (and gcc 2.7.2.2 dies with a division by zero
173 * trap), so we define a value SC_0 which evaluates to zero when fed to
174 * '1024 / SC_0' */
175
176 #define SC( weight ) ( 1024 / weight )/* Scale factor */
177 #define SC_0 16384/* SC( SC_0 ) evalutes to 0 */
178
179static struct RI {
180 const char *path; /* Path to check for existence of source */
181 const char *arg; /* Args for source */
182 const int usefulness;/* Usefulness of source */
183 FILE *pipe; /* Pipe to source as FILE * */
184 int pipeFD; /* Pipe to source as FD */
185 pid_t pid; /* pid of child for waitpid() */
186 int length; /* Quantity of output produced */
187 const int hasAlternative; /* Whether source has alt.location */
188} dataSources[] = {
189
190 {"/bin/vmstat", "-s", SC(-3), NULL, 0, 0, 0, 1 },
191 {"/usr/bin/vmstat", "-s", SC(-3), NULL, 0, 0, 0, 0},
192 {"/bin/vmstat", "-c", SC(-3), NULL, 0, 0, 0, 1 },
193 {"/usr/bin/vmstat", "-c", SC(-3), NULL, 0, 0, 0, 0},
194 {"/usr/bin/pfstat", NULL, SC(-2), NULL, 0, 0, 0, 0},
195 {"/bin/vmstat", "-i", SC(-2), NULL, 0, 0, 0, 1 },
196 {"/usr/bin/vmstat", "-i", SC(-2), NULL, 0, 0, 0, 0},
197 {"/usr/ucb/netstat", "-s", SC(2), NULL, 0, 0, 0, 1 },
198 {"/usr/bin/netstat", "-s", SC(2), NULL, 0, 0, 0, 1 },
199 {"/usr/sbin/netstat", "-s", SC(2), NULL, 0, 0, 0, 1},
200 {"/usr/etc/netstat", "-s", SC(2), NULL, 0, 0, 0, 0},
201 {"/usr/bin/nfsstat", NULL, SC(2), NULL, 0, 0, 0, 0},
202 {"/usr/ucb/netstat", "-m", SC(-1), NULL, 0, 0, 0, 1 },
203 {"/usr/bin/netstat", "-m", SC(-1), NULL, 0, 0, 0, 1 },
204 {"/usr/sbin/netstat", "-m", SC(-1), NULL, 0, 0, 0, 1 },
205 {"/usr/etc/netstat", "-m", SC(-1), NULL, 0, 0, 0, 0 },
206 {"/bin/netstat", "-in", SC(-1), NULL, 0, 0, 0, 1 },
207 {"/usr/ucb/netstat", "-in", SC(-1), NULL, 0, 0, 0, 1 },
208 {"/usr/bin/netstat", "-in", SC(-1), NULL, 0, 0, 0, 1 },
209 {"/usr/sbin/netstat", "-in", SC(-1), NULL, 0, 0, 0, 1},
210 {"/usr/etc/netstat", "-in", SC(-1), NULL, 0, 0, 0, 0},
211 {"/usr/sbin/snmp_request", "localhost public get 1.3.6.1.2.1.7.1.0",
212 SC(-1), NULL, 0, 0, 0, 0 }, /* UDP in */
213 {"/usr/sbin/snmp_request", "localhost public get 1.3.6.1.2.1.7.4.0",
214 SC(-1), NULL, 0, 0, 0, 0 }, /* UDP out */
215 {"/usr/sbin/snmp_request", "localhost public get 1.3.6.1.2.1.4.3.0",
216 SC(-1), NULL, 0, 0, 0, 0 }, /* IP ? */
217 {"/usr/sbin/snmp_request", "localhost public get 1.3.6.1.2.1.6.10.0",
218 SC(-1), NULL, 0, 0, 0, 0 }, /* TCP ? */
219 {"/usr/sbin/snmp_request", "localhost public get 1.3.6.1.2.1.6.11.0",
220 SC(-1), NULL, 0, 0, 0, 0 }, /* TCP ? */
221 {"/usr/sbin/snmp_request", "localhost public get 1.3.6.1.2.1.6.13.0",
222 SC(-1), NULL, 0, 0, 0, 0 }, /* TCP ? */
223 {"/usr/bin/mpstat", NULL, SC(1), NULL, 0, 0, 0, 0 },
224 {"/usr/bin/w", NULL, SC(1), NULL, 0, 0, 0, 1 },
225 {"/usr/bsd/w", NULL, SC(1), NULL, 0, 0, 0, 0 },
226 {"/usr/bin/df", NULL, SC(1), NULL, 0, 0, 0, 1 },
227 {"/bin/df", NULL, SC(1), NULL, 0, 0, 0, 0 },
228 {"/usr/sbin/portstat", NULL, SC(1), NULL, 0, 0, 0, 0 },
229 {"/usr/bin/iostat", NULL, SC(SC_0), NULL, 0, 0, 0, 0 },
230 {"/usr/bin/uptime", NULL, SC(SC_0), NULL, 0, 0, 0, 1 },
231 {"/usr/bsd/uptime", NULL, SC(SC_0), NULL, 0, 0, 0, 0 },
232 {"/bin/vmstat", "-f", SC(SC_0), NULL, 0, 0, 0, 1 },
233 {"/usr/bin/vmstat", "-f", SC(SC_0), NULL, 0, 0, 0, 0 },
234 {"/bin/vmstat", NULL, SC(SC_0), NULL, 0, 0, 0, 1 },
235 {"/usr/bin/vmstat", NULL, SC(SC_0), NULL, 0, 0, 0, 0 },
236 {"/usr/ucb/netstat", "-n", SC(0.5), NULL, 0, 0, 0, 1 },
237 {"/usr/bin/netstat", "-n", SC(0.5), NULL, 0, 0, 0, 1 },
238 {"/usr/sbin/netstat", "-n", SC(0.5), NULL, 0, 0, 0, 1 },
239 {"/usr/etc/netstat", "-n", SC(0.5), NULL, 0, 0, 0, 0 },
240#if defined( __sgi ) || defined( __hpux )
241 {"/bin/ps", "-el", SC(0.3), NULL, 0, 0, 0, 1 },
242 #endif /* __sgi || __hpux */
243 {"/usr/ucb/ps", "aux", SC(0.3), NULL, 0, 0, 0, 1 },
244 {"/usr/bin/ps", "aux", SC(0.3), NULL, 0, 0, 0, 1 },
245 {"/bin/ps", "aux", SC(0.3), NULL, 0, 0, 0, 0 },
246 { "/bin/ps", "-A", SC(0.3), NULL, 0, 0, 0, 0 }, /*QNX*/
247 {"/usr/bin/ipcs", "-a", SC(0.5), NULL, 0, 0, 0, 1 },
248 {"/bin/ipcs", "-a", SC(0.5), NULL, 0, 0, 0, 0 },
249 /* Unreliable source, depends on system usage */
250 {"/etc/pstat", "-p", SC(0.5), NULL, 0, 0, 0, 1 },
251 {"/bin/pstat", "-p", SC(0.5), NULL, 0, 0, 0, 0 },
252 {"/etc/pstat", "-S", SC(0.2), NULL, 0, 0, 0, 1 },
253 {"/bin/pstat", "-S", SC(0.2), NULL, 0, 0, 0, 0 },
254 {"/etc/pstat", "-v", SC(0.2), NULL, 0, 0, 0, 1 },
255 {"/bin/pstat", "-v", SC(0.2), NULL, 0, 0, 0, 0 },
256 {"/etc/pstat", "-x", SC(0.2), NULL, 0, 0, 0, 1 },
257 {"/bin/pstat", "-x", SC(0.2), NULL, 0, 0, 0, 0 },
258 {"/etc/pstat", "-t", SC(0.1), NULL, 0, 0, 0, 1 },
259 {"/bin/pstat", "-t", SC(0.1), NULL, 0, 0, 0, 0 },
260 /* pstat is your friend */
261 {"/usr/bin/last", "-n 50", SC(0.3), NULL, 0, 0, 0, 1 },
262#ifdef __sgi
263 {"/usr/bsd/last", "-50", SC(0.3), NULL, 0, 0, 0, 0 },
264 #endif /* __sgi */
265#ifdef __hpux
266 {"/etc/last", "-50", SC(0.3), NULL, 0, 0, 0, 0 },
267 #endif /* __hpux */
268 {"/usr/bsd/last", "-n 50", SC(0.3), NULL, 0, 0, 0, 0 },
269 {"/usr/sbin/snmp_request", "localhost public get 1.3.6.1.2.1.5.1.0",
270 SC(0.1), NULL, 0, 0, 0, 0 }, /* ICMP ? */
271 {"/usr/sbin/snmp_request", "localhost public get 1.3.6.1.2.1.5.3.0",
272 SC(0.1), NULL, 0, 0, 0, 0 }, /* ICMP ? */
273 {"/etc/arp", "-a", SC(0.1), NULL, 0, 0, 0, 1 },
274 {"/usr/etc/arp", "-a", SC(0.1), NULL, 0, 0, 0, 1 },
275 {"/usr/bin/arp", "-a", SC(0.1), NULL, 0, 0, 0, 1 },
276 {"/usr/sbin/arp", "-a", SC(0.1), NULL, 0, 0, 0, 0 },
277 {"/usr/sbin/ripquery", "-nw 1 127.0.0.1",
278 SC(0.1), NULL, 0, 0, 0, 0 },
279 {"/bin/lpstat", "-t", SC(0.1), NULL, 0, 0, 0, 1 },
280 {"/usr/bin/lpstat", "-t", SC(0.1), NULL, 0, 0, 0, 1 },
281 {"/usr/ucb/lpstat", "-t", SC(0.1), NULL, 0, 0, 0, 0 },
282 {"/usr/bin/tcpdump", "-c 5 -efvvx", SC(1), NULL, 0, 0, 0, 0 },
283 /* This is very environment-dependant. If network traffic is low, it'll
284 * probably time out before delivering 5 packets, which is OK because
285 * it'll probably be fixed stuff like ARP anyway */
286 {"/usr/sbin/advfsstat", "-b usr_domain",
287 SC(SC_0), NULL, 0, 0, 0, 0},
288 {"/usr/sbin/advfsstat", "-l 2 usr_domain",
289 SC(0.5), NULL, 0, 0, 0, 0},
290 {"/usr/sbin/advfsstat", "-p usr_domain",
291 SC(SC_0), NULL, 0, 0, 0, 0},
292 /* This is a complex and screwball program. Some systems have things
293 * like rX_dmn, x = integer, for RAID systems, but the statistics are
294 * pretty dodgy */
295#ifdef __QNXNTO__
296 { "/bin/pidin", "-F%A%B%c%d%E%I%J%K%m%M%n%N%p%P%S%s%T", SC(0.3),
297 NULL, 0, 0, 0, 0 },
298#endif
299#if 0
300 /* The following aren't enabled since they're somewhat slow and not very
301 * unpredictable, however they give an indication of the sort of sources
302 * you can use (for example the finger might be more useful on a
303 * firewalled internal network) */
304 {"/usr/bin/finger", "@ml.media.mit.edu", SC(0.9), NULL, 0, 0, 0, 0 },
305 {"/usr/local/bin/wget", "-O - http://lavarand.sgi.com/block.html",
306 SC(0.9), NULL, 0, 0, 0, 0 },
307 {"/bin/cat", "/usr/spool/mqueue/syslog", SC(0.9), NULL, 0, 0, 0, 0 },
308 #endif /* 0 */
309 {NULL, NULL, 0, NULL, 0, 0, 0, 0 }
310};
311
312 static byte *gather_buffer; /* buffer for gathering random noise */
313 static int gather_buffer_size; /* size of the memory buffer */
314static uid_t gatherer_uid;
315
316/* The message structure used to communicate with the parent */
317typedef struct {
318 int usefulness;/* usefulness of data */
319 int ndata; /* valid bytes in data */
320 char data[500];/* gathered data */
321} GATHER_MSG;
322
323#ifndef HAVE_WAITPID
324static pid_t
325waitpid(pid_t pid, int *statptr, int options)
326{
327#ifdef HAVE_WAIT4
328 return wait4(pid, statptr, options, NULL);
329#else
330 /* If wait4 is also not available, try wait3 for SVR3 variants */
331 /* Less ideal because can't actually request a specific pid */
332 /* For that reason, first check to see if pid is for an */
333 /* existing process. */
334 int tmp_pid, dummystat;;
335 if (kill(pid, 0) == -1) {
336 errno = ECHILD;
337 return -1;
338 }
339 if (statptr == NULL)
340 statptr = &dummystat;
341 while (((tmp_pid = wait3(statptr, options, 0)) != pid) &&
342 (tmp_pid != -1) && (tmp_pid != 0) && (pid != -1))
343 ;
344 return tmp_pid;
345#endif
346}
347#endif
348
349/* Under SunOS popen() doesn't record the pid of the child process. When
350 * pclose() is called, instead of calling waitpid() for the correct child, it
351 * calls wait() repeatedly until the right child is reaped. The problem is
352 * that this reaps any other children that happen to have died at that
353 * moment, and when their pclose() comes along, the process hangs forever.
354 * The fix is to use a wrapper for popen()/pclose() which saves the pid in
355 * the dataSources structure (code adapted from GNU-libc's popen() call).
356 *
357 * Aut viam inveniam aut faciam */
358
359static FILE *
360my_popen(struct RI *entry)
361{
362 int pipedes[2];
363 FILE *stream;
364
365 /* Create the pipe */
366 if (pipe(pipedes) < 0)
367 return (NULL);
368
369 /* Fork off the child ("vfork() is like an OS orgasm. All OS's want to
370 * do it, but most just end up faking it" - Chris Wedgwood). If your OS
371 * supports it, you should try to use vfork() here because it's somewhat
372 * more efficient */
373#if defined( sun ) || defined( __ultrix__ ) || defined( __osf__ ) || \
374 defined(__hpux)
375 entry->pid = vfork();
376 #else /* */
377 entry->pid = fork();
378 #endif /* Unixen which have vfork() */
379 if (entry->pid == (pid_t) - 1) {
380 /* The fork failed */
381 close(pipedes[0]);
382 close(pipedes[1]);
383 return (NULL);
384 }
385
386 if (entry->pid == (pid_t) 0) {
387 struct passwd *passwd;
388
389 /* We are the child. Make the read side of the pipe be stdout */
390 if (dup2(pipedes[STDOUT_FILENO], STDOUT_FILENO) < 0)
391 exit(127);
392
393 /* Now that everything is set up, give up our permissions to make
394 * sure we don't read anything sensitive. If the getpwnam() fails,
395 * we default to -1, which is usually nobody */
396 if (gatherer_uid == (uid_t)-1 && \
397 (passwd = getpwnam("nobody")) != NULL)
398 gatherer_uid = passwd->pw_uid;
399
400 setuid(gatherer_uid);
401
402 /* Close the pipe descriptors */
403 close(pipedes[STDIN_FILENO]);
404 close(pipedes[STDOUT_FILENO]);
405
406 /* Try and exec the program */
407 execl(entry->path, entry->path, entry->arg, NULL);
408
409 /* Die if the exec failed */
410 exit(127);
411 }
412
413 /* We are the parent. Close the irrelevant side of the pipe and open
414 * the relevant side as a new stream. Mark our side of the pipe to
415 * close on exec, so new children won't see it */
416 close(pipedes[STDOUT_FILENO]);
417
418#ifdef FD_CLOEXEC
419 fcntl(pipedes[STDIN_FILENO], F_SETFD, FD_CLOEXEC);
420#endif
421
422 stream = fdopen(pipedes[STDIN_FILENO], "r");
423
424 if (stream == NULL) {
425 int savedErrno = errno;
426
427 /* The stream couldn't be opened or the child structure couldn't be
428 * allocated. Kill the child and close the other side of the pipe */
429 kill(entry->pid, SIGKILL);
430 if (stream == NULL)
431 close(pipedes[STDOUT_FILENO]);
432 else
433 fclose(stream);
434
435 waitpid(entry->pid, NULL, 0);
436
437 entry->pid = 0;
438 errno = savedErrno;
439 return (NULL);
440 }
441
442 return (stream);
443}
444
445static int
446my_pclose(struct RI *entry)
447{
448 int status = 0;
449
450 if (fclose(entry->pipe))
451 return (-1);
452
453 /* We ignore the return value from the process because some programs
454 * return funny values which would result in the input being discarded
455 * even if they executed successfully. This isn't a problem because the
456 * result data size threshold will filter out any programs which exit
457 * with a usage message without producing useful output */
458 if (waitpid(entry->pid, NULL, 0) != entry->pid)
459 status = -1;
460
461 entry->pipe = NULL;
462 entry->pid = 0;
463 return (status);
464}
465
466
467/* Unix slow poll (without special support for Linux)
468 *
469 * If a few of the randomness sources create a large amount of output then
470 * the slowPoll() stops once the buffer has been filled (but before all the
471 * randomness sources have been sucked dry) so that the 'usefulness' factor
472 * remains below the threshold. For this reason the gatherer buffer has to
473 * be fairly sizeable on moderately loaded systems. This is something of a
474 * bug since the usefulness should be influenced by the amount of output as
475 * well as the source type */
476
477
478static int
479slow_poll(FILE *dbgfp, int dbgall, size_t *nbytes )
480{
481 int moreSources;
482 struct timeval tv;
483 fd_set fds;
484#if defined( __hpux )
485 size_t maxFD = 0;
486#else
487 int maxFD = 0;
488#endif /* OS-specific brokenness */
489 int bufPos, i, usefulness = 0;
490
491
492 /* Fire up each randomness source */
493 FD_ZERO(&fds);
494 for (i = 0; dataSources[i].path != NULL; i++) {
495 /* Since popen() is a fairly heavy function, we check to see whether
496 * the executable exists before we try to run it */
497 if (access(dataSources[i].path, X_OK)) {
498 if( dbgfp && dbgall )
499 fprintf(dbgfp, "%s not present%s\n", dataSources[i].path,
500 dataSources[i].hasAlternative ?
501 ", has alternatives" : "");
502 dataSources[i].pipe = NULL;
503 }
504 else
505 dataSources[i].pipe = my_popen(&dataSources[i]);
506
507 if (dataSources[i].pipe != NULL) {
508 dataSources[i].pipeFD = fileno(dataSources[i].pipe);
509 if (dataSources[i].pipeFD > maxFD)
510 maxFD = dataSources[i].pipeFD;
511
512#ifdef O_NONBLOCK /* Ohhh what a hack (used for Atari) */
513 fcntl(dataSources[i].pipeFD, F_SETFL, O_NONBLOCK);
514#else
515#error O_NONBLOCK is missing
516#endif
517
518 FD_SET(dataSources[i].pipeFD, &fds);
519 dataSources[i].length = 0;
520
521 /* If there are alternatives for this command, don't try and
522 * execute them */
523 while (dataSources[i].hasAlternative) {
524 if( dbgfp && dbgall )
525 fprintf(dbgfp, "Skipping %s\n", dataSources[i + 1].path);
526 i++;
527 }
528 }
529 }
530
531
532 /* Suck all the data we can get from each of the sources */
533 bufPos = 0;
534 moreSources = 1;
535 while (moreSources && bufPos <= gather_buffer_size) {
536 /* Wait for data to become available from any of the sources, with a
537 * timeout of 10 seconds. This adds even more randomness since data
538 * becomes available in a nondeterministic fashion. Kudos to HP's QA
539 * department for managing to ship a select() which breaks its own
540 * prototype */
541 tv.tv_sec = 10;
542 tv.tv_usec = 0;
543
544#if defined( __hpux ) && ( OS_VERSION == 9 )
545 if (select(maxFD + 1, (int *)&fds, NULL, NULL, &tv) == -1)
546#else /* */
547 if (select(maxFD + 1, &fds, NULL, NULL, &tv) == -1)
548#endif /* __hpux */
549 break;
550
551 /* One of the sources has data available, read it into the buffer */
552 for (i = 0; dataSources[i].path != NULL; i++) {
553 if( dataSources[i].pipe && FD_ISSET(dataSources[i].pipeFD, &fds)) {
554 size_t noBytes;
555
556 if ((noBytes = fread(gather_buffer + bufPos, 1,
557 gather_buffer_size - bufPos,
558 dataSources[i].pipe)) == 0) {
559 if (my_pclose(&dataSources[i]) == 0) {
560 int total = 0;
561
562 /* Try and estimate how much entropy we're getting
563 * from a data source */
564 if (dataSources[i].usefulness) {
565 if (dataSources[i].usefulness < 0)
566 total = (dataSources[i].length + 999)
567 / -dataSources[i].usefulness;
568 else
569 total = dataSources[i].length
570 / dataSources[i].usefulness;
571 }
572 if( dbgfp )
573 fprintf(dbgfp,
574 "%s %s contributed %d bytes, "
575 "usefulness = %d\n", dataSources[i].path,
576 (dataSources[i].arg != NULL) ?
577 dataSources[i].arg : "",
578 dataSources[i].length, total);
579 if( dataSources[i].length )
580 usefulness += total;
581 }
582 dataSources[i].pipe = NULL;
583 }
584 else {
585 int currPos = bufPos;
586 int endPos = bufPos + noBytes;
587
588 /* Run-length compress the input byte sequence */
589 while (currPos < endPos) {
590 int ch = gather_buffer[currPos];
591
592 /* If it's a single byte, just copy it over */
593 if (ch != gather_buffer[currPos + 1]) {
594 gather_buffer[bufPos++] = ch;
595 currPos++;
596 }
597 else {
598 int count = 0;
599
600 /* It's a run of repeated bytes, replace them
601 * with the byte count mod 256 */
602 while ((ch == gather_buffer[currPos])
603 && currPos < endPos) {
604 count++;
605 currPos++;
606 }
607 gather_buffer[bufPos++] = count;
608 noBytes -= count - 1;
609 }
610 }
611
612 /* Remember the number of (compressed) bytes of input we
613 * obtained */
614 dataSources[i].length += noBytes;
615 }
616 }
617 }
618
619 /* Check if there is more input available on any of the sources */
620 moreSources = 0;
621 FD_ZERO(&fds);
622 for (i = 0; dataSources[i].path != NULL; i++) {
623 if (dataSources[i].pipe != NULL) {
624 FD_SET(dataSources[i].pipeFD, &fds);
625 moreSources = 1;
626 }
627 }
628 }
629
630 if( dbgfp ) {
631 fprintf(dbgfp, "Got %d bytes, usefulness = %d\n", bufPos, usefulness);
632 fflush(dbgfp);
633 }
634 *nbytes = bufPos;
635 return usefulness;
636}
637
638/****************
639 * Start the gatherer process which writes messages of
640 * type GATHERER_MSG to pipedes
641 */
642static void
643start_gatherer( int pipefd )
644{
645 FILE *dbgfp = NULL;
646 int dbgall;
647
648 {
649 const char *s = getenv("GNUPG_RNDUNIX_DBG");
650 if( s ) {
651 dbgfp = (*s=='-' && !s[1])? stdout : fopen(s, "a");
652 if( !dbgfp )
653 log_info("can't open debug file `%s': %s\n",
654 s, strerror(errno) );
655 else
656 fprintf(dbgfp,"\nSTART RNDUNIX DEBUG pid=%d\n", (int)getpid());
657 }
658 dbgall = !!getenv("GNUPG_RNDUNIX_DBGALL");
659 }
660 /* close all files but the ones we need */
661 {int nmax, n1, n2, i;
662#ifdef _SC_OPEN_MAX
663 if( (nmax=sysconf( _SC_OPEN_MAX )) < 0 ) {
664#ifdef _POSIX_OPEN_MAX
665 nmax = _POSIX_OPEN_MAX;
666#else
667 nmax = 20; /* assume a reasonable value */
668#endif
669 }
670#else /*!_SC_OPEN_MAX*/
671 nmax = 20; /* assume a reasonable value */
672#endif /*!_SC_OPEN_MAX*/
673 n1 = fileno( stderr );
674 n2 = dbgfp? fileno( dbgfp ) : -1;
675 for(i=0; i < nmax; i++ ) {
676 if( i != n1 && i != n2 && i != pipefd )
677 close(i);
678 }
679 errno = 0;
680 }
681
682
683 /* Set up the buffer */
684 gather_buffer_size = GATHER_BUFSIZE;
685 gather_buffer = malloc( gather_buffer_size );
686 if( !gather_buffer ) {
687 log_error("out of core while allocating the gatherer buffer\n");
688 exit(2);
689 }
690
691 /* Reset the SIGC(H)LD handler to the system default. This is necessary
692 * because if the program which cryptlib is a part of installs its own
693 * SIGC(H)LD handler, it will end up reaping the cryptlib children before
694 * cryptlib can. As a result, my_pclose() will call waitpid() on a
695 * process which has already been reaped by the installed handler and
696 * return an error, so the read data won't be added to the randomness
697 * pool. There are two types of SIGC(H)LD naming, the SysV SIGCLD and
698 * the BSD/Posix SIGCHLD, so we need to handle either possibility */
699#ifdef SIGCLD
700 signal(SIGCLD, SIG_DFL);
701#else
702 signal(SIGCHLD, SIG_DFL);
703#endif
704
705 fclose(stderr); /* Arrghh!! It's Stuart code!! */
706
707 for(;;) {
708 GATHER_MSG msg;
709 size_t nbytes;
710 const char *p;
711
712 msg.usefulness = slow_poll( dbgfp, dbgall, &nbytes );
713 p = gather_buffer;
714 while( nbytes ) {
715 msg.ndata = nbytes > sizeof(msg.data)? sizeof(msg.data) : nbytes;
716 memcpy( msg.data, p, msg.ndata );
717 nbytes -= msg.ndata;
718 p += msg.ndata;
719
720 while( write( pipefd, &msg, sizeof(msg) ) != sizeof(msg) ) {
721 if( errno == EINTR )
722 continue;
723 if( errno == EAGAIN ) {
724 struct timeval tv;
725 tv.tv_sec = 0;
726 tv.tv_usec = 50000;
727 select(0, NULL, NULL, NULL, &tv);
728 continue;
729 }
730 if( errno == EPIPE ) /* parent has exited, so give up */
731 exit(0);
732
733 /* we can't do very much here because stderr is closed */
734 if( dbgfp )
735 fprintf(dbgfp, "gatherer can't write to pipe: %s\n",
736 strerror(errno) );
737 /* we start a new poll to give the system some time */
738 nbytes = 0;
739 break;
740 }
741 }
742 }
743 /* we are killed when the parent dies */
744}
745
746
747static int
748read_a_msg( int fd, GATHER_MSG *msg )
749{
750 char *buffer = (char*)msg;
751 size_t length = sizeof( *msg );
752 int n;
753
754 do {
755 do {
756 n = read(fd, buffer, length );
757 } while( n == -1 && errno == EINTR );
758 if( n == -1 )
759 return -1;
760 buffer += n;
761 length -= n;
762 } while( length );
763 return 0;
764}
765
766
767/****************
768 * Using a level of 0 should never block and better add nothing
769 * to the pool. So this is just a dummy for this gatherer.
770 */
771int
772_gcry_rndunix_gather_random (void (*add)(const void*, size_t, int),
773 int requester,
774 size_t length, int level )
775{
776 static pid_t gatherer_pid = 0;
777 static int pipedes[2];
778 GATHER_MSG msg;
779 size_t n;
780
781 if( !level )
782 return 0;
783
784 if( !gatherer_pid ) {
785 /* make sure we are not setuid */
786 if( getuid() != geteuid() )
787 BUG();
788 /* time to start the gatherer process */
789 if( pipe( pipedes ) ) {
790 log_error("pipe() failed: %s\n", strerror(errno));
791 return -1;
792 }
793 gatherer_pid = fork();
794 if( gatherer_pid == -1 ) {
795 log_error("can't for gatherer process: %s\n", strerror(errno));
796 return -1;
797 }
798 if( !gatherer_pid ) {
799 start_gatherer( pipedes[1] );
800 /* oops, can't happen */
801 return -1;
802 }
803 }
804
805 /* now read from the gatherer */
806 while( length ) {
807 int goodness;
808 ulong subtract;
809
810 if( read_a_msg( pipedes[0], &msg ) ) {
811 log_error("reading from gatherer pipe failed: %s\n",
812 strerror(errno));
813 return -1;
814 }
815
816
817 if( level > 1 ) {
818 if( msg.usefulness > 30 )
819 goodness = 100;
820 else if ( msg.usefulness )
821 goodness = msg.usefulness * 100 / 30;
822 else
823 goodness = 0;
824 }
825 else if( level ) {
826 if( msg.usefulness > 15 )
827 goodness = 100;
828 else if ( msg.usefulness )
829 goodness = msg.usefulness * 100 / 15;
830 else
831 goodness = 0;
832 }
833 else
834 goodness = 100; /* goodness of level 0 is always 100 % */
835
836 n = msg.ndata;
837 if( n > length )
838 n = length;
839 (*add)( msg.data, n, requester );
840
841 /* this is the trick how we cope with the goodness */
842 subtract = (ulong)n * goodness / 100;
843 /* subtract at least 1 byte to avoid infinite loops */
844 length -= subtract ? subtract : 1;
845 }
846
847 return 0;
848}
diff --git a/pwmanager/libcrypt/cipher/rndw32.c b/pwmanager/libcrypt/cipher/rndw32.c
new file mode 100644
index 0000000..870eec6
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/rndw32.c
@@ -0,0 +1,682 @@
1 /* rndw32.c -W32 entropy gatherer
2 *Copyright (C) 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
3 *Copyright Peter Gutmann, Matt Thomlinson and Blake Coverett 1996-1999
4 *
5 * This file is part of Libgcrypt.
6 *
7 *************************************************************************
8 * The code here is based on code from Cryptlib 3.0 beta by Peter Gutmann.
9 * Source file misc/rndwin32.c "Win32 Randomness-Gathering Code" with this
10 * copyright notice:
11 *
12 * This module is part of the cryptlib continuously seeded pseudorandom
13 * number generator. For usage conditions, see lib_rand.c
14 *
15 * [Here is the notice from lib_rand.c, which is now called dev_sys.c]
16 *
17 * This module and the misc/rnd*.c modules represent the cryptlib
18 * continuously seeded pseudorandom number generator (CSPRNG) as described in
19 * my 1998 Usenix Security Symposium paper "The generation of random numbers
20 * for cryptographic purposes".
21 *
22 * The CSPRNG code is copyright Peter Gutmann (and various others) 1996,
23 * 1997, 1998, 1999, all rights reserved. Redistribution of the CSPRNG
24 * modules and use in source and binary forms, with or without modification,
25 * are permitted provided that the following conditions are met:
26 *
27 * 1. Redistributions of source code must retain the above copyright notice
28 * and this permission notice in its entirety.
29 *
30 * 2. Redistributions in binary form must reproduce the copyright notice in
31 * the documentation and/or other materials provided with the distribution.
32 *
33 * 3. A copy of any bugfixes or enhancements made must be provided to the
34 * author, <pgut001@cs.auckland.ac.nz> to allow them to be added to the
35 * baseline version of the code.
36 *
37 * ALTERNATIVELY, the code may be distributed under the terms of the GNU
38 * General Public License, version 2 or any later version published by the
39 * Free Software Foundation, in which case the provisions of the GNU GPL are
40 * required INSTEAD OF the above restrictions.
41 *
42 * Although not required under the terms of the GPL, it would still be nice if
43 * you could make any changes available to the author to allow a consistent
44 * code base to be maintained
45 *************************************************************************
46 */
47
48#include <config.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <assert.h>
52#include <errno.h>
53#include <string.h>
54
55#include <windows.h>
56
57
58#include "types.h"
59#include "g10lib.h"
60#include "rand-internal.h"
61
62
63static int debug_me;
64
65/*
66 * Definitions which are missing from the current GNU Windows32Api
67 */
68
69#ifndef TH32CS_SNAPHEAPLIST
70#define TH32CS_SNAPHEAPLIST 1
71#define TH32CS_SNAPPROCESS 2
72#define TH32CS_SNAPTHREAD 4
73#define TH32CS_SNAPMODULE 8
74 #define TH32CS_SNAPALL (1|2|4|8)
75 #define TH32CS_INHERIT 0x80000000
76#endif /*TH32CS_SNAPHEAPLIST*/
77
78#ifndef IOCTL_DISK_PERFORMANCE
79 #define IOCTL_DISK_PERFORMANCE0x00070020
80#endif
81#ifndef VER_PLATFORM_WIN32_WINDOWS
82#define VER_PLATFORM_WIN32_WINDOWS 1
83#endif
84
85/* This used to be (6*8+5*4+8*2), but Peter Gutmann figured a larger
86 value in a newer release. So we use a far larger value. */
87#define SIZEOF_DISK_PERFORMANCE_STRUCT 256
88
89
90typedef struct {
91 DWORD dwSize;
92 DWORD th32ProcessID;
93 DWORD th32HeapID;
94 DWORD dwFlags;
95} HEAPLIST32;
96
97typedef struct {
98 DWORD dwSize;
99 HANDLE hHandle;
100 DWORD dwAddress;
101 DWORD dwBlockSize;
102 DWORD dwFlags;
103 DWORD dwLockCount;
104 DWORD dwResvd;
105 DWORD th32ProcessID;
106 DWORD th32HeapID;
107} HEAPENTRY32;
108
109typedef struct {
110 DWORD dwSize;
111 DWORD cntUsage;
112 DWORD th32ProcessID;
113 DWORD th32DefaultHeapID;
114 DWORD th32ModuleID;
115 DWORD cntThreads;
116 DWORD th32ParentProcessID;
117 LONG pcPriClassBase;
118 DWORD dwFlags;
119 char szExeFile[260];
120} PROCESSENTRY32;
121
122typedef struct {
123 DWORD dwSize;
124 DWORD cntUsage;
125 DWORD th32ThreadID;
126 DWORD th32OwnerProcessID;
127 LONG tpBasePri;
128 LONG tpDeltaPri;
129 DWORD dwFlags;
130} THREADENTRY32;
131
132typedef struct {
133 DWORD dwSize;
134 DWORD th32ModuleID;
135 DWORD th32ProcessID;
136 DWORD GlblcntUsage;
137 DWORD ProccntUsage;
138 BYTE *modBaseAddr;
139 DWORD modBaseSize;
140 HMODULE hModule;
141 char szModule[256];
142 char szExePath[260];
143} MODULEENTRY32;
144
145
146
147/* Type definitions for function pointers to call Toolhelp32 functions
148 * used with the windows95 gatherer */
149typedef BOOL (WINAPI * MODULEWALK) (HANDLE hSnapshot, MODULEENTRY32 *lpme);
150typedef BOOL (WINAPI * THREADWALK) (HANDLE hSnapshot, THREADENTRY32 *lpte);
151typedef BOOL (WINAPI * PROCESSWALK) (HANDLE hSnapshot, PROCESSENTRY32 *lppe);
152typedef BOOL (WINAPI * HEAPLISTWALK) (HANDLE hSnapshot, HEAPLIST32 *lphl);
153typedef BOOL (WINAPI * HEAPFIRST) (HEAPENTRY32 *lphe, DWORD th32ProcessID,
154 DWORD th32HeapID);
155typedef BOOL (WINAPI * HEAPNEXT) (HEAPENTRY32 *lphe);
156typedef HANDLE (WINAPI * CREATESNAPSHOT) (DWORD dwFlags, DWORD th32ProcessID);
157
158/* Type definitions for function pointers to call NetAPI32 functions */
159typedef DWORD (WINAPI * NETSTATISTICSGET) (LPWSTR szServer, LPWSTR szService,
160 DWORD dwLevel, DWORD dwOptions,
161 LPBYTE * lpBuffer);
162typedef DWORD (WINAPI * NETAPIBUFFERSIZE) (LPVOID lpBuffer, LPDWORD cbBuffer);
163typedef DWORD (WINAPI * NETAPIBUFFERFREE) (LPVOID lpBuffer);
164
165
166/* When we query the performance counters, we allocate an initial buffer and
167 * then reallocate it as required until RegQueryValueEx() stops returning
168 * ERROR_MORE_DATA. The following values define the initial buffer size and
169 * step size by which the buffer is increased
170 */
171 #define PERFORMANCE_BUFFER_SIZE 65536/* Start at 64K */
172 #define PERFORMANCE_BUFFER_STEP 16384/* Step by 16K */
173
174
175static void
176slow_gatherer_windows95( void (*add)(const void*, size_t, int), int requester )
177{
178 static CREATESNAPSHOT pCreateToolhelp32Snapshot = NULL;
179 static MODULEWALK pModule32First = NULL;
180 static MODULEWALK pModule32Next = NULL;
181 static PROCESSWALK pProcess32First = NULL;
182 static PROCESSWALK pProcess32Next = NULL;
183 static THREADWALK pThread32First = NULL;
184 static THREADWALK pThread32Next = NULL;
185 static HEAPLISTWALK pHeap32ListFirst = NULL;
186 static HEAPLISTWALK pHeap32ListNext = NULL;
187 static HEAPFIRST pHeap32First = NULL;
188 static HEAPNEXT pHeap32Next = NULL;
189 HANDLE hSnapshot;
190
191
192 /* initialize the Toolhelp32 function pointers */
193 if ( !pCreateToolhelp32Snapshot ) {
194 HANDLE hKernel;
195
196 if ( debug_me )
197 log_debug ("rndw32#slow_gatherer_95: init toolkit\n" );
198
199 /* Obtain the module handle of the kernel to retrieve the addresses
200 * of the Toolhelp32 functions */
201 if ( ( !(hKernel = GetModuleHandle ("KERNEL32.DLL"))) ) {
202 log_fatal ( "rndw32: can't get module handle\n" );
203 }
204
205 /* Now get pointers to the functions */
206 pCreateToolhelp32Snapshot = (CREATESNAPSHOT) GetProcAddress (hKernel,
207 "CreateToolhelp32Snapshot");
208 pModule32First = (MODULEWALK) GetProcAddress (hKernel, "Module32First");
209 pModule32Next = (MODULEWALK) GetProcAddress (hKernel, "Module32Next");
210 pProcess32First = (PROCESSWALK) GetProcAddress (hKernel,
211 "Process32First");
212 pProcess32Next = (PROCESSWALK) GetProcAddress (hKernel,
213 "Process32Next");
214 pThread32First = (THREADWALK) GetProcAddress (hKernel, "Thread32First");
215 pThread32Next = (THREADWALK) GetProcAddress (hKernel, "Thread32Next");
216 pHeap32ListFirst = (HEAPLISTWALK) GetProcAddress (hKernel,
217 "Heap32ListFirst");
218 pHeap32ListNext = (HEAPLISTWALK) GetProcAddress (hKernel,
219 "Heap32ListNext");
220 pHeap32First = (HEAPFIRST) GetProcAddress (hKernel, "Heap32First");
221 pHeap32Next = (HEAPNEXT) GetProcAddress (hKernel, "Heap32Next");
222
223 if (!pCreateToolhelp32Snapshot
224 || !pModule32First || !pModule32Next
225 || !pProcess32First || !pProcess32Next
226 || !pThread32First || !pThread32Next
227 || !pHeap32ListFirst || !pHeap32ListNext
228 || !pHeap32First || !pHeap32Next ) {
229 log_fatal ( "rndw32: failed to get a toolhelp function\n" );
230 }
231 }
232
233 /* Take a snapshot of everything we can get to which is currently
234 *in the system */
235 if ( !(hSnapshot = pCreateToolhelp32Snapshot (TH32CS_SNAPALL, 0)) ) {
236 log_fatal ( "rndw32: failed to take a toolhelp snapshot\n" );
237 }
238
239 /* Walk through the local heap */
240 {HEAPLIST32 hl32;
241 hl32.dwSize = sizeof (HEAPLIST32);
242 if (pHeap32ListFirst (hSnapshot, &hl32)) {
243 if ( debug_me )
244 log_debug ("rndw32#slow_gatherer_95: walk heap\n" );
245 do {
246 HEAPENTRY32 he32;
247
248 /* First add the information from the basic Heaplist32 struct */
249 (*add) ( &hl32, sizeof (hl32), requester );
250
251 /* Now walk through the heap blocks getting information
252 * on each of them */
253 he32.dwSize = sizeof (HEAPENTRY32);
254 if (pHeap32First (&he32, hl32.th32ProcessID, hl32.th32HeapID)){
255 do {
256 (*add) ( &he32, sizeof (he32), requester );
257 } while (pHeap32Next (&he32));
258 }
259 } while (pHeap32ListNext (hSnapshot, &hl32));
260 }
261 }
262
263
264 /* Walk through all processes */
265 {PROCESSENTRY32 pe32;
266 pe32.dwSize = sizeof (PROCESSENTRY32);
267 if (pProcess32First (hSnapshot, &pe32)) {
268 if ( debug_me )
269 log_debug ("rndw32#slow_gatherer_95: walk processes\n" );
270 do {
271 (*add) ( &pe32, sizeof (pe32), requester );
272 } while (pProcess32Next (hSnapshot, &pe32));
273 }
274 }
275
276 /* Walk through all threads */
277 {THREADENTRY32 te32;
278 te32.dwSize = sizeof (THREADENTRY32);
279 if (pThread32First (hSnapshot, &te32)) {
280 if ( debug_me )
281 log_debug ("rndw32#slow_gatherer_95: walk threads\n" );
282 do {
283 (*add) ( &te32, sizeof (te32), requester );
284 } while (pThread32Next (hSnapshot, &te32));
285 }
286 }
287
288 /* Walk through all modules associated with the process */
289 {MODULEENTRY32 me32;
290 me32.dwSize = sizeof (MODULEENTRY32);
291 if (pModule32First (hSnapshot, &me32)) {
292 if ( debug_me )
293 log_debug ("rndw32#slow_gatherer_95: walk modules\n" );
294 do {
295 (*add) ( &me32, sizeof (me32), requester );
296 } while (pModule32Next (hSnapshot, &me32));
297 }
298 }
299
300 CloseHandle (hSnapshot);
301}
302
303
304
305static void
306slow_gatherer_windowsNT( void (*add)(const void*, size_t, int), int requester )
307{
308 static int is_initialized = 0;
309 static NETSTATISTICSGET pNetStatisticsGet = NULL;
310 static NETAPIBUFFERSIZE pNetApiBufferSize = NULL;
311 static NETAPIBUFFERFREE pNetApiBufferFree = NULL;
312 static int is_workstation = 1;
313
314 static int cbPerfData = PERFORMANCE_BUFFER_SIZE;
315 PERF_DATA_BLOCK *pPerfData;
316 HANDLE hDevice, hNetAPI32 = NULL;
317 DWORD dwSize, status;
318 int nDrive;
319
320 if ( !is_initialized ) {
321 HKEY hKey;
322
323 if ( debug_me )
324 log_debug ("rndw32#slow_gatherer_nt: init toolkit\n" );
325 /* Find out whether this is an NT server or workstation if necessary */
326 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE,
327 "SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
328 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
329 BYTE szValue[32];
330 dwSize = sizeof (szValue);
331
332 if ( debug_me )
333 log_debug ("rndw32#slow_gatherer_nt: check product options\n" );
334 status = RegQueryValueEx (hKey, "ProductType", 0, NULL,
335 szValue, &dwSize);
336 if (status == ERROR_SUCCESS && stricmp (szValue, "WinNT")) {
337 /* Note: There are (at least) three cases for ProductType:
338 * WinNT = NT Workstation, ServerNT = NT Server, LanmanNT =
339 * NT Server acting as a Domain Controller */
340 is_workstation = 0;
341 if ( debug_me )
342 log_debug ("rndw32: this is a NT server\n");
343 }
344 RegCloseKey (hKey);
345 }
346
347 /* Initialize the NetAPI32 function pointers if necessary */
348 if ( (hNetAPI32 = LoadLibrary ("NETAPI32.DLL")) ) {
349 if ( debug_me )
350 log_debug ("rndw32#slow_gatherer_nt: netapi32 loaded\n" );
351 pNetStatisticsGet = (NETSTATISTICSGET) GetProcAddress (hNetAPI32,
352 "NetStatisticsGet");
353 pNetApiBufferSize = (NETAPIBUFFERSIZE) GetProcAddress (hNetAPI32,
354 "NetApiBufferSize");
355 pNetApiBufferFree = (NETAPIBUFFERFREE) GetProcAddress (hNetAPI32,
356 "NetApiBufferFree");
357
358 if ( !pNetStatisticsGet
359 || !pNetApiBufferSize || !pNetApiBufferFree ) {
360 FreeLibrary (hNetAPI32);
361 hNetAPI32 = NULL;
362 log_debug ("rndw32: No NETAPI found\n" );
363 }
364 }
365
366 is_initialized = 1;
367 }
368
369 /* Get network statistics.Note: Both NT Workstation and NT Server by
370 * default will be running both the workstation and server services. The
371 * heuristic below is probably useful though on the assumption that the
372 * majority of the network traffic will be via the appropriate service.
373 * In any case the network statistics return almost no randomness */
374 {LPBYTE lpBuffer;
375 if (hNetAPI32 && !pNetStatisticsGet (NULL,
376 is_workstation ? L"LanmanWorkstation" :
377 L"LanmanServer", 0, 0, &lpBuffer) ) {
378 if ( debug_me )
379 log_debug ("rndw32#slow_gatherer_nt: get netstats\n" );
380 pNetApiBufferSize (lpBuffer, &dwSize);
381 (*add) ( lpBuffer, dwSize,requester );
382 pNetApiBufferFree (lpBuffer);
383 }
384 }
385
386 /* Get disk I/O statistics for all the hard drives */
387 for (nDrive = 0;; nDrive++) {
388 char diskPerformance[SIZEOF_DISK_PERFORMANCE_STRUCT];
389 char szDevice[50];
390
391 /* Check whether we can access this device */
392 sprintf (szDevice, "\\\\.\\PhysicalDrive%d", nDrive);
393 hDevice = CreateFile (szDevice, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
394 NULL, OPEN_EXISTING, 0, NULL);
395 if (hDevice == INVALID_HANDLE_VALUE)
396 break;
397
398 /* Note: This only works if you have turned on the disk performance
399 * counters with 'diskperf -y'. These counters are off by default */
400 if (DeviceIoControl (hDevice, IOCTL_DISK_PERFORMANCE, NULL, 0,
401 &diskPerformance, SIZEOF_DISK_PERFORMANCE_STRUCT,
402 &dwSize, NULL))
403 {
404 if ( debug_me )
405 log_debug ("rndw32#slow_gatherer_nt: iostats drive %d\n",
406 nDrive );
407 (*add) (diskPerformance, dwSize, requester );
408 }
409 else {
410 log_info ("NOTE: you should run 'diskperf -y' "
411 "to enable the disk statistics\n");
412 }
413 CloseHandle (hDevice);
414 }
415
416#if 0 /* we don't need this in GnuPG */
417 /* Wait for any async keyset driver binding to complete. You may be
418 * wondering what this call is doing here... the reason it's necessary is
419 * because RegQueryValueEx() will hang indefinitely if the async driver
420 * bind is in progress. The problem occurs in the dynamic loading and
421 * linking of driver DLL's, which work as follows:
422 *
423 * hDriver = LoadLibrary( DRIVERNAME );
424 * pFunction1 = ( TYPE_FUNC1 ) GetProcAddress( hDriver, NAME_FUNC1 );
425 * pFunction2 = ( TYPE_FUNC1 ) GetProcAddress( hDriver, NAME_FUNC2 );
426 *
427 * If RegQueryValueEx() is called while the GetProcAddress()'s are in
428 * progress, it will hang indefinitely. This is probably due to some
429 * synchronisation problem in the NT kernel where the GetProcAddress()
430 * calls affect something like a module reference count or function
431 * reference count while RegQueryValueEx() is trying to take a snapshot
432 * of the statistics, which include the reference counts. Because of
433 * this, we have to wait until any async driver bind has completed
434 * before we can call RegQueryValueEx() */
435 waitSemaphore (SEMAPHORE_DRIVERBIND);
436#endif
437
438 /* Get information from the system performance counters. This can take
439 * a few seconds to do. In some environments the call to
440 * RegQueryValueEx() can produce an access violation at some random time
441 * in the future, adding a short delay after the following code block
442 * makes the problem go away. This problem is extremely difficult to
443 * reproduce, I haven't been able to get it to occur despite running it
444 * on a number of machines. The best explanation for the problem is that
445 * on the machine where it did occur, it was caused by an external driver
446 * or other program which adds its own values under the
447 * HKEY_PERFORMANCE_DATA key. The NT kernel calls the required external
448 * modules to map in the data, if there's a synchronisation problem the
449 * external module would write its data at an inappropriate moment,
450 * causing the access violation. A low-level memory checker indicated
451 * that ExpandEnvironmentStrings() in KERNEL32.DLL, called an
452 * interminable number of calls down inside RegQueryValueEx(), was
453 * overwriting memory (it wrote twice the allocated size of a buffer to a
454 * buffer allocated by the NT kernel). This may be what's causing the
455 * problem, but since it's in the kernel there isn't much which can be
456 * done.
457 *
458 * In addition to these problems the code in RegQueryValueEx() which
459 * estimates the amount of memory required to return the performance
460 * counter information isn't very accurate, since it always returns a
461 * worst-case estimate which is usually nowhere near the actual amount
462 * required. For example it may report that 128K of memory is required,
463 * but only return 64K of data */
464 {pPerfData = gcry_xmalloc (cbPerfData);
465 for (;;) {
466 dwSize = cbPerfData;
467 if ( debug_me )
468 log_debug ("rndw32#slow_gatherer_nt: get perf data\n" );
469 status = RegQueryValueEx (HKEY_PERFORMANCE_DATA, "Global", NULL,
470 NULL, (LPBYTE) pPerfData, &dwSize);
471 if (status == ERROR_SUCCESS) {
472 if (!memcmp (pPerfData->Signature, L"PERF", 8)) {
473 (*add) ( pPerfData, dwSize, requester );
474 }
475 else
476 log_debug ( "rndw32: no PERF signature\n");
477 break;
478 }
479 else if (status == ERROR_MORE_DATA) {
480 cbPerfData += PERFORMANCE_BUFFER_STEP;
481 pPerfData = gcry_realloc (pPerfData, cbPerfData);
482 }
483 else {
484 log_debug ( "rndw32: get performance data problem\n");
485 break;
486 }
487 }
488 gcry_free (pPerfData);
489 }
490 /* Although this isn't documented in the Win32 API docs, it's necessary
491 to explicitly close the HKEY_PERFORMANCE_DATA key after use (it's
492 implicitly opened on the first call to RegQueryValueEx()). If this
493 isn't done then any system components which provide performance data
494 can't be removed or changed while the handle remains active */
495 RegCloseKey (HKEY_PERFORMANCE_DATA);
496}
497
498
499int
500_gcry_rndw32_gather_random( void (*add)(const void*, size_t, int),
501 int requester,
502 size_t length, int level )
503{
504 static int is_initialized;
505 static int is_windowsNT, has_toolhelp;
506
507
508 if( !level )
509 return 0;
510 /* We don't differentiate between level 1 and 2 here because
511 * there is no internal entropy pool as a scary resource. It may
512 * all work slower, but because our entropy source will never
513 * block but deliver some not easy to measure entropy, we assume level 2
514 */
515
516
517 if ( !is_initialized ) {
518 OSVERSIONINFO osvi = { sizeof( osvi ) };
519 DWORD platform;
520
521 GetVersionEx( &osvi );
522 platform = osvi.dwPlatformId;
523 is_windowsNT = platform == VER_PLATFORM_WIN32_NT;
524 has_toolhelp = (platform == VER_PLATFORM_WIN32_WINDOWS
525 || (is_windowsNT && osvi.dwMajorVersion >= 5));
526
527 if ( platform == VER_PLATFORM_WIN32s ) {
528 log_fatal("can't run on a W32s platform\n" );
529 }
530 is_initialized = 1;
531 if ( debug_me )
532 log_debug ("rndw32#gather_random: platform=%d\n", (int)platform );
533 }
534
535
536 if ( debug_me )
537 log_debug ("rndw32#gather_random: req=%d len=%u lvl=%d\n",
538 requester, (unsigned int)length, level );
539
540 if ( has_toolhelp ) {
541 slow_gatherer_windows95 ( add, requester );
542 }
543 if ( is_windowsNT ) {
544 slow_gatherer_windowsNT ( add, requester );
545 }
546
547 return 0;
548}
549
550
551int
552_gcry_rndw32_gather_random_fast (void (*add)(const void*, size_t, int),
553 int requester )
554{
555 static int addedFixedItems = 0;
556
557 if ( debug_me )
558 log_debug ("rndw32#gather_random_fast: req=%d\n", requester );
559
560 /* Get various basic pieces of system information: Handle of active
561 * window, handle of window with mouse capture, handle of clipboard owner
562 * handle of start of clpboard viewer list, pseudohandle of current
563 * process, current process ID, pseudohandle of current thread, current
564 * thread ID, handle of desktop window, handle of window with keyboard
565 * focus, whether system queue has any events, cursor position for last
566 * message, 1 ms time for last message, handle of window with clipboard
567 * open, handle of process heap, handle of procs window station, types of
568 * events in input queue, and milliseconds since Windows was started */
569 {byte buffer[20*sizeof(ulong)], *bufptr;
570 bufptr = buffer;
571 #define ADD(f) do { ulong along = (ulong)(f); \
572 memcpy (bufptr, &along, sizeof (along) ); \
573 bufptr += sizeof (along); } while (0)
574 ADD ( GetActiveWindow ());
575 ADD ( GetCapture ());
576 ADD ( GetClipboardOwner ());
577 ADD ( GetClipboardViewer ());
578 ADD ( GetCurrentProcess ());
579 ADD ( GetCurrentProcessId ());
580 ADD ( GetCurrentThread ());
581 ADD ( GetCurrentThreadId ());
582 ADD ( GetDesktopWindow ());
583 ADD ( GetFocus ());
584 ADD ( GetInputState ());
585 ADD ( GetMessagePos ());
586 ADD ( GetMessageTime ());
587 ADD ( GetOpenClipboardWindow ());
588 ADD ( GetProcessHeap ());
589 ADD ( GetProcessWindowStation ());
590 ADD ( GetQueueStatus (QS_ALLEVENTS));
591 ADD ( GetTickCount ());
592
593 assert ( bufptr-buffer < sizeof (buffer) );
594 (*add) ( buffer, bufptr-buffer, requester );
595#undef ADD
596 }
597
598 /* Get multiword system information: Current caret position, current
599 * mouse cursor position */
600 {POINT point;
601 GetCaretPos (&point);
602 (*add) ( &point, sizeof (point), requester );
603 GetCursorPos (&point);
604 (*add) ( &point, sizeof (point), requester );
605 }
606
607 /* Get percent of memory in use, bytes of physical memory, bytes of free
608 * physical memory, bytes in paging file, free bytes in paging file, user
609 * bytes of address space, and free user bytes */
610 {MEMORYSTATUS memoryStatus;
611 memoryStatus.dwLength = sizeof (MEMORYSTATUS);
612 GlobalMemoryStatus (&memoryStatus);
613 (*add) ( &memoryStatus, sizeof (memoryStatus), requester );
614 }
615
616 /* Get thread and process creation time, exit time, time in kernel mode,
617 and time in user mode in 100ns intervals */
618 {HANDLE handle;
619 FILETIME creationTime, exitTime, kernelTime, userTime;
620 DWORD minimumWorkingSetSize, maximumWorkingSetSize;
621
622 handle = GetCurrentThread ();
623 GetThreadTimes (handle, &creationTime, &exitTime,
624 &kernelTime, &userTime);
625 (*add) ( &creationTime, sizeof (creationTime), requester );
626 (*add) ( &exitTime, sizeof (exitTime), requester );
627 (*add) ( &kernelTime, sizeof (kernelTime), requester );
628 (*add) ( &userTime, sizeof (userTime), requester );
629
630 handle = GetCurrentProcess ();
631 GetProcessTimes (handle, &creationTime, &exitTime,
632 &kernelTime, &userTime);
633 (*add) ( &creationTime, sizeof (creationTime), requester );
634 (*add) ( &exitTime, sizeof (exitTime), requester );
635 (*add) ( &kernelTime, sizeof (kernelTime), requester );
636 (*add) ( &userTime, sizeof (userTime), requester );
637
638 /* Get the minimum and maximum working set size for the current process */
639 GetProcessWorkingSetSize (handle, &minimumWorkingSetSize,
640 &maximumWorkingSetSize);
641 (*add) ( &minimumWorkingSetSize,
642 sizeof (&minimumWorkingSetSize), requester );
643 (*add) ( &maximumWorkingSetSize,
644 sizeof (&maximumWorkingSetSize), requester );
645 }
646
647
648 /* The following are fixed for the lifetime of the process so we only
649 * add them once */
650 if (!addedFixedItems) {
651 STARTUPINFO startupInfo;
652
653 /* Get name of desktop, console window title, new window position and
654 * size, window flags, and handles for stdin, stdout, and stderr */
655 startupInfo.cb = sizeof (STARTUPINFO);
656 GetStartupInfo (&startupInfo);
657 (*add) ( &startupInfo, sizeof (STARTUPINFO), requester );
658 addedFixedItems = 1;
659 }
660
661 /* The performance of QPC varies depending on the architecture it's
662 * running on and on the OS. Under NT it reads the CPU's 64-bit timestamp
663 * counter (at least on a Pentium and newer '486's, it hasn't been tested
664 * on anything without a TSC), under Win95 it reads the 1.193180 MHz PIC
665 * timer. There are vague mumblings in the docs that it may fail if the
666 * appropriate hardware isn't available (possibly '386's or MIPS machines
667 * running NT), but who's going to run NT on a '386? */
668 {LARGE_INTEGER performanceCount;
669 if (QueryPerformanceCounter (&performanceCount)) {
670 if ( debug_me )
671 log_debug ("rndw32#gather_random_fast: perf data\n");
672 (*add) (&performanceCount, sizeof (&performanceCount), requester);
673 }
674 else { /* Millisecond accuracy at best... */
675 DWORD aword = GetTickCount ();
676 (*add) (&aword, sizeof (aword), requester );
677 }
678 }
679
680 return 0;
681}
682
diff --git a/pwmanager/libcrypt/cipher/rsa.c b/pwmanager/libcrypt/cipher/rsa.c
new file mode 100644
index 0000000..fa26622
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/rsa.c
@@ -0,0 +1,630 @@
1/* rsa.c - RSA function
2 *Copyright (C) 1997, 1998, 1999 by Werner Koch (dd9jn)
3 *Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 */
21
22/* This code uses an algorithm protected by U.S. Patent #4,405,829
23 which expired on September 20, 2000. The patent holder placed that
24 patent into the public domain on Sep 6th, 2000.
25*/
26
27#include <config.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include "g10lib.h"
32#include "mpi.h"
33#include "cipher.h"
34
35
36typedef struct
37{
38 gcry_mpi_t n; /* modulus */
39 gcry_mpi_t e; /* exponent */
40} RSA_public_key;
41
42
43typedef struct
44{
45 gcry_mpi_t n; /* public modulus */
46 gcry_mpi_t e; /* public exponent */
47 gcry_mpi_t d; /* exponent */
48 gcry_mpi_t p; /* prime p. */
49 gcry_mpi_t q; /* prime q. */
50 gcry_mpi_t u; /* inverse of p mod q. */
51} RSA_secret_key;
52
53
54static void test_keys (RSA_secret_key *sk, unsigned nbits);
55static void generate (RSA_secret_key *sk,
56 unsigned int nbits, unsigned long use_e);
57static int check_secret_key (RSA_secret_key *sk);
58static void public (gcry_mpi_t output, gcry_mpi_t input, RSA_public_key *skey);
59static void secret (gcry_mpi_t output, gcry_mpi_t input, RSA_secret_key *skey);
60
61
62static void
63test_keys( RSA_secret_key *sk, unsigned nbits )
64{
65 RSA_public_key pk;
66 gcry_mpi_t test = gcry_mpi_new ( nbits );
67 gcry_mpi_t out1 = gcry_mpi_new ( nbits );
68 gcry_mpi_t out2 = gcry_mpi_new ( nbits );
69
70 pk.n = sk->n;
71 pk.e = sk->e;
72 gcry_mpi_randomize( test, nbits, GCRY_WEAK_RANDOM );
73
74 public( out1, test, &pk );
75 secret( out2, out1, sk );
76 if( mpi_cmp( test, out2 ) )
77 log_fatal("RSA operation: public, secret failed\n");
78 secret( out1, test, sk );
79 public( out2, out1, &pk );
80 if( mpi_cmp( test, out2 ) )
81 log_fatal("RSA operation: secret, public failed\n");
82 gcry_mpi_release ( test );
83 gcry_mpi_release ( out1 );
84 gcry_mpi_release ( out2 );
85}
86
87
88/* Callback used by the prime generation to test whether the exponent
89 is suitable. Returns 0 if the test has been passed. */
90static int
91check_exponent (void *arg, gcry_mpi_t a)
92{
93 gcry_mpi_t e = arg;
94 gcry_mpi_t tmp;
95 int result;
96
97 mpi_sub_ui (a, a, 1);
98 tmp = _gcry_mpi_alloc_like (a);
99 result = !gcry_mpi_gcd(tmp, e, a); /* GCD is not 1. */
100 gcry_mpi_release (tmp);
101 mpi_add_ui (a, a, 1);
102 return result;
103}
104
105/****************
106 * Generate a key pair with a key of size NBITS.
107 * USE_E = 0 let Libcgrypt decide what exponent to use.
108 * = 1 request the use of a "secure" exponent; this is required by some
109 * specification to be 65537.
110 * > 2 Try starting at this value until a working exponent is found.
111 * Returns: 2 structures filled with all needed values
112 */
113static void
114generate (RSA_secret_key *sk, unsigned int nbits, unsigned long use_e)
115{
116 gcry_mpi_t p, q; /* the two primes */
117 gcry_mpi_t d; /* the private key */
118 gcry_mpi_t u;
119 gcry_mpi_t t1, t2;
120 gcry_mpi_t n; /* the public key */
121 gcry_mpi_t e; /* the exponent */
122 gcry_mpi_t phi; /* helper: (p-1)(q-1) */
123 gcry_mpi_t g;
124 gcry_mpi_t f;
125
126 /* make sure that nbits is even so that we generate p, q of equal size */
127 if ( (nbits&1) )
128 nbits++;
129
130 if (use_e == 1) /* Alias for a secure value. */
131 use_e = 65537; /* as demanded by Spinx. */
132
133 /* Public exponent:
134 In general we use 41 as this is quite fast and more secure than the
135 commonly used 17. Benchmarking the RSA verify function
136 with a 1024 bit key yields (2001-11-08):
137 e=17 0.54 ms
138 e=41 0.75 ms
139 e=257 0.95 ms
140 e=65537 1.80 ms
141 */
142 e = mpi_alloc( (32+BITS_PER_MPI_LIMB-1)/BITS_PER_MPI_LIMB );
143 if (!use_e)
144 mpi_set_ui (e, 41); /* This is a reasonable secure and fast value */
145 else
146 {
147 use_e |= 1; /* make sure this is odd */
148 mpi_set_ui (e, use_e);
149 }
150
151 n = gcry_mpi_new (nbits);
152
153 p = q = NULL;
154 do
155 {
156 /* select two (very secret) primes */
157 if (p)
158 gcry_mpi_release (p);
159 if (q)
160 gcry_mpi_release (q);
161 if (use_e)
162 { /* Do an extra test to ensure that the given exponent is
163 suitable. */
164 p = _gcry_generate_secret_prime (nbits/2, check_exponent, e);
165 q = _gcry_generate_secret_prime (nbits/2, check_exponent, e);
166 }
167 else
168 { /* We check the exponent later. */
169 p = _gcry_generate_secret_prime (nbits/2, NULL, NULL);
170 q = _gcry_generate_secret_prime (nbits/2, NULL, NULL);
171 }
172 if (mpi_cmp (p, q) > 0 ) /* p shall be smaller than q (for calc of u)*/
173 mpi_swap(p,q);
174 /* calculate the modulus */
175 mpi_mul( n, p, q );
176 }
177 while ( mpi_get_nbits(n) != nbits );
178
179 /* calculate Euler totient: phi = (p-1)(q-1) */
180 t1 = mpi_alloc_secure( mpi_get_nlimbs(p) );
181 t2 = mpi_alloc_secure( mpi_get_nlimbs(p) );
182 phi = gcry_mpi_snew ( nbits );
183 g= gcry_mpi_snew ( nbits );
184 f= gcry_mpi_snew ( nbits );
185 mpi_sub_ui( t1, p, 1 );
186 mpi_sub_ui( t2, q, 1 );
187 mpi_mul( phi, t1, t2 );
188 gcry_mpi_gcd(g, t1, t2);
189 mpi_fdiv_q(f, phi, g);
190
191 while (!gcry_mpi_gcd(t1, e, phi)) /* (while gcd is not 1) */
192 {
193 if (use_e)
194 BUG (); /* The prime generator already made sure that we
195 never can get to here. */
196 mpi_add_ui (e, e, 2);
197 }
198
199 /* calculate the secret key d = e^1 mod phi */
200 d = gcry_mpi_snew ( nbits );
201 mpi_invm(d, e, f );
202 /* calculate the inverse of p and q (used for chinese remainder theorem)*/
203 u = gcry_mpi_snew ( nbits );
204 mpi_invm(u, p, q );
205
206 if( DBG_CIPHER )
207 {
208 log_mpidump(" p= ", p );
209 log_mpidump(" q= ", q );
210 log_mpidump("phi= ", phi );
211 log_mpidump(" g= ", g );
212 log_mpidump(" f= ", f );
213 log_mpidump(" n= ", n );
214 log_mpidump(" e= ", e );
215 log_mpidump(" d= ", d );
216 log_mpidump(" u= ", u );
217 }
218
219 gcry_mpi_release (t1);
220 gcry_mpi_release (t2);
221 gcry_mpi_release (phi);
222 gcry_mpi_release (f);
223 gcry_mpi_release (g);
224
225 sk->n = n;
226 sk->e = e;
227 sk->p = p;
228 sk->q = q;
229 sk->d = d;
230 sk->u = u;
231
232 /* now we can test our keys (this should never fail!) */
233 test_keys( sk, nbits - 64 );
234}
235
236
237/****************
238 * Test wether the secret key is valid.
239 * Returns: true if this is a valid key.
240 */
241static int
242check_secret_key( RSA_secret_key *sk )
243{
244 int rc;
245 gcry_mpi_t temp = mpi_alloc( mpi_get_nlimbs(sk->p)*2 );
246
247 mpi_mul(temp, sk->p, sk->q );
248 rc = mpi_cmp( temp, sk->n );
249 mpi_free(temp);
250 return !rc;
251}
252
253
254
255/****************
256 * Public key operation. Encrypt INPUT with PKEY and put result into OUTPUT.
257 *
258 *c = m^e mod n
259 *
260 * Where c is OUTPUT, m is INPUT and e,n are elements of PKEY.
261 */
262static void
263public(gcry_mpi_t output, gcry_mpi_t input, RSA_public_key *pkey )
264{
265 if( output == input ) /* powm doesn't like output and input the same */
266 {
267 gcry_mpi_t x = mpi_alloc( mpi_get_nlimbs(input)*2 );
268 mpi_powm( x, input, pkey->e, pkey->n );
269 mpi_set(output, x);
270 mpi_free(x);
271 }
272 else
273 mpi_powm( output, input, pkey->e, pkey->n );
274}
275
276#if 0
277static void
278stronger_key_check ( RSA_secret_key *skey )
279{
280 gcry_mpi_t t = mpi_alloc_secure ( 0 );
281 gcry_mpi_t t1 = mpi_alloc_secure ( 0 );
282 gcry_mpi_t t2 = mpi_alloc_secure ( 0 );
283 gcry_mpi_t phi = mpi_alloc_secure ( 0 );
284
285 /* check that n == p * q */
286 mpi_mul( t, skey->p, skey->q);
287 if (mpi_cmp( t, skey->n) )
288 log_info ( "RSA Oops: n != p * q\n" );
289
290 /* check that p is less than q */
291 if( mpi_cmp( skey->p, skey->q ) > 0 )
292 {
293 log_info ("RSA Oops: p >= q - fixed\n");
294 _gcry_mpi_swap ( skey->p, skey->q);
295 }
296
297 /* check that e divides neither p-1 nor q-1 */
298 mpi_sub_ui(t, skey->p, 1 );
299 mpi_fdiv_r(t, t, skey->e );
300 if ( !mpi_cmp_ui( t, 0) )
301 log_info ( "RSA Oops: e divides p-1\n" );
302 mpi_sub_ui(t, skey->q, 1 );
303 mpi_fdiv_r(t, t, skey->e );
304 if ( !mpi_cmp_ui( t, 0) )
305 log_info ( "RSA Oops: e divides q-1\n" );
306
307 /* check that d is correct */
308 mpi_sub_ui( t1, skey->p, 1 );
309 mpi_sub_ui( t2, skey->q, 1 );
310 mpi_mul( phi, t1, t2 );
311 gcry_mpi_gcd(t, t1, t2);
312 mpi_fdiv_q(t, phi, t);
313 mpi_invm(t, skey->e, t );
314 if ( mpi_cmp(t, skey->d ) )
315 {
316 log_info ( "RSA Oops: d is wrong - fixed\n");
317 mpi_set (skey->d, t);
318 _gcry_log_mpidump (" fixed d", skey->d);
319 }
320
321 /* check for correctness of u */
322 mpi_invm(t, skey->p, skey->q );
323 if ( mpi_cmp(t, skey->u ) )
324 {
325 log_info ( "RSA Oops: u is wrong - fixed\n");
326 mpi_set (skey->u, t);
327 _gcry_log_mpidump (" fixed u", skey->u);
328 }
329
330 log_info ( "RSA secret key check finished\n");
331
332 mpi_free (t);
333 mpi_free (t1);
334 mpi_free (t2);
335 mpi_free (phi);
336}
337#endif
338
339
340
341/****************
342 * Secret key operation. Encrypt INPUT with SKEY and put result into OUTPUT.
343 *
344 *m = c^d mod n
345 *
346 * Or faster:
347 *
348 * m1 = c ^ (d mod (p-1)) mod p
349 * m2 = c ^ (d mod (q-1)) mod q
350 * h = u * (m2 - m1) mod q
351 * m = m1 + h * p
352 *
353 * Where m is OUTPUT, c is INPUT and d,n,p,q,u are elements of SKEY.
354 */
355static void
356secret(gcry_mpi_t output, gcry_mpi_t input, RSA_secret_key *skey )
357{
358 if (!skey->p && !skey->q && !skey->u)
359 {
360 mpi_powm (output, input, skey->d, skey->n);
361 }
362 else
363 {
364 gcry_mpi_t m1 = mpi_alloc_secure( mpi_get_nlimbs(skey->n)+1 );
365 gcry_mpi_t m2 = mpi_alloc_secure( mpi_get_nlimbs(skey->n)+1 );
366 gcry_mpi_t h = mpi_alloc_secure( mpi_get_nlimbs(skey->n)+1 );
367
368 /* m1 = c ^ (d mod (p-1)) mod p */
369 mpi_sub_ui( h, skey->p, 1 );
370 mpi_fdiv_r( h, skey->d, h );
371 mpi_powm( m1, input, h, skey->p );
372 /* m2 = c ^ (d mod (q-1)) mod q */
373 mpi_sub_ui( h, skey->q, 1 );
374 mpi_fdiv_r( h, skey->d, h );
375 mpi_powm( m2, input, h, skey->q );
376 /* h = u * ( m2 - m1 ) mod q */
377 mpi_sub( h, m2, m1 );
378 if ( mpi_is_neg( h ) )
379 mpi_add ( h, h, skey->q );
380 mpi_mulm( h, skey->u, h, skey->q );
381 /* m = m2 + h * p */
382 mpi_mul ( h, h, skey->p );
383 mpi_add ( output, m1, h );
384
385 mpi_free ( h );
386 mpi_free ( m1 );
387 mpi_free ( m2 );
388 }
389}
390
391
392
393/* Perform RSA blinding. */
394static gcry_mpi_t
395rsa_blind (gcry_mpi_t x, gcry_mpi_t r, gcry_mpi_t e, gcry_mpi_t n)
396{
397 /* A helper. */
398 gcry_mpi_t a;
399
400 /* Result. */
401 gcry_mpi_t y;
402
403 a = gcry_mpi_snew (gcry_mpi_get_nbits (n));
404 y = gcry_mpi_snew (gcry_mpi_get_nbits (n));
405
406 /* Now we calculate: y = (x * r^e) mod n, where r is the random
407 number, e is the public exponent, x is the non-blinded data and n
408 is the RSA modulus. */
409 gcry_mpi_powm (a, r, e, n);
410 gcry_mpi_mulm (y, a, x, n);
411
412 gcry_mpi_release (a);
413
414 return y;
415}
416
417/* Undo RSA blinding. */
418static gcry_mpi_t
419rsa_unblind (gcry_mpi_t x, gcry_mpi_t ri, gcry_mpi_t n)
420{
421 gcry_mpi_t y;
422
423 y = gcry_mpi_snew (gcry_mpi_get_nbits (n));
424
425 /* Here we calculate: y = (x * r^-1) mod n, where x is the blinded
426 decrypted data, ri is the modular multiplicative inverse of r and
427 n is the RSA modulus. */
428
429 gcry_mpi_mulm (y, ri, x, n);
430
431 return y;
432}
433
434/*********************************************
435 ************** interface ******************
436 *********************************************/
437
438gcry_err_code_t
439_gcry_rsa_generate (int algo, unsigned int nbits, unsigned long use_e,
440 gcry_mpi_t *skey, gcry_mpi_t **retfactors)
441{
442 RSA_secret_key sk;
443
444 generate (&sk, nbits, use_e);
445 skey[0] = sk.n;
446 skey[1] = sk.e;
447 skey[2] = sk.d;
448 skey[3] = sk.p;
449 skey[4] = sk.q;
450 skey[5] = sk.u;
451
452 /* make an empty list of factors */
453 *retfactors = gcry_xcalloc( 1, sizeof **retfactors );
454
455 return GPG_ERR_NO_ERROR;
456}
457
458
459gcry_err_code_t
460_gcry_rsa_check_secret_key( int algo, gcry_mpi_t *skey )
461{
462 gcry_err_code_t err = GPG_ERR_NO_ERROR;
463 RSA_secret_key sk;
464
465 sk.n = skey[0];
466 sk.e = skey[1];
467 sk.d = skey[2];
468 sk.p = skey[3];
469 sk.q = skey[4];
470 sk.u = skey[5];
471
472 if (! check_secret_key (&sk))
473 err = GPG_ERR_PUBKEY_ALGO;
474
475 return err;
476}
477
478
479gcry_err_code_t
480_gcry_rsa_encrypt (int algo, gcry_mpi_t *resarr, gcry_mpi_t data,
481 gcry_mpi_t *pkey, int flags)
482{
483 RSA_public_key pk;
484
485 pk.n = pkey[0];
486 pk.e = pkey[1];
487 resarr[0] = mpi_alloc (mpi_get_nlimbs (pk.n));
488 public (resarr[0], data, &pk);
489
490 return GPG_ERR_NO_ERROR;
491}
492
493gcry_err_code_t
494_gcry_rsa_decrypt (int algo, gcry_mpi_t *result, gcry_mpi_t *data,
495 gcry_mpi_t *skey, int flags)
496{
497 RSA_secret_key sk;
498 gcry_mpi_t r = MPI_NULL;/* Random number needed for blinding. */
499 gcry_mpi_t ri = MPI_NULL;/* Modular multiplicative inverse of
500 r. */
501 gcry_mpi_t x = MPI_NULL;/* Data to decrypt. */
502 gcry_mpi_t y; /* Result. */
503
504 /* Extract private key. */
505 sk.n = skey[0];
506 sk.e = skey[1];
507 sk.d = skey[2];
508 sk.p = skey[3];
509 sk.q = skey[4];
510 sk.u = skey[5];
511
512 y = gcry_mpi_snew (gcry_mpi_get_nbits (sk.n));
513
514 if (! (flags & PUBKEY_FLAG_NO_BLINDING))
515 {
516 /* Initialize blinding. */
517
518 /* First, we need a random number r between 0 and n - 1, which
519 is relatively prime to n (i.e. it is neither p nor q). */
520 r = gcry_mpi_snew (gcry_mpi_get_nbits (sk.n));
521 ri = gcry_mpi_snew (gcry_mpi_get_nbits (sk.n));
522
523 gcry_mpi_randomize (r, gcry_mpi_get_nbits (sk.n),
524 GCRY_STRONG_RANDOM);
525 gcry_mpi_mod (r, r, sk.n);
526
527 /* Actually it should be okay to skip the check for equality
528 with either p or q here. */
529
530 /* Calculate inverse of r. */
531 if (! gcry_mpi_invm (ri, r, sk.n))
532 BUG ();
533 }
534
535 if (! (flags & PUBKEY_FLAG_NO_BLINDING))
536 x = rsa_blind (data[0], r, sk.e, sk.n);
537 else
538 x = data[0];
539
540 /* Do the encryption. */
541 secret (y, x, &sk);
542
543 if (! (flags & PUBKEY_FLAG_NO_BLINDING))
544 {
545 /* Undo blinding. */
546 gcry_mpi_t a = gcry_mpi_copy (y);
547
548 gcry_mpi_release (y);
549 y = rsa_unblind (a, ri, sk.n);
550 }
551
552 if (! (flags & PUBKEY_FLAG_NO_BLINDING))
553 {
554 /* Deallocate resources needed for blinding. */
555 gcry_mpi_release (x);
556 gcry_mpi_release (r);
557 gcry_mpi_release (ri);
558 }
559
560 /* Copy out result. */
561 *result = y;
562
563 return GPG_ERR_NO_ERROR;
564}
565
566gcry_err_code_t
567_gcry_rsa_sign (int algo, gcry_mpi_t *resarr, gcry_mpi_t data, gcry_mpi_t *skey)
568{
569 RSA_secret_key sk;
570
571 sk.n = skey[0];
572 sk.e = skey[1];
573 sk.d = skey[2];
574 sk.p = skey[3];
575 sk.q = skey[4];
576 sk.u = skey[5];
577 resarr[0] = mpi_alloc( mpi_get_nlimbs (sk.n));
578 secret (resarr[0], data, &sk);
579
580 return GPG_ERR_NO_ERROR;
581}
582
583gcry_err_code_t
584_gcry_rsa_verify (int algo, gcry_mpi_t hash, gcry_mpi_t *data, gcry_mpi_t *pkey,
585 int (*cmp) (void *opaque, gcry_mpi_t tmp),
586 void *opaquev)
587{
588 RSA_public_key pk;
589 gcry_mpi_t result;
590 gcry_err_code_t rc;
591
592 pk.n = pkey[0];
593 pk.e = pkey[1];
594 result = gcry_mpi_new ( 160 );
595 public( result, data[0], &pk );
596 /*rc = (*cmp)( opaquev, result );*/
597 rc = mpi_cmp (result, hash) ? GPG_ERR_BAD_SIGNATURE : GPG_ERR_NO_ERROR;
598 gcry_mpi_release (result);
599
600 return rc;
601}
602
603
604unsigned int
605_gcry_rsa_get_nbits (int algo, gcry_mpi_t *pkey)
606{
607 return mpi_get_nbits (pkey[0]);
608}
609
610static char *rsa_names[] =
611 {
612 "rsa",
613 "openpgp-rsa",
614 "oid.1.2.840.113549.1.1.1",
615 NULL,
616 };
617
618gcry_pk_spec_t _gcry_pubkey_spec_rsa =
619 {
620 "RSA", rsa_names,
621 "ne", "nedpqu", "a", "s", "n",
622 GCRY_PK_USAGE_SIGN | GCRY_PK_USAGE_ENCR,
623 _gcry_rsa_generate,
624 _gcry_rsa_check_secret_key,
625 _gcry_rsa_encrypt,
626 _gcry_rsa_decrypt,
627 _gcry_rsa_sign,
628 _gcry_rsa_verify,
629 _gcry_rsa_get_nbits,
630 };
diff --git a/pwmanager/libcrypt/cipher/serpent.c b/pwmanager/libcrypt/cipher/serpent.c
new file mode 100644
index 0000000..d606d9f
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/serpent.c
@@ -0,0 +1,979 @@
1/* serpent.c - Implementation of the Serpent encryption algorithm.
2 *Copyright (C) 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22#include <config.h>
23
24#include <string.h>
25#include <stdio.h>
26
27#include "types.h"
28#include "g10lib.h"
29#include "cipher.h"
30#include "bithelp.h"
31
32/* Number of rounds per Serpent encrypt/decrypt operation. */
33#define ROUNDS 32
34
35/* Magic number, used during generating of the subkeys. */
36#define PHI 0x9E3779B9
37
38/* Internal types. */
39typedef byte byte_t;
40typedef u32 u32_t;
41
42/* Serpent works on 128 bit blocks. */
43typedef u32_t serpent_block_t[4];
44
45/* Serpent key, provided by the user. If the original key is shorter
46 than 256 bits, it is padded. */
47typedef u32_t serpent_key_t[8];
48
49/* The key schedule consists of 33 128 bit subkeys. */
50typedef u32_t serpent_subkeys_t[ROUNDS + 1][4];
51
52/* A Serpent context. */
53typedef struct serpent_context
54{
55 serpent_subkeys_t keys;/* Generated subkeys. */
56} serpent_context_t;
57
58#define byte_swap_32(x) \
59 (0 \
60 | (((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) \
61 | (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24));
62
63/* These are the S-Boxes of Serpent. They are copied from Serpents
64 reference implementation (the optimized one, contained in
65 `floppy2') and are therefore:
66
67 Copyright (C) 1998 Ross Anderson, Eli Biham, Lars Knudsen.
68
69 To quote the Serpent homepage
70 (http://www.cl.cam.ac.uk/~rja14/serpent.html):
71
72 "Serpent is now completely in the public domain, and we impose no
73 restrictions on its use. This was announced on the 21st August at
74 the First AES Candidate Conference. The optimised implementations
75 in the submission package are now under the GNU PUBLIC LICENSE
76 (GPL), although some comments in the code still say otherwise. You
77 are welcome to use Serpent for any application." */
78
79#define SBOX0(a, b, c, d, w, x, y, z) \
80 { \
81 register unsigned long t02, t03, t05, t06, t07, t08, t09; \
82 register unsigned long t11, t12, t13, t14, t15, t17, t01; \
83 t01 = b ^ c ; \
84 t02 = a | d ; \
85 t03 = a ^ b ; \
86 z = t02 ^ t01; \
87 t05 = c | z ; \
88 t06 = a ^ d ; \
89 t07 = b | c ; \
90 t08 = d & t05; \
91 t09 = t03 & t07; \
92 y = t09 ^ t08; \
93 t11 = t09 & y ; \
94 t12 = c ^ d ; \
95 t13 = t07 ^ t11; \
96 t14 = b & t06; \
97 t15 = t06 ^ t13; \
98 w = ~ t15; \
99 t17 = w ^ t14; \
100 x = t12 ^ t17; \
101 }
102
103#define SBOX0_INVERSE(a, b, c, d, w, x, y, z) \
104 { \
105 register unsigned long t02, t03, t04, t05, t06, t08, t09, t10; \
106 register unsigned long t12, t13, t14, t15, t17, t18, t01; \
107 t01 = c ^ d ; \
108 t02 = a | b ; \
109 t03 = b | c ; \
110 t04 = c & t01; \
111 t05 = t02 ^ t01; \
112 t06 = a | t04; \
113 y = ~ t05; \
114 t08 = b ^ d ; \
115 t09 = t03 & t08; \
116 t10 = d | y ; \
117 x = t09 ^ t06; \
118 t12 = a | t05; \
119 t13 = x ^ t12; \
120 t14 = t03 ^ t10; \
121 t15 = a ^ c ; \
122 z = t14 ^ t13; \
123 t17 = t05 & t13; \
124 t18 = t14 | t17; \
125 w = t15 ^ t18; \
126 }
127
128#define SBOX1(a, b, c, d, w, x, y, z) \
129 { \
130 register unsigned long t02, t03, t04, t05, t06, t07, t08; \
131 register unsigned long t10, t11, t12, t13, t16, t17, t01; \
132 t01 = a | d ; \
133 t02 = c ^ d ; \
134 t03 = ~ b ; \
135 t04 = a ^ c ; \
136 t05 = a | t03; \
137 t06 = d & t04; \
138 t07 = t01 & t02; \
139 t08 = b | t06; \
140 y = t02 ^ t05; \
141 t10 = t07 ^ t08; \
142 t11 = t01 ^ t10; \
143 t12 = y ^ t11; \
144 t13 = b & d ; \
145 z = ~ t10; \
146 x = t13 ^ t12; \
147 t16 = t10 | x ; \
148 t17 = t05 & t16; \
149 w = c ^ t17; \
150 }
151
152#define SBOX1_INVERSE(a, b, c, d, w, x, y, z) \
153 { \
154 register unsigned long t02, t03, t04, t05, t06, t07, t08; \
155 register unsigned long t09, t10, t11, t14, t15, t17, t01; \
156 t01 = a ^ b ; \
157 t02 = b | d ; \
158 t03 = a & c ; \
159 t04 = c ^ t02; \
160 t05 = a | t04; \
161 t06 = t01 & t05; \
162 t07 = d | t03; \
163 t08 = b ^ t06; \
164 t09 = t07 ^ t06; \
165 t10 = t04 | t03; \
166 t11 = d & t08; \
167 y = ~ t09; \
168 x = t10 ^ t11; \
169 t14 = a | y ; \
170 t15 = t06 ^ x ; \
171 z = t01 ^ t04; \
172 t17 = c ^ t15; \
173 w = t14 ^ t17; \
174 }
175
176#define SBOX2(a, b, c, d, w, x, y, z) \
177 { \
178 register unsigned long t02, t03, t05, t06, t07, t08; \
179 register unsigned long t09, t10, t12, t13, t14, t01; \
180 t01 = a | c ; \
181 t02 = a ^ b ; \
182 t03 = d ^ t01; \
183 w = t02 ^ t03; \
184 t05 = c ^ w ; \
185 t06 = b ^ t05; \
186 t07 = b | t05; \
187 t08 = t01 & t06; \
188 t09 = t03 ^ t07; \
189 t10 = t02 | t09; \
190 x = t10 ^ t08; \
191 t12 = a | d ; \
192 t13 = t09 ^ x ; \
193 t14 = b ^ t13; \
194 z = ~ t09; \
195 y = t12 ^ t14; \
196 }
197
198#define SBOX2_INVERSE(a, b, c, d, w, x, y, z) \
199 { \
200 register unsigned long t02, t03, t04, t06, t07, t08, t09; \
201 register unsigned long t10, t11, t12, t15, t16, t17, t01; \
202 t01 = a ^ d ; \
203 t02 = c ^ d ; \
204 t03 = a & c ; \
205 t04 = b | t02; \
206 w = t01 ^ t04; \
207 t06 = a | c ; \
208 t07 = d | w ; \
209 t08 = ~ d ; \
210 t09 = b & t06; \
211 t10 = t08 | t03; \
212 t11 = b & t07; \
213 t12 = t06 & t02; \
214 z = t09 ^ t10; \
215 x = t12 ^ t11; \
216 t15 = c & z ; \
217 t16 = w ^ x ; \
218 t17 = t10 ^ t15; \
219 y = t16 ^ t17; \
220 }
221
222#define SBOX3(a, b, c, d, w, x, y, z) \
223 { \
224 register unsigned long t02, t03, t04, t05, t06, t07, t08; \
225 register unsigned long t09, t10, t11, t13, t14, t15, t01; \
226 t01 = a ^ c ; \
227 t02 = a | d ; \
228 t03 = a & d ; \
229 t04 = t01 & t02; \
230 t05 = b | t03; \
231 t06 = a & b ; \
232 t07 = d ^ t04; \
233 t08 = c | t06; \
234 t09 = b ^ t07; \
235 t10 = d & t05; \
236 t11 = t02 ^ t10; \
237 z = t08 ^ t09; \
238 t13 = d | z ; \
239 t14 = a | t07; \
240 t15 = b & t13; \
241 y = t08 ^ t11; \
242 w = t14 ^ t15; \
243 x = t05 ^ t04; \
244 }
245
246#define SBOX3_INVERSE(a, b, c, d, w, x, y, z) \
247 { \
248 register unsigned long t02, t03, t04, t05, t06, t07, t09; \
249 register unsigned long t11, t12, t13, t14, t16, t01; \
250 t01 = c | d ; \
251 t02 = a | d ; \
252 t03 = c ^ t02; \
253 t04 = b ^ t02; \
254 t05 = a ^ d ; \
255 t06 = t04 & t03; \
256 t07 = b & t01; \
257 y = t05 ^ t06; \
258 t09 = a ^ t03; \
259 w = t07 ^ t03; \
260 t11 = w | t05; \
261 t12 = t09 & t11; \
262 t13 = a & y ; \
263 t14 = t01 ^ t05; \
264 x = b ^ t12; \
265 t16 = b | t13; \
266 z = t14 ^ t16; \
267 }
268
269#define SBOX4(a, b, c, d, w, x, y, z) \
270 { \
271 register unsigned long t02, t03, t04, t05, t06, t08, t09; \
272 register unsigned long t10, t11, t12, t13, t14, t15, t16, t01; \
273 t01 = a | b ; \
274 t02 = b | c ; \
275 t03 = a ^ t02; \
276 t04 = b ^ d ; \
277 t05 = d | t03; \
278 t06 = d & t01; \
279 z = t03 ^ t06; \
280 t08 = z & t04; \
281 t09 = t04 & t05; \
282 t10 = c ^ t06; \
283 t11 = b & c ; \
284 t12 = t04 ^ t08; \
285 t13 = t11 | t03; \
286 t14 = t10 ^ t09; \
287 t15 = a & t05; \
288 t16 = t11 | t12; \
289 y = t13 ^ t08; \
290 x = t15 ^ t16; \
291 w = ~ t14; \
292 }
293
294#define SBOX4_INVERSE(a, b, c, d, w, x, y, z) \
295 { \
296 register unsigned long t02, t03, t04, t05, t06, t07, t09; \
297 register unsigned long t10, t11, t12, t13, t15, t01; \
298 t01 = b | d ; \
299 t02 = c | d ; \
300 t03 = a & t01; \
301 t04 = b ^ t02; \
302 t05 = c ^ d ; \
303 t06 = ~ t03; \
304 t07 = a & t04; \
305 x = t05 ^ t07; \
306 t09 = x | t06; \
307 t10 = a ^ t07; \
308 t11 = t01 ^ t09; \
309 t12 = d ^ t04; \
310 t13 = c | t10; \
311 z = t03 ^ t12; \
312 t15 = a ^ t04; \
313 y = t11 ^ t13; \
314 w = t15 ^ t09; \
315 }
316
317#define SBOX5(a, b, c, d, w, x, y, z) \
318 { \
319 register unsigned long t02, t03, t04, t05, t07, t08, t09; \
320 register unsigned long t10, t11, t12, t13, t14, t01; \
321 t01 = b ^ d ; \
322 t02 = b | d ; \
323 t03 = a & t01; \
324 t04 = c ^ t02; \
325 t05 = t03 ^ t04; \
326 w = ~ t05; \
327 t07 = a ^ t01; \
328 t08 = d | w ; \
329 t09 = b | t05; \
330 t10 = d ^ t08; \
331 t11 = b | t07; \
332 t12 = t03 | w ; \
333 t13 = t07 | t10; \
334 t14 = t01 ^ t11; \
335 y = t09 ^ t13; \
336 x = t07 ^ t08; \
337 z = t12 ^ t14; \
338 }
339
340#define SBOX5_INVERSE(a, b, c, d, w, x, y, z) \
341 { \
342 register unsigned long t02, t03, t04, t05, t07, t08, t09; \
343 register unsigned long t10, t12, t13, t15, t16, t01; \
344 t01 = a & d ; \
345 t02 = c ^ t01; \
346 t03 = a ^ d ; \
347 t04 = b & t02; \
348 t05 = a & c ; \
349 w = t03 ^ t04; \
350 t07 = a & w ; \
351 t08 = t01 ^ w ; \
352 t09 = b | t05; \
353 t10 = ~ b ; \
354 x = t08 ^ t09; \
355 t12 = t10 | t07; \
356 t13 = w | x ; \
357 z = t02 ^ t12; \
358 t15 = t02 ^ t13; \
359 t16 = b ^ d ; \
360 y = t16 ^ t15; \
361 }
362
363#define SBOX6(a, b, c, d, w, x, y, z) \
364 { \
365 register unsigned long t02, t03, t04, t05, t07, t08, t09, t10; \
366 register unsigned long t11, t12, t13, t15, t17, t18, t01; \
367 t01 = a & d ; \
368 t02 = b ^ c ; \
369 t03 = a ^ d ; \
370 t04 = t01 ^ t02; \
371 t05 = b | c ; \
372 x = ~ t04; \
373 t07 = t03 & t05; \
374 t08 = b & x ; \
375 t09 = a | c ; \
376 t10 = t07 ^ t08; \
377 t11 = b | d ; \
378 t12 = c ^ t11; \
379 t13 = t09 ^ t10; \
380 y = ~ t13; \
381 t15 = x & t03; \
382 z = t12 ^ t07; \
383 t17 = a ^ b ; \
384 t18 = y ^ t15; \
385 w = t17 ^ t18; \
386 }
387
388#define SBOX6_INVERSE(a, b, c, d, w, x, y, z) \
389 { \
390 register unsigned long t02, t03, t04, t05, t06, t07, t08, t09; \
391 register unsigned long t12, t13, t14, t15, t16, t17, t01; \
392 t01 = a ^ c ; \
393 t02 = ~ c ; \
394 t03 = b & t01; \
395 t04 = b | t02; \
396 t05 = d | t03; \
397 t06 = b ^ d ; \
398 t07 = a & t04; \
399 t08 = a | t02; \
400 t09 = t07 ^ t05; \
401 x = t06 ^ t08; \
402 w = ~ t09; \
403 t12 = b & w ; \
404 t13 = t01 & t05; \
405 t14 = t01 ^ t12; \
406 t15 = t07 ^ t13; \
407 t16 = d | t02; \
408 t17 = a ^ x ; \
409 z = t17 ^ t15; \
410 y = t16 ^ t14; \
411 }
412
413#define SBOX7(a, b, c, d, w, x, y, z) \
414 { \
415 register unsigned long t02, t03, t04, t05, t06, t08, t09, t10; \
416 register unsigned long t11, t13, t14, t15, t16, t17, t01; \
417 t01 = a & c ; \
418 t02 = ~ d ; \
419 t03 = a & t02; \
420 t04 = b | t01; \
421 t05 = a & b ; \
422 t06 = c ^ t04; \
423 z = t03 ^ t06; \
424 t08 = c | z ; \
425 t09 = d | t05; \
426 t10 = a ^ t08; \
427 t11 = t04 & z ; \
428 x = t09 ^ t10; \
429 t13 = b ^ x ; \
430 t14 = t01 ^ x ; \
431 t15 = c ^ t05; \
432 t16 = t11 | t13; \
433 t17 = t02 | t14; \
434 w = t15 ^ t17; \
435 y = a ^ t16; \
436 }
437
438#define SBOX7_INVERSE(a, b, c, d, w, x, y, z) \
439 { \
440 register unsigned long t02, t03, t04, t06, t07, t08, t09; \
441 register unsigned long t10, t11, t13, t14, t15, t16, t01; \
442 t01 = a & b ; \
443 t02 = a | b ; \
444 t03 = c | t01; \
445 t04 = d & t02; \
446 z = t03 ^ t04; \
447 t06 = b ^ t04; \
448 t07 = d ^ z ; \
449 t08 = ~ t07; \
450 t09 = t06 | t08; \
451 t10 = b ^ d ; \
452 t11 = a | d ; \
453 x = a ^ t09; \
454 t13 = c ^ t06; \
455 t14 = c & t11; \
456 t15 = d | x ; \
457 t16 = t01 | t10; \
458 w = t13 ^ t15; \
459 y = t14 ^ t16; \
460 }
461
462/* XOR BLOCK1 into BLOCK0. */
463#define BLOCK_XOR(block0, block1) \
464 { \
465 block0[0] ^= block1[0]; \
466 block0[1] ^= block1[1]; \
467 block0[2] ^= block1[2]; \
468 block0[3] ^= block1[3]; \
469 }
470
471/* Copy BLOCK_SRC to BLOCK_DST. */
472#define BLOCK_COPY(block_dst, block_src) \
473 { \
474 block_dst[0] = block_src[0]; \
475 block_dst[1] = block_src[1]; \
476 block_dst[2] = block_src[2]; \
477 block_dst[3] = block_src[3]; \
478 }
479
480/* Apply SBOX number WHICH to to the block found in ARRAY0 at index
481 INDEX, writing the output to the block found in ARRAY1 at index
482 INDEX. */
483#define SBOX(which, array0, array1, index) \
484 SBOX##which (array0[index + 0], array0[index + 1], \
485 array0[index + 2], array0[index + 3], \
486 array1[index + 0], array1[index + 1], \
487 array1[index + 2], array1[index + 3]);
488
489/* Apply inverse SBOX number WHICH to to the block found in ARRAY0 at
490 index INDEX, writing the output to the block found in ARRAY1 at
491 index INDEX. */
492#define SBOX_INVERSE(which, array0, array1, index) \
493 SBOX##which##_INVERSE (array0[index + 0], array0[index + 1], \
494 array0[index + 2], array0[index + 3], \
495 array1[index + 0], array1[index + 1], \
496 array1[index + 2], array1[index + 3]);
497
498/* Apply the linear transformation to BLOCK. */
499#define LINEAR_TRANSFORMATION(block) \
500 { \
501 block[0] = rol (block[0], 13); \
502 block[2] = rol (block[2], 3); \
503 block[1] = block[1] ^ block[0] ^ block[2]; \
504 block[3] = block[3] ^ block[2] ^ (block[0] << 3); \
505 block[1] = rol (block[1], 1); \
506 block[3] = rol (block[3], 7); \
507 block[0] = block[0] ^ block[1] ^ block[3]; \
508 block[2] = block[2] ^ block[3] ^ (block[1] << 7); \
509 block[0] = rol (block[0], 5); \
510 block[2] = rol (block[2], 22); \
511 }
512
513/* Apply the inverse linear transformation to BLOCK. */
514#define LINEAR_TRANSFORMATION_INVERSE(block) \
515 { \
516 block[2] = ror (block[2], 22); \
517 block[0] = ror (block[0] , 5); \
518 block[2] = block[2] ^ block[3] ^ (block[1] << 7); \
519 block[0] = block[0] ^ block[1] ^ block[3]; \
520 block[3] = ror (block[3], 7); \
521 block[1] = ror (block[1], 1); \
522 block[3] = block[3] ^ block[2] ^ (block[0] << 3); \
523 block[1] = block[1] ^ block[0] ^ block[2]; \
524 block[2] = ror (block[2], 3); \
525 block[0] = ror (block[0], 13); \
526 }
527
528/* Apply a Serpent round to BLOCK, using the SBOX number WHICH and the
529 subkeys contained in SUBKEYS. Use BLOCK_TMP as temporary storage.
530 This macro increments `round'. */
531#define ROUND(which, subkeys, block, block_tmp) \
532 { \
533 BLOCK_XOR (block, subkeys[round]); \
534 round++; \
535 SBOX (which, block, block_tmp, 0); \
536 LINEAR_TRANSFORMATION (block_tmp); \
537 BLOCK_COPY (block, block_tmp); \
538 }
539
540/* Apply the last Serpent round to BLOCK, using the SBOX number WHICH
541 and the subkeys contained in SUBKEYS. Use BLOCK_TMP as temporary
542 storage. The result will be stored in BLOCK_TMP. This macro
543 increments `round'. */
544#define ROUND_LAST(which, subkeys, block, block_tmp) \
545 { \
546 BLOCK_XOR (block, subkeys[round]); \
547 round++; \
548 SBOX (which, block, block_tmp, 0); \
549 BLOCK_XOR (block_tmp, subkeys[round]); \
550 round++; \
551 }
552
553/* Apply an inverse Serpent round to BLOCK, using the SBOX number
554 WHICH and the subkeys contained in SUBKEYS. Use BLOCK_TMP as
555 temporary storage. This macro increments `round'. */
556#define ROUND_INVERSE(which, subkey, block, block_tmp) \
557 { \
558 LINEAR_TRANSFORMATION_INVERSE (block); \
559 SBOX_INVERSE (which, block, block_tmp, 0); \
560 BLOCK_XOR (block_tmp, subkey[round]); \
561 round--; \
562 BLOCK_COPY (block, block_tmp); \
563 }
564
565/* Apply the first Serpent round to BLOCK, using the SBOX number WHICH
566 and the subkeys contained in SUBKEYS. Use BLOCK_TMP as temporary
567 storage. The result will be stored in BLOCK_TMP. This macro
568 increments `round'. */
569#define ROUND_FIRST_INVERSE(which, subkeys, block, block_tmp) \
570 { \
571 BLOCK_XOR (block, subkeys[round]); \
572 round--; \
573 SBOX_INVERSE (which, block, block_tmp, 0); \
574 BLOCK_XOR (block_tmp, subkeys[round]); \
575 round--; \
576 }
577
578/* Convert the user provided key KEY of KEY_LENGTH bytes into the
579 internally used format. */
580static void
581serpent_key_prepare (const byte_t *key, unsigned int key_length,
582 serpent_key_t key_prepared)
583{
584 int i;
585
586 /* Copy key. */
587 for (i = 0; i < key_length / 4; i++)
588 {
589#ifdef WORDS_BIGENDIAN
590 key_prepared[i] = byte_swap_32 (((u32_t *) key)[i]);
591#else
592 key_prepared[i] = ((u32_t *) key)[i];
593#endif
594 }
595
596 if (i < 8)
597 {
598 /* Key must be padded according to the Serpent
599 specification. */
600 key_prepared[i] = 0x00000001;
601
602 for (i++; i < 8; i++)
603 key_prepared[i] = 0;
604 }
605}
606
607/* Derive the 33 subkeys from KEY and store them in SUBKEYS. */
608static void
609serpent_subkeys_generate (serpent_key_t key, serpent_subkeys_t subkeys)
610{
611 u32_t w_real[140]; /* The `prekey'. */
612 u32_t k[132];
613 u32_t *w = &w_real[8];
614 int i, j;
615
616 /* Initialize with key values. */
617 for (i = 0; i < 8; i++)
618 w[i - 8] = key[i];
619
620 /* Expand to intermediate key using the affine recurrence. */
621 for (i = 0; i < 132; i++)
622 w[i] = rol (w[i - 8] ^ w[i - 5] ^ w[i - 3] ^ w[i - 1] ^ PHI ^ i, 11);
623
624 /* Calculate subkeys via S-Boxes, in bitslice mode. */
625 SBOX (3, w, k, 0);
626 SBOX (2, w, k, 4);
627 SBOX (1, w, k, 8);
628 SBOX (0, w, k, 12);
629 SBOX (7, w, k, 16);
630 SBOX (6, w, k, 20);
631 SBOX (5, w, k, 24);
632 SBOX (4, w, k, 28);
633 SBOX (3, w, k, 32);
634 SBOX (2, w, k, 36);
635 SBOX (1, w, k, 40);
636 SBOX (0, w, k, 44);
637 SBOX (7, w, k, 48);
638 SBOX (6, w, k, 52);
639 SBOX (5, w, k, 56);
640 SBOX (4, w, k, 60);
641 SBOX (3, w, k, 64);
642 SBOX (2, w, k, 68);
643 SBOX (1, w, k, 72);
644 SBOX (0, w, k, 76);
645 SBOX (7, w, k, 80);
646 SBOX (6, w, k, 84);
647 SBOX (5, w, k, 88);
648 SBOX (4, w, k, 92);
649 SBOX (3, w, k, 96);
650 SBOX (2, w, k, 100);
651 SBOX (1, w, k, 104);
652 SBOX (0, w, k, 108);
653 SBOX (7, w, k, 112);
654 SBOX (6, w, k, 116);
655 SBOX (5, w, k, 120);
656 SBOX (4, w, k, 124);
657 SBOX (3, w, k, 128);
658
659 /* Renumber subkeys. */
660 for (i = 0; i < ROUNDS + 1; i++)
661 for (j = 0; j < 4; j++)
662 subkeys[i][j] = k[4 * i + j];
663}
664
665/* Initialize CONTEXT with the key KEY of KEY_LENGTH bits. */
666static void
667serpent_setkey_internal (serpent_context_t *context,
668 const byte_t *key, unsigned int key_length)
669{
670 serpent_key_t key_prepared;
671
672 serpent_key_prepare (key, key_length, key_prepared);
673 serpent_subkeys_generate (key_prepared, context->keys);
674 _gcry_burn_stack (272 * sizeof (u32_t));
675}
676
677/* Initialize CTX with the key KEY of KEY_LENGTH bytes. */
678static gcry_err_code_t
679serpent_setkey (void *ctx,
680 const byte_t *key, unsigned int key_length)
681{
682 serpent_context_t *context = ctx;
683 static const char *serpent_test_ret;
684 static int serpent_init_done;
685 gcry_err_code_t ret = GPG_ERR_NO_ERROR;
686
687 if (! serpent_init_done)
688 {
689 /* Execute a self-test the first time, Serpent is used. */
690 static const char *serpent_test (void);
691
692 serpent_test_ret = serpent_test ();
693 if (serpent_test_ret)
694 log_error ("Serpent test failure: %s\n", serpent_test_ret);
695 serpent_init_done = 1;
696 }
697
698 if (serpent_test_ret)
699 ret = GPG_ERR_SELFTEST_FAILED;
700 else
701 {
702 serpent_setkey_internal (context, key, key_length);
703 _gcry_burn_stack (sizeof (serpent_key_t));
704 }
705
706 return ret;
707}
708
709static void
710serpent_encrypt_internal (serpent_context_t *context,
711 const serpent_block_t input, serpent_block_t output)
712{
713 serpent_block_t b, b_next;
714 int round = 0;
715
716#ifdef WORDS_BIGENDIAN
717 b[0] = byte_swap_32 (input[0]);
718 b[1] = byte_swap_32 (input[1]);
719 b[2] = byte_swap_32 (input[2]);
720 b[3] = byte_swap_32 (input[3]);
721#else
722 b[0] = input[0];
723 b[1] = input[1];
724 b[2] = input[2];
725 b[3] = input[3];
726#endif
727
728 ROUND (0, context->keys, b, b_next);
729 ROUND (1, context->keys, b, b_next);
730 ROUND (2, context->keys, b, b_next);
731 ROUND (3, context->keys, b, b_next);
732 ROUND (4, context->keys, b, b_next);
733 ROUND (5, context->keys, b, b_next);
734 ROUND (6, context->keys, b, b_next);
735 ROUND (7, context->keys, b, b_next);
736 ROUND (0, context->keys, b, b_next);
737 ROUND (1, context->keys, b, b_next);
738 ROUND (2, context->keys, b, b_next);
739 ROUND (3, context->keys, b, b_next);
740 ROUND (4, context->keys, b, b_next);
741 ROUND (5, context->keys, b, b_next);
742 ROUND (6, context->keys, b, b_next);
743 ROUND (7, context->keys, b, b_next);
744 ROUND (0, context->keys, b, b_next);
745 ROUND (1, context->keys, b, b_next);
746 ROUND (2, context->keys, b, b_next);
747 ROUND (3, context->keys, b, b_next);
748 ROUND (4, context->keys, b, b_next);
749 ROUND (5, context->keys, b, b_next);
750 ROUND (6, context->keys, b, b_next);
751 ROUND (7, context->keys, b, b_next);
752 ROUND (0, context->keys, b, b_next);
753 ROUND (1, context->keys, b, b_next);
754 ROUND (2, context->keys, b, b_next);
755 ROUND (3, context->keys, b, b_next);
756 ROUND (4, context->keys, b, b_next);
757 ROUND (5, context->keys, b, b_next);
758 ROUND (6, context->keys, b, b_next);
759
760 ROUND_LAST (7, context->keys, b, b_next);
761
762#ifdef WORDS_BIGENDIAN
763 output[0] = byte_swap_32 (b_next[0]);
764 output[1] = byte_swap_32 (b_next[1]);
765 output[2] = byte_swap_32 (b_next[2]);
766 output[3] = byte_swap_32 (b_next[3]);
767#else
768 output[0] = b_next[0];
769 output[1] = b_next[1];
770 output[2] = b_next[2];
771 output[3] = b_next[3];
772#endif
773}
774
775static void
776serpent_decrypt_internal (serpent_context_t *context,
777 const serpent_block_t input, serpent_block_t output)
778{
779 serpent_block_t b, b_next;
780 int round = ROUNDS;
781
782#ifdef WORDS_BIGENDIAN
783 b_next[0] = byte_swap_32 (input[0]);
784 b_next[1] = byte_swap_32 (input[1]);
785 b_next[2] = byte_swap_32 (input[2]);
786 b_next[3] = byte_swap_32 (input[3]);
787#else
788 b_next[0] = input[0];
789 b_next[1] = input[1];
790 b_next[2] = input[2];
791 b_next[3] = input[3];
792#endif
793
794 ROUND_FIRST_INVERSE (7, context->keys, b_next, b);
795
796 ROUND_INVERSE (6, context->keys, b, b_next);
797 ROUND_INVERSE (5, context->keys, b, b_next);
798 ROUND_INVERSE (4, context->keys, b, b_next);
799 ROUND_INVERSE (3, context->keys, b, b_next);
800 ROUND_INVERSE (2, context->keys, b, b_next);
801 ROUND_INVERSE (1, context->keys, b, b_next);
802 ROUND_INVERSE (0, context->keys, b, b_next);
803 ROUND_INVERSE (7, context->keys, b, b_next);
804 ROUND_INVERSE (6, context->keys, b, b_next);
805 ROUND_INVERSE (5, context->keys, b, b_next);
806 ROUND_INVERSE (4, context->keys, b, b_next);
807 ROUND_INVERSE (3, context->keys, b, b_next);
808 ROUND_INVERSE (2, context->keys, b, b_next);
809 ROUND_INVERSE (1, context->keys, b, b_next);
810 ROUND_INVERSE (0, context->keys, b, b_next);
811 ROUND_INVERSE (7, context->keys, b, b_next);
812 ROUND_INVERSE (6, context->keys, b, b_next);
813 ROUND_INVERSE (5, context->keys, b, b_next);
814 ROUND_INVERSE (4, context->keys, b, b_next);
815 ROUND_INVERSE (3, context->keys, b, b_next);
816 ROUND_INVERSE (2, context->keys, b, b_next);
817 ROUND_INVERSE (1, context->keys, b, b_next);
818 ROUND_INVERSE (0, context->keys, b, b_next);
819 ROUND_INVERSE (7, context->keys, b, b_next);
820 ROUND_INVERSE (6, context->keys, b, b_next);
821 ROUND_INVERSE (5, context->keys, b, b_next);
822 ROUND_INVERSE (4, context->keys, b, b_next);
823 ROUND_INVERSE (3, context->keys, b, b_next);
824 ROUND_INVERSE (2, context->keys, b, b_next);
825 ROUND_INVERSE (1, context->keys, b, b_next);
826 ROUND_INVERSE (0, context->keys, b, b_next);
827
828
829#ifdef WORDS_BIGENDIAN
830 output[0] = byte_swap_32 (b_next[0]);
831 output[1] = byte_swap_32 (b_next[1]);
832 output[2] = byte_swap_32 (b_next[2]);
833 output[3] = byte_swap_32 (b_next[3]);
834#else
835 output[0] = b_next[0];
836 output[1] = b_next[1];
837 output[2] = b_next[2];
838 output[3] = b_next[3];
839#endif
840}
841
842static void
843serpent_encrypt (void *ctx, byte_t *buffer_out, const byte_t *buffer_in)
844{
845 serpent_context_t *context = ctx;
846
847 serpent_encrypt_internal (context,
848 (const u32_t *) buffer_in, (u32_t *) buffer_out);
849 _gcry_burn_stack (2 * sizeof (serpent_block_t));
850}
851
852static void
853serpent_decrypt (void *ctx, byte *buffer_out, const byte *buffer_in)
854{
855 serpent_context_t *context = ctx;
856
857 serpent_decrypt_internal (context,
858 (const u32_t *) buffer_in,
859 (u32_t *) buffer_out);
860 _gcry_burn_stack (2 * sizeof (serpent_block_t));
861}
862
863
864
865/* Serpent test. */
866
867static const char *
868serpent_test (void)
869{
870 serpent_context_t context;
871 unsigned char scratch[16];
872 unsigned int i;
873
874 static struct test
875 {
876 int key_length;
877 unsigned char key[32];
878 unsigned char text_plain[16];
879 unsigned char text_cipher[16];
880 } test_data[] =
881 {
882 {
883 16,
884 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
885 "\xD2\x9D\x57\x6F\xCE\xA3\xA3\xA7\xED\x90\x99\xF2\x92\x73\xD7\x8E",
886 "\xB2\x28\x8B\x96\x8A\xE8\xB0\x86\x48\xD1\xCE\x96\x06\xFD\x99\x2D"
887 },
888 {
889 24,
890 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
891 "\x00\x00\x00\x00\x00\x00\x00\x00",
892 "\xD2\x9D\x57\x6F\xCE\xAB\xA3\xA7\xED\x98\x99\xF2\x92\x7B\xD7\x8E",
893 "\x13\x0E\x35\x3E\x10\x37\xC2\x24\x05\xE8\xFA\xEF\xB2\xC3\xC3\xE9"
894 },
895 {
896 32,
897 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
898 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
899 "\xD0\x95\x57\x6F\xCE\xA3\xE3\xA7\xED\x98\xD9\xF2\x90\x73\xD7\x8E",
900 "\xB9\x0E\xE5\x86\x2D\xE6\x91\x68\xF2\xBD\xD5\x12\x5B\x45\x47\x2B"
901 },
902 {
903 32,
904 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
905 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
906 "\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00",
907 "\x20\x61\xA4\x27\x82\xBD\x52\xEC\x69\x1E\xC3\x83\xB0\x3B\xA7\x7C"
908 },
909 {
910 0
911 },
912 };
913
914 for (i = 0; test_data[i].key_length; i++)
915 {
916 serpent_setkey_internal (&context, test_data[i].key,
917 test_data[i].key_length);
918 serpent_encrypt_internal (&context,
919 (const u32_t *) test_data[i].text_plain,
920 (u32_t *) scratch);
921
922 if (memcmp (scratch, test_data[i].text_cipher, sizeof (serpent_block_t)))
923 switch (test_data[i].key_length)
924 {
925 case 16:
926 return "Serpent-128 test encryption failed.";
927 case 24:
928 return "Serpent-192 test encryption failed.";
929 case 32:
930 return "Serpent-256 test encryption failed.";
931 }
932
933 serpent_decrypt_internal (&context,
934 (const u32_t *) test_data[i].text_cipher,
935 (u32_t *) scratch);
936 if (memcmp (scratch, test_data[i].text_plain, sizeof (serpent_block_t)))
937 switch (test_data[i].key_length)
938 {
939 case 16:
940 return "Serpent-128 test decryption failed.";
941 case 24:
942 return "Serpent-192 test decryption failed.";
943 case 32:
944 return "Serpent-256 test decryption failed.";
945 }
946 }
947
948 return NULL;
949}
950
951
952
953/* "SERPENT" is an alias for "SERPENT128". */
954static const char *cipher_spec_serpent128_aliases[] =
955 {
956 "SERPENT",
957 NULL,
958 };
959
960gcry_cipher_spec_t _gcry_cipher_spec_serpent128 =
961 {
962 "SERPENT128", cipher_spec_serpent128_aliases, NULL, 16, 128,
963 sizeof (serpent_context_t),
964 serpent_setkey, serpent_encrypt, serpent_decrypt,
965 };
966
967gcry_cipher_spec_t _gcry_cipher_spec_serpent192 =
968 {
969 "SERPENT192", NULL, NULL, 16, 192,
970 sizeof (serpent_context_t),
971 serpent_setkey, serpent_encrypt, serpent_decrypt,
972 };
973
974gcry_cipher_spec_t _gcry_cipher_spec_serpent256 =
975 {
976 "SERPENT256", NULL, NULL, 16, 256,
977 sizeof (serpent_context_t),
978 serpent_setkey, serpent_encrypt, serpent_decrypt,
979 };
diff --git a/pwmanager/libcrypt/cipher/sha1.c b/pwmanager/libcrypt/cipher/sha1.c
new file mode 100644
index 0000000..1f31443
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/sha1.c
@@ -0,0 +1,368 @@
1/* sha1.c - SHA1 hash function
2 *Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21
22/* Test vectors:
23 *
24 * "abc"
25 * A999 3E36 4706 816A BA3E 2571 7850 C26C 9CD0 D89D
26 *
27 * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
28 * 8498 3E44 1C3B D26E BAAE 4AA1 F951 29E5 E546 70F1
29 */
30
31
32#include <config.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <assert.h>
37#include "g10lib.h"
38#include "memory.h"
39#include "bithelp.h"
40#include "cipher.h"
41
42typedef struct {
43 u32 h0,h1,h2,h3,h4;
44 u32 nblocks;
45 byte buf[64];
46 int count;
47} SHA1_CONTEXT;
48
49
50static void
51sha1_init (void *context)
52{
53 SHA1_CONTEXT *hd = context;
54
55 hd->h0 = 0x67452301;
56 hd->h1 = 0xefcdab89;
57 hd->h2 = 0x98badcfe;
58 hd->h3 = 0x10325476;
59 hd->h4 = 0xc3d2e1f0;
60 hd->nblocks = 0;
61 hd->count = 0;
62}
63
64
65/****************
66 * Transform the message X which consists of 16 32-bit-words
67 */
68static void
69transform( SHA1_CONTEXT *hd, byte *data )
70{
71 register u32 a,b,c,d,e,tm;
72 u32 x[16];
73
74 /* Get values from the chaining vars. */
75 a = hd->h0;
76 b = hd->h1;
77 c = hd->h2;
78 d = hd->h3;
79 e = hd->h4;
80
81#ifdef WORDS_BIGENDIAN
82 memcpy( x, data, 64 );
83#else
84 {
85 int i;
86 byte *p2;
87 for(i=0, p2=(byte*)x; i < 16; i++, p2 += 4 )
88 {
89 p2[3] = *data++;
90 p2[2] = *data++;
91 p2[1] = *data++;
92 p2[0] = *data++;
93 }
94 }
95#endif
96
97
98#define K1 0x5A827999L
99#define K2 0x6ED9EBA1L
100#define K3 0x8F1BBCDCL
101#define K4 0xCA62C1D6L
102#define F1(x,y,z) ( z ^ ( x & ( y ^ z ) ) )
103#define F2(x,y,z) ( x ^ y ^ z )
104#define F3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) )
105#define F4(x,y,z) ( x ^ y ^ z )
106
107
108#define M(i) ( tm = x[i&0x0f] ^ x[(i-14)&0x0f] \
109 ^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f] \
110 , (x[i&0x0f] = rol(tm, 1)) )
111
112#define R(a,b,c,d,e,f,k,m) do { e += rol( a, 5 ) \
113 + f( b, c, d ) \
114 + k \
115 + m; \
116 b = rol( b, 30 ); \
117 } while(0)
118 R( a, b, c, d, e, F1, K1, x[ 0] );
119 R( e, a, b, c, d, F1, K1, x[ 1] );
120 R( d, e, a, b, c, F1, K1, x[ 2] );
121 R( c, d, e, a, b, F1, K1, x[ 3] );
122 R( b, c, d, e, a, F1, K1, x[ 4] );
123 R( a, b, c, d, e, F1, K1, x[ 5] );
124 R( e, a, b, c, d, F1, K1, x[ 6] );
125 R( d, e, a, b, c, F1, K1, x[ 7] );
126 R( c, d, e, a, b, F1, K1, x[ 8] );
127 R( b, c, d, e, a, F1, K1, x[ 9] );
128 R( a, b, c, d, e, F1, K1, x[10] );
129 R( e, a, b, c, d, F1, K1, x[11] );
130 R( d, e, a, b, c, F1, K1, x[12] );
131 R( c, d, e, a, b, F1, K1, x[13] );
132 R( b, c, d, e, a, F1, K1, x[14] );
133 R( a, b, c, d, e, F1, K1, x[15] );
134 R( e, a, b, c, d, F1, K1, M(16) );
135 R( d, e, a, b, c, F1, K1, M(17) );
136 R( c, d, e, a, b, F1, K1, M(18) );
137 R( b, c, d, e, a, F1, K1, M(19) );
138 R( a, b, c, d, e, F2, K2, M(20) );
139 R( e, a, b, c, d, F2, K2, M(21) );
140 R( d, e, a, b, c, F2, K2, M(22) );
141 R( c, d, e, a, b, F2, K2, M(23) );
142 R( b, c, d, e, a, F2, K2, M(24) );
143 R( a, b, c, d, e, F2, K2, M(25) );
144 R( e, a, b, c, d, F2, K2, M(26) );
145 R( d, e, a, b, c, F2, K2, M(27) );
146 R( c, d, e, a, b, F2, K2, M(28) );
147 R( b, c, d, e, a, F2, K2, M(29) );
148 R( a, b, c, d, e, F2, K2, M(30) );
149 R( e, a, b, c, d, F2, K2, M(31) );
150 R( d, e, a, b, c, F2, K2, M(32) );
151 R( c, d, e, a, b, F2, K2, M(33) );
152 R( b, c, d, e, a, F2, K2, M(34) );
153 R( a, b, c, d, e, F2, K2, M(35) );
154 R( e, a, b, c, d, F2, K2, M(36) );
155 R( d, e, a, b, c, F2, K2, M(37) );
156 R( c, d, e, a, b, F2, K2, M(38) );
157 R( b, c, d, e, a, F2, K2, M(39) );
158 R( a, b, c, d, e, F3, K3, M(40) );
159 R( e, a, b, c, d, F3, K3, M(41) );
160 R( d, e, a, b, c, F3, K3, M(42) );
161 R( c, d, e, a, b, F3, K3, M(43) );
162 R( b, c, d, e, a, F3, K3, M(44) );
163 R( a, b, c, d, e, F3, K3, M(45) );
164 R( e, a, b, c, d, F3, K3, M(46) );
165 R( d, e, a, b, c, F3, K3, M(47) );
166 R( c, d, e, a, b, F3, K3, M(48) );
167 R( b, c, d, e, a, F3, K3, M(49) );
168 R( a, b, c, d, e, F3, K3, M(50) );
169 R( e, a, b, c, d, F3, K3, M(51) );
170 R( d, e, a, b, c, F3, K3, M(52) );
171 R( c, d, e, a, b, F3, K3, M(53) );
172 R( b, c, d, e, a, F3, K3, M(54) );
173 R( a, b, c, d, e, F3, K3, M(55) );
174 R( e, a, b, c, d, F3, K3, M(56) );
175 R( d, e, a, b, c, F3, K3, M(57) );
176 R( c, d, e, a, b, F3, K3, M(58) );
177 R( b, c, d, e, a, F3, K3, M(59) );
178 R( a, b, c, d, e, F4, K4, M(60) );
179 R( e, a, b, c, d, F4, K4, M(61) );
180 R( d, e, a, b, c, F4, K4, M(62) );
181 R( c, d, e, a, b, F4, K4, M(63) );
182 R( b, c, d, e, a, F4, K4, M(64) );
183 R( a, b, c, d, e, F4, K4, M(65) );
184 R( e, a, b, c, d, F4, K4, M(66) );
185 R( d, e, a, b, c, F4, K4, M(67) );
186 R( c, d, e, a, b, F4, K4, M(68) );
187 R( b, c, d, e, a, F4, K4, M(69) );
188 R( a, b, c, d, e, F4, K4, M(70) );
189 R( e, a, b, c, d, F4, K4, M(71) );
190 R( d, e, a, b, c, F4, K4, M(72) );
191 R( c, d, e, a, b, F4, K4, M(73) );
192 R( b, c, d, e, a, F4, K4, M(74) );
193 R( a, b, c, d, e, F4, K4, M(75) );
194 R( e, a, b, c, d, F4, K4, M(76) );
195 R( d, e, a, b, c, F4, K4, M(77) );
196 R( c, d, e, a, b, F4, K4, M(78) );
197 R( b, c, d, e, a, F4, K4, M(79) );
198
199 /* Update chaining vars. */
200 hd->h0 += a;
201 hd->h1 += b;
202 hd->h2 += c;
203 hd->h3 += d;
204 hd->h4 += e;
205}
206
207
208/* Update the message digest with the contents
209 * of INBUF with length INLEN.
210 */
211static void
212sha1_write( void *context, byte *inbuf, size_t inlen)
213{
214 SHA1_CONTEXT *hd = context;
215
216 if( hd->count == 64 ) /* flush the buffer */
217 {
218 transform( hd, hd->buf );
219 _gcry_burn_stack (88+4*sizeof(void*));
220 hd->count = 0;
221 hd->nblocks++;
222 }
223 if( !inbuf )
224 return;
225
226 if( hd->count )
227 {
228 for( ; inlen && hd->count < 64; inlen-- )
229 hd->buf[hd->count++] = *inbuf++;
230 sha1_write( hd, NULL, 0 );
231 if( !inlen )
232 return;
233 }
234
235 while( inlen >= 64 )
236 {
237 transform( hd, inbuf );
238 hd->count = 0;
239 hd->nblocks++;
240 inlen -= 64;
241 inbuf += 64;
242 }
243 _gcry_burn_stack (88+4*sizeof(void*));
244 for( ; inlen && hd->count < 64; inlen-- )
245 hd->buf[hd->count++] = *inbuf++;
246}
247
248
249/* The routine final terminates the computation and
250 * returns the digest.
251 * The handle is prepared for a new cycle, but adding bytes to the
252 * handle will the destroy the returned buffer.
253 * Returns: 20 bytes representing the digest.
254 */
255
256static void
257sha1_final(void *context)
258{
259 SHA1_CONTEXT *hd = context;
260
261 u32 t, msb, lsb;
262 byte *p;
263
264 sha1_write(hd, NULL, 0); /* flush */;
265
266 t = hd->nblocks;
267 /* multiply by 64 to make a byte count */
268 lsb = t << 6;
269 msb = t >> 26;
270 /* add the count */
271 t = lsb;
272 if( (lsb += hd->count) < t )
273 msb++;
274 /* multiply by 8 to make a bit count */
275 t = lsb;
276 lsb <<= 3;
277 msb <<= 3;
278 msb |= t >> 29;
279
280 if( hd->count < 56 ) /* enough room */
281 {
282 hd->buf[hd->count++] = 0x80; /* pad */
283 while( hd->count < 56 )
284 hd->buf[hd->count++] = 0; /* pad */
285 }
286 else /* need one extra block */
287 {
288 hd->buf[hd->count++] = 0x80; /* pad character */
289 while( hd->count < 64 )
290 hd->buf[hd->count++] = 0;
291 sha1_write(hd, NULL, 0); /* flush */;
292 memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
293 }
294 /* append the 64 bit count */
295 hd->buf[56] = msb >> 24;
296 hd->buf[57] = msb >> 16;
297 hd->buf[58] = msb >> 8;
298 hd->buf[59] = msb ;
299 hd->buf[60] = lsb >> 24;
300 hd->buf[61] = lsb >> 16;
301 hd->buf[62] = lsb >> 8;
302 hd->buf[63] = lsb ;
303 transform( hd, hd->buf );
304 _gcry_burn_stack (88+4*sizeof(void*));
305
306 p = hd->buf;
307#ifdef WORDS_BIGENDIAN
308#define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
309#else /* little endian */
310 #define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16; \
311 *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
312#endif
313 X(0);
314 X(1);
315 X(2);
316 X(3);
317 X(4);
318#undef X
319
320}
321
322static byte *
323sha1_read( void *context )
324{
325 SHA1_CONTEXT *hd = context;
326
327 return hd->buf;
328}
329
330/****************
331 * Shortcut functions which puts the hash value of the supplied buffer
332 * into outbuf which must have a size of 20 bytes.
333 */
334void
335_gcry_sha1_hash_buffer (char *outbuf, const char *buffer, size_t length)
336{
337 SHA1_CONTEXT hd;
338
339 sha1_init (&hd);
340 sha1_write (&hd, (byte*)buffer, length);
341 sha1_final (&hd);
342 memcpy (outbuf, hd.buf, 20);
343}
344
345
346static byte asn[15] = /* Object ID is 1.3.14.3.2.26 */
347 { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
348 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 };
349
350static gcry_md_oid_spec_t oid_spec_sha1[] =
351 {
352 /* iso.member-body.us.rsadsi.pkcs.pkcs-1.5 (sha1WithRSAEncryption) */
353 { "1.2.840.113549.1.1.5" },
354 /* iso.member-body.us.x9-57.x9cm.3 (dsaWithSha1)*/
355 { "1.2.840.10040.4.3" },
356 /* from NIST's OIW (sha1) */
357 { "1.3.14.3.2.26" },
358 /* from NIST OIW (sha-1WithRSAEncryption) */
359 { "1.3.14.3.2.29" },
360 { NULL },
361 };
362
363gcry_md_spec_t _gcry_digest_spec_sha1 =
364 {
365 "SHA1", asn, DIM (asn), oid_spec_sha1, 20,
366 sha1_init, sha1_write, sha1_final, sha1_read,
367 sizeof (SHA1_CONTEXT)
368 };
diff --git a/pwmanager/libcrypt/cipher/sha256.c b/pwmanager/libcrypt/cipher/sha256.c
new file mode 100644
index 0000000..712c4e0
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/sha256.c
@@ -0,0 +1,310 @@
1/* sha256.c - SHA256 hash function
2 *Copyright (C) 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21
22/* Test vectors:
23
24 "abc"
25 ba7816bf 8f01cfea 414140de 5dae2223 b00361a3 96177a9c b410ff61 f20015ad
26
27 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
28 248d6a61 d20638b8 e5c02693 0c3e6039 a33ce459 64ff2167 f6ecedd4 19db06c1
29
30 "a" one million times
31 cdc76e5c 9914fb92 81a1c7e2 84d73e67 f1809a48 a497200e 046d39cc c7112cd0
32
33 */
34
35
36#include <config.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <assert.h>
41#include "g10lib.h"
42#include "memory.h"
43#include "bithelp.h"
44#include "cipher.h"
45
46typedef struct {
47 u32 h0,h1,h2,h3,h4,h5,h6,h7;
48 u32 nblocks;
49 byte buf[64];
50 int count;
51} SHA256_CONTEXT;
52
53
54static void
55sha256_init (void *context)
56{
57 SHA256_CONTEXT *hd = context;
58
59 hd->h0 = 0x6a09e667;
60 hd->h1 = 0xbb67ae85;
61 hd->h2 = 0x3c6ef372;
62 hd->h3 = 0xa54ff53a;
63 hd->h4 = 0x510e527f;
64 hd->h5 = 0x9b05688c;
65 hd->h6 = 0x1f83d9ab;
66 hd->h7 = 0x5be0cd19;
67
68 hd->nblocks = 0;
69 hd->count = 0;
70}
71
72
73/*
74 Transform the message X which consists of 16 32-bit-words. See FIPS
75 180-2 for details. */
76#define Cho(x,y,z) (z ^ (x & (y ^ z))) /* (4.2) same as SHA-1's F1 */
77#define Maj(x,y,z) ((x & y) | (z & (x|y))) /* (4.3) same as SHA-1's F3 */
78#define Sum0(x) (ror ((x), 2) ^ ror ((x), 13) ^ ror ((x), 22)) /* (4.4) */
79#define Sum1(x) (ror ((x), 6) ^ ror ((x), 11) ^ ror ((x), 25)) /* (4.5) */
80#define S0(x) (ror ((x), 7) ^ ror ((x), 18) ^ ((x) >> 3)) /* (4.6) */
81#define S1(x) (ror ((x), 17) ^ ror ((x), 19) ^ ((x) >> 10)) /* (4.7) */
82#define R(a,b,c,d,e,f,g,h,k,w) do \
83 { \
84 t1 = (h) + Sum1((e)) + Cho((e),(f),(g)) + (k) + (w); \
85 t2 = Sum0((a)) + Maj((a),(b),(c)); \
86 h = g; \
87 g = f; \
88 f = e; \
89 e = d + t1; \
90 d = c; \
91 c = b; \
92 b = a; \
93 a = t1 + t2; \
94 } while (0)
95
96static void
97transform (SHA256_CONTEXT *hd, byte *data)
98{
99 static const u32 K[64] = {
100 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
101 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
102 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
103 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
104 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
105 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
106 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
107 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
108 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
109 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
110 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
111 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
112 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
113 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
114 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
115 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
116 };
117
118 u32 a,b,c,d,e,f,g,h,t1,t2;
119 u32 x[16];
120 u32 w[64];
121 int i;
122
123 a = hd->h0;
124 b = hd->h1;
125 c = hd->h2;
126 d = hd->h3;
127 e = hd->h4;
128 f = hd->h5;
129 g = hd->h6;
130 h = hd->h7;
131
132#ifdef WORDS_BIGENDIAN
133 memcpy (x, data, 64);
134#else
135 {
136 byte *p2;
137
138 for (i=0, p2=(byte*)x; i < 16; i++, p2 += 4 )
139 {
140 p2[3] = *data++;
141 p2[2] = *data++;
142 p2[1] = *data++;
143 p2[0] = *data++;
144 }
145 }
146#endif
147
148 for (i=0; i < 16; i++)
149 w[i] = x[i];
150 for (; i < 64; i++)
151 w[i] = S1(w[i-2]) + w[i-7] + S0(w[i-15]) + w[i-16];
152
153 for (i=0; i < 64; i++)
154 R(a,b,c,d,e,f,g,h,K[i],w[i]);
155
156 hd->h0 += a;
157 hd->h1 += b;
158 hd->h2 += c;
159 hd->h3 += d;
160 hd->h4 += e;
161 hd->h5 += f;
162 hd->h6 += g;
163 hd->h7 += h;
164}
165#undef Cho
166#undef Maj
167#undef Sum0
168#undef Sum1
169#undef S0
170#undef S1
171#undef R
172
173
174/* Update the message digest with the contents of INBUF with length
175 INLEN. */
176static void
177sha256_write (void *context, byte *inbuf, size_t inlen)
178{
179 SHA256_CONTEXT *hd = context;
180
181 if (hd->count == 64)
182 { /* flush the buffer */
183 transform (hd, hd->buf);
184 _gcry_burn_stack (74*4+32);
185 hd->count = 0;
186 hd->nblocks++;
187 }
188 if (!inbuf)
189 return;
190 if (hd->count)
191 {
192 for (; inlen && hd->count < 64; inlen--)
193 hd->buf[hd->count++] = *inbuf++;
194 sha256_write (hd, NULL, 0);
195 if (!inlen)
196 return;
197 }
198
199 while (inlen >= 64)
200 {
201 transform (hd, inbuf);
202 hd->count = 0;
203 hd->nblocks++;
204 inlen -= 64;
205 inbuf += 64;
206 }
207 _gcry_burn_stack (74*4+32);
208 for (; inlen && hd->count < 64; inlen--)
209 hd->buf[hd->count++] = *inbuf++;
210}
211
212
213/*
214 The routine finally terminates the computation and returns the
215 digest. The handle is prepared for a new cycle, but adding bytes
216 to the handle will the destroy the returned buffer. Returns: 32
217 bytes with the message the digest. */
218static void
219sha256_final(void *context)
220{
221 SHA256_CONTEXT *hd = context;
222 u32 t, msb, lsb;
223 byte *p;
224
225 sha256_write (hd, NULL, 0); /* flush */;
226
227 t = hd->nblocks;
228 /* multiply by 64 to make a byte count */
229 lsb = t << 6;
230 msb = t >> 26;
231 /* add the count */
232 t = lsb;
233 if ((lsb += hd->count) < t)
234 msb++;
235 /* multiply by 8 to make a bit count */
236 t = lsb;
237 lsb <<= 3;
238 msb <<= 3;
239 msb |= t >> 29;
240
241 if (hd->count < 56)
242 { /* enough room */
243 hd->buf[hd->count++] = 0x80; /* pad */
244 while (hd->count < 56)
245 hd->buf[hd->count++] = 0; /* pad */
246 }
247 else
248 { /* need one extra block */
249 hd->buf[hd->count++] = 0x80; /* pad character */
250 while (hd->count < 64)
251 hd->buf[hd->count++] = 0;
252 sha256_write (hd, NULL, 0); /* flush */;
253 memset (hd->buf, 0, 56 ); /* fill next block with zeroes */
254 }
255 /* append the 64 bit count */
256 hd->buf[56] = msb >> 24;
257 hd->buf[57] = msb >> 16;
258 hd->buf[58] = msb >> 8;
259 hd->buf[59] = msb;
260 hd->buf[60] = lsb >> 24;
261 hd->buf[61] = lsb >> 16;
262 hd->buf[62] = lsb >> 8;
263 hd->buf[63] = lsb;
264 transform (hd, hd->buf);
265 _gcry_burn_stack (74*4+32);
266
267 p = hd->buf;
268#ifdef WORDS_BIGENDIAN
269#define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
270#else /* little endian */
271 #define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16; \
272 *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
273#endif
274 X(0);
275 X(1);
276 X(2);
277 X(3);
278 X(4);
279 X(5);
280 X(6);
281 X(7);
282#undef X
283}
284
285static byte *
286sha256_read (void *context)
287{
288 SHA256_CONTEXT *hd = context;
289
290 return hd->buf;
291}
292
293static byte asn[19] = /* Object ID is 2.16.840.1.101.3.4.2.1 */
294 { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
295 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
296 0x00, 0x04, 0x20 };
297
298static gcry_md_oid_spec_t oid_spec_sha256[] =
299 {
300 /* According to the OpenPGG draft rfc2440-bis06 */
301 { "2.16.840.1.101.3.4.2.1" },
302 { NULL },
303 };
304
305gcry_md_spec_t _gcry_digest_spec_sha256 =
306 {
307 "SHA256", asn, DIM (asn), oid_spec_sha256, 32,
308 sha256_init, sha256_write, sha256_final, sha256_read,
309 sizeof (SHA256_CONTEXT)
310 };
diff --git a/pwmanager/libcrypt/cipher/sha512.c b/pwmanager/libcrypt/cipher/sha512.c
new file mode 100644
index 0000000..e2160eb
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/sha512.c
@@ -0,0 +1,400 @@
1/* sha512.c - SHA384 and SHA512 hash functions
2 *Copyright (C) 2003 Free Software Foundation, Inc.
3 *
4 * Please see below for more legal information!
5 *
6 * This file is part of Libgcrypt.
7 *
8 * Libgcrypt is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser general Public License as
10 * published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
12 *
13 * Libgcrypt is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 */
22
23
24/* Test vectors from FIPS-180-2:
25 *
26 * "abc"
27 * 384:
28 * CB00753F 45A35E8B B5A03D69 9AC65007 272C32AB 0EDED163
29 * 1A8B605A 43FF5BED 8086072B A1E7CC23 58BAECA1 34C825A7
30 * 512:
31 * DDAF35A1 93617ABA CC417349 AE204131 12E6FA4E 89A97EA2 0A9EEEE6 4B55D39A
32 * 2192992A 274FC1A8 36BA3C23 A3FEEBBD 454D4423 643CE80E 2A9AC94F A54CA49F
33 *
34 * "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
35 * 384:
36 * 09330C33 F71147E8 3D192FC7 82CD1B47 53111B17 3B3B05D2
37 * 2FA08086 E3B0F712 FCC7C71A 557E2DB9 66C3E9FA 91746039
38 * 512:
39 * 8E959B75 DAE313DA 8CF4F728 14FC143F 8F7779C6 EB9F7FA1 7299AEAD B6889018
40 * 501D289E 4900F7E4 331B99DE C4B5433A C7D329EE B6DD2654 5E96E55B 874BE909
41 *
42 * "a" x 1000000
43 * 384:
44 * 9D0E1809 716474CB 086E834E 310A4A1C ED149E9C 00F24852
45 * 7972CEC5 704C2A5B 07B8B3DC 38ECC4EB AE97DDD8 7F3D8985
46 * 512:
47 * E718483D 0CE76964 4E2E42C7 BC15B463 8E1F98B1 3B204428 5632A803 AFA973EB
48 * DE0FF244 877EA60A 4CB0432C E577C31B EB009C5C 2C49AA2E 4EADB217 AD8CC09B
49 */
50
51
52#include <config.h>
53#include <string.h>
54#include "g10lib.h"
55#include "bithelp.h"
56#include "cipher.h"
57
58typedef struct
59{
60 u64 h0, h1, h2, h3, h4, h5, h6, h7;
61 u64 nblocks;
62 byte buf[128];
63 int count;
64} SHA512_CONTEXT;
65
66static void
67sha512_init (void *context)
68{
69 SHA512_CONTEXT *hd = context;
70
71 hd->h0 = U64_C(0x6a09e667f3bcc908);
72 hd->h1 = U64_C(0xbb67ae8584caa73b);
73 hd->h2 = U64_C(0x3c6ef372fe94f82b);
74 hd->h3 = U64_C(0xa54ff53a5f1d36f1);
75 hd->h4 = U64_C(0x510e527fade682d1);
76 hd->h5 = U64_C(0x9b05688c2b3e6c1f);
77 hd->h6 = U64_C(0x1f83d9abfb41bd6b);
78 hd->h7 = U64_C(0x5be0cd19137e2179);
79
80 hd->nblocks = 0;
81 hd->count = 0;
82}
83
84static void
85sha384_init (void *context)
86{
87 SHA512_CONTEXT *hd = context;
88
89 hd->h0 = U64_C(0xcbbb9d5dc1059ed8);
90 hd->h1 = U64_C(0x629a292a367cd507);
91 hd->h2 = U64_C(0x9159015a3070dd17);
92 hd->h3 = U64_C(0x152fecd8f70e5939);
93 hd->h4 = U64_C(0x67332667ffc00b31);
94 hd->h5 = U64_C(0x8eb44a8768581511);
95 hd->h6 = U64_C(0xdb0c2e0d64f98fa7);
96 hd->h7 = U64_C(0x47b5481dbefa4fa4);
97
98 hd->nblocks = 0;
99 hd->count = 0;
100}
101
102
103/****************
104 * Transform the message W which consists of 16 64-bit-words
105 */
106static void
107transform (SHA512_CONTEXT *hd, byte *data)
108{
109 u64 a, b, c, d, e, f, g, h;
110 u64 w[80];
111 int t;
112 static const u64 k[] =
113 {
114 U64_C(0x428a2f98d728ae22), U64_C(0x7137449123ef65cd),
115 U64_C(0xb5c0fbcfec4d3b2f), U64_C(0xe9b5dba58189dbbc),
116 U64_C(0x3956c25bf348b538), U64_C(0x59f111f1b605d019),
117 U64_C(0x923f82a4af194f9b), U64_C(0xab1c5ed5da6d8118),
118 U64_C(0xd807aa98a3030242), U64_C(0x12835b0145706fbe),
119 U64_C(0x243185be4ee4b28c), U64_C(0x550c7dc3d5ffb4e2),
120 U64_C(0x72be5d74f27b896f), U64_C(0x80deb1fe3b1696b1),
121 U64_C(0x9bdc06a725c71235), U64_C(0xc19bf174cf692694),
122 U64_C(0xe49b69c19ef14ad2), U64_C(0xefbe4786384f25e3),
123 U64_C(0x0fc19dc68b8cd5b5), U64_C(0x240ca1cc77ac9c65),
124 U64_C(0x2de92c6f592b0275), U64_C(0x4a7484aa6ea6e483),
125 U64_C(0x5cb0a9dcbd41fbd4), U64_C(0x76f988da831153b5),
126 U64_C(0x983e5152ee66dfab), U64_C(0xa831c66d2db43210),
127 U64_C(0xb00327c898fb213f), U64_C(0xbf597fc7beef0ee4),
128 U64_C(0xc6e00bf33da88fc2), U64_C(0xd5a79147930aa725),
129 U64_C(0x06ca6351e003826f), U64_C(0x142929670a0e6e70),
130 U64_C(0x27b70a8546d22ffc), U64_C(0x2e1b21385c26c926),
131 U64_C(0x4d2c6dfc5ac42aed), U64_C(0x53380d139d95b3df),
132 U64_C(0x650a73548baf63de), U64_C(0x766a0abb3c77b2a8),
133 U64_C(0x81c2c92e47edaee6), U64_C(0x92722c851482353b),
134 U64_C(0xa2bfe8a14cf10364), U64_C(0xa81a664bbc423001),
135 U64_C(0xc24b8b70d0f89791), U64_C(0xc76c51a30654be30),
136 U64_C(0xd192e819d6ef5218), U64_C(0xd69906245565a910),
137 U64_C(0xf40e35855771202a), U64_C(0x106aa07032bbd1b8),
138 U64_C(0x19a4c116b8d2d0c8), U64_C(0x1e376c085141ab53),
139 U64_C(0x2748774cdf8eeb99), U64_C(0x34b0bcb5e19b48a8),
140 U64_C(0x391c0cb3c5c95a63), U64_C(0x4ed8aa4ae3418acb),
141 U64_C(0x5b9cca4f7763e373), U64_C(0x682e6ff3d6b2b8a3),
142 U64_C(0x748f82ee5defb2fc), U64_C(0x78a5636f43172f60),
143 U64_C(0x84c87814a1f0ab72), U64_C(0x8cc702081a6439ec),
144 U64_C(0x90befffa23631e28), U64_C(0xa4506cebde82bde9),
145 U64_C(0xbef9a3f7b2c67915), U64_C(0xc67178f2e372532b),
146 U64_C(0xca273eceea26619c), U64_C(0xd186b8c721c0c207),
147 U64_C(0xeada7dd6cde0eb1e), U64_C(0xf57d4f7fee6ed178),
148 U64_C(0x06f067aa72176fba), U64_C(0x0a637dc5a2c898a6),
149 U64_C(0x113f9804bef90dae), U64_C(0x1b710b35131c471b),
150 U64_C(0x28db77f523047d84), U64_C(0x32caab7b40c72493),
151 U64_C(0x3c9ebe0a15c9bebc), U64_C(0x431d67c49c100d4c),
152 U64_C(0x4cc5d4becb3e42b6), U64_C(0x597f299cfc657e2a),
153 U64_C(0x5fcb6fab3ad6faec), U64_C(0x6c44198c4a475817)
154 };
155
156 /* get values from the chaining vars */
157 a = hd->h0;
158 b = hd->h1;
159 c = hd->h2;
160 d = hd->h3;
161 e = hd->h4;
162 f = hd->h5;
163 g = hd->h6;
164 h = hd->h7;
165
166#ifdef WORDS_BIGENDIAN
167 memcpy (w, data, 128);
168#else
169 {
170 int i;
171 byte *p2;
172
173 for (i = 0, p2 = (byte *) w; i < 16; i++, p2 += 8)
174 {
175 p2[7] = *data++;
176 p2[6] = *data++;
177 p2[5] = *data++;
178 p2[4] = *data++;
179 p2[3] = *data++;
180 p2[2] = *data++;
181 p2[1] = *data++;
182 p2[0] = *data++;
183 }
184 }
185#endif
186
187#define ROTR(x,n) (((x)>>(n)) | ((x)<<(64-(n))))
188#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
189#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
190#define Sum0(x) (ROTR((x),28) ^ ROTR((x),34) ^ ROTR((x),39))
191#define Sum1(x) (ROTR((x),14) ^ ROTR((x),18) ^ ROTR((x),41))
192#define S0(x) (ROTR((x),1) ^ ROTR((x),8) ^ ((x)>>7))
193#define S1(x) (ROTR((x),19) ^ ROTR((x),61) ^ ((x)>>6))
194
195 for (t = 16; t < 80; t++)
196 w[t] = S1 (w[t - 2]) + w[t - 7] + S0 (w[t - 15]) + w[t - 16];
197
198 for (t = 0; t < 80; t++)
199 {
200 u64 t1, t2;
201
202 t1 = h + Sum1 (e) + Ch (e, f, g) + k[t] + w[t];
203 t2 = Sum0 (a) + Maj (a, b, c);
204 h = g;
205 g = f;
206 f = e;
207 e = d + t1;
208 d = c;
209 c = b;
210 b = a;
211 a = t1 + t2;
212
213 /* printf("t=%d a=%016llX b=%016llX c=%016llX d=%016llX "
214 "e=%016llX f=%016llX g=%016llX h=%016llX\n",t,a,b,c,d,e,f,g,h); */
215 }
216
217 /* update chaining vars */
218 hd->h0 += a;
219 hd->h1 += b;
220 hd->h2 += c;
221 hd->h3 += d;
222 hd->h4 += e;
223 hd->h5 += f;
224 hd->h6 += g;
225 hd->h7 += h;
226}
227
228
229/* Update the message digest with the contents
230 * of INBUF with length INLEN.
231 */
232static void
233sha512_write (void *context, byte *inbuf, size_t inlen)
234{
235 SHA512_CONTEXT *hd = context;
236
237 if (hd->count == 128)
238 { /* flush the buffer */
239 transform (hd, hd->buf);
240 _gcry_burn_stack (768);
241 hd->count = 0;
242 hd->nblocks++;
243 }
244 if (!inbuf)
245 return;
246 if (hd->count)
247 {
248 for (; inlen && hd->count < 128; inlen--)
249 hd->buf[hd->count++] = *inbuf++;
250 sha512_write (context, NULL, 0);
251 if (!inlen)
252 return;
253 }
254
255 while (inlen >= 128)
256 {
257 transform (hd, inbuf);
258 hd->count = 0;
259 hd->nblocks++;
260 inlen -= 128;
261 inbuf += 128;
262 }
263 _gcry_burn_stack (768);
264 for (; inlen && hd->count < 128; inlen--)
265 hd->buf[hd->count++] = *inbuf++;
266}
267
268
269/* The routine final terminates the computation and
270 * returns the digest.
271 * The handle is prepared for a new cycle, but adding bytes to the
272 * handle will the destroy the returned buffer.
273 * Returns: 64 bytes representing the digest. When used for sha384,
274 * we take the leftmost 48 of those bytes.
275 */
276
277static void
278sha512_final (void *context)
279{
280 SHA512_CONTEXT *hd = context;
281 u64 t, msb, lsb;
282 byte *p;
283
284 sha512_write (context, NULL, 0); /* flush */ ;
285
286 t = hd->nblocks;
287 /* multiply by 128 to make a byte count */
288 lsb = t << 7;
289 msb = t >> 57;
290 /* add the count */
291 t = lsb;
292 if ((lsb += hd->count) < t)
293 msb++;
294 /* multiply by 8 to make a bit count */
295 t = lsb;
296 lsb <<= 3;
297 msb <<= 3;
298 msb |= t >> 61;
299
300 if (hd->count < 112)
301 { /* enough room */
302 hd->buf[hd->count++] = 0x80;/* pad */
303 while (hd->count < 112)
304 hd->buf[hd->count++] = 0;/* pad */
305 }
306 else
307 { /* need one extra block */
308 hd->buf[hd->count++] = 0x80;/* pad character */
309 while (hd->count < 128)
310 hd->buf[hd->count++] = 0;
311 sha512_write (context, NULL, 0); /* flush */ ;
312 memset (hd->buf, 0, 112);/* fill next block with zeroes */
313 }
314 /* append the 128 bit count */
315 hd->buf[112] = msb >> 56;
316 hd->buf[113] = msb >> 48;
317 hd->buf[114] = msb >> 40;
318 hd->buf[115] = msb >> 32;
319 hd->buf[116] = msb >> 24;
320 hd->buf[117] = msb >> 16;
321 hd->buf[118] = msb >> 8;
322 hd->buf[119] = msb;
323
324 hd->buf[120] = lsb >> 56;
325 hd->buf[121] = lsb >> 48;
326 hd->buf[122] = lsb >> 40;
327 hd->buf[123] = lsb >> 32;
328 hd->buf[124] = lsb >> 24;
329 hd->buf[125] = lsb >> 16;
330 hd->buf[126] = lsb >> 8;
331 hd->buf[127] = lsb;
332 transform (hd, hd->buf);
333 _gcry_burn_stack (768);
334
335 p = hd->buf;
336#ifdef WORDS_BIGENDIAN
337#define X(a) do { *(u64*)p = hd->h##a ; p += 8; } while (0)
338#else /* little endian */
339 #define X(a) do { *p++ = hd->h##a >> 56; *p++ = hd->h##a >> 48; \
340 *p++ = hd->h##a >> 40; *p++ = hd->h##a >> 32; \
341 *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16; \
342 *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while (0)
343#endif
344 X (0);
345 X (1);
346 X (2);
347 X (3);
348 X (4);
349 X (5);
350 /* Note that these last two chunks are included even for SHA384.
351 We just ignore them. */
352 X (6);
353 X (7);
354#undef X
355}
356
357static byte *
358sha512_read (void *context)
359{
360 SHA512_CONTEXT *hd = (SHA512_CONTEXT *) context;
361 return hd->buf;
362}
363
364 static byte sha512_asn[] =/* Object ID is 2.16.840.1.101.3.4.2.3 */
365{
366 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
367 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05,
368 0x00, 0x04, 0x40
369};
370
371static gcry_md_oid_spec_t oid_spec_sha512[] =
372 {
373 { "2.16.840.1.101.3.4.2.3" },
374 { NULL }
375 };
376
377gcry_md_spec_t _gcry_digest_spec_sha512 = {
378 "SHA512", sha512_asn, DIM (sha512_asn), oid_spec_sha512, 64,
379 sha512_init, sha512_write, sha512_final, sha512_read,
380 sizeof (SHA512_CONTEXT),
381};
382
383 static byte sha384_asn[] =/* Object ID is 2.16.840.1.101.3.4.2.2 */
384{
385 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
386 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05,
387 0x00, 0x04, 0x30
388};
389
390static gcry_md_oid_spec_t oid_spec_sha384[] =
391 {
392 { "2.16.840.1.101.3.4.2.2" },
393 { NULL },
394 };
395
396gcry_md_spec_t _gcry_digest_spec_sha384 = {
397 "SHA384", sha384_asn, DIM (sha384_asn), oid_spec_sha384, 48,
398 sha384_init, sha512_write, sha512_final, sha512_read,
399 sizeof (SHA512_CONTEXT),
400};
diff --git a/pwmanager/libcrypt/cipher/tiger.c b/pwmanager/libcrypt/cipher/tiger.c
new file mode 100644
index 0000000..a92e812
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/tiger.c
@@ -0,0 +1,850 @@
1/* tiger.c - The TIGER hash function
2 *Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <config.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <assert.h>
26#include "g10lib.h"
27#include "memory.h"
28#include "cipher.h"
29
30#ifdef HAVE_U64_TYPEDEF
31
32/* we really need it here, but as this is only experiment we
33 * can live without Tiger */
34
35typedef struct {
36 u64 a, b, c;
37 byte buf[64];
38 int count;
39 u32 nblocks;
40} TIGER_CONTEXT;
41
42
43/*********************************
44 * Okay, okay, this is not the fastest code - improvements are welcome.
45 *
46 */
47
48/* Some test vectors:
49 * "" 24F0130C63AC9332 16166E76B1BB925F F373DE2D49584E7A
50 * "abc" F258C1E88414AB2A 527AB541FFC5B8BF 935F7B951C132951
51 * "Tiger" 9F00F599072300DD 276ABB38C8EB6DEC 37790C116F9D2BDF
52 * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-"
53 * 87FB2A9083851CF7 470D2CF810E6DF9E B586445034A5A386
54 * "ABCDEFGHIJKLMNOPQRSTUVWXYZ=abcdefghijklmnopqrstuvwxyz+0123456789"
55 * 467DB80863EBCE48 8DF1CD1261655DE9 57896565975F9197
56 * "Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham"
57 * 0C410A042968868A 1671DA5A3FD29A72 5EC1E457D3CDB303
58 * "Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham, proc"
59 * "eedings of Fast Software Encryption 3, Cambridge."
60 * EBF591D5AFA655CE 7F22894FF87F54AC 89C811B6B0DA3193
61 * "Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham, proc"
62 * "eedings of Fast Software Encryption 3, Cambridge, 1996."
63 * 3D9AEB03D1BD1A63 57B2774DFD6D5B24 DD68151D503974FC
64 * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-ABCDEF"
65 * "GHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-"
66 * 00B83EB4E53440C5 76AC6AAEE0A74858 25FD15E70A59FFE4
67 */
68
69static u64 sbox1[256] = {
70 U64_C(0x02aab17cf7e90c5e) /* 0 */, U64_C(0xac424b03e243a8ec) /* 1 */,
71 U64_C(0x72cd5be30dd5fcd3) /* 2 */, U64_C(0x6d019b93f6f97f3a) /* 3 */,
72 U64_C(0xcd9978ffd21f9193) /* 4 */, U64_C(0x7573a1c9708029e2) /* 5 */,
73 U64_C(0xb164326b922a83c3) /* 6 */, U64_C(0x46883eee04915870) /* 7 */,
74 U64_C(0xeaace3057103ece6) /* 8 */, U64_C(0xc54169b808a3535c) /* 9 */,
75 U64_C(0x4ce754918ddec47c) /* 10 */, U64_C(0x0aa2f4dfdc0df40c) /* 11 */,
76 U64_C(0x10b76f18a74dbefa) /* 12 */, U64_C(0xc6ccb6235ad1ab6a) /* 13 */,
77 U64_C(0x13726121572fe2ff) /* 14 */, U64_C(0x1a488c6f199d921e) /* 15 */,
78 U64_C(0x4bc9f9f4da0007ca) /* 16 */, U64_C(0x26f5e6f6e85241c7) /* 17 */,
79 U64_C(0x859079dbea5947b6) /* 18 */, U64_C(0x4f1885c5c99e8c92) /* 19 */,
80 U64_C(0xd78e761ea96f864b) /* 20 */, U64_C(0x8e36428c52b5c17d) /* 21 */,
81 U64_C(0x69cf6827373063c1) /* 22 */, U64_C(0xb607c93d9bb4c56e) /* 23 */,
82 U64_C(0x7d820e760e76b5ea) /* 24 */, U64_C(0x645c9cc6f07fdc42) /* 25 */,
83 U64_C(0xbf38a078243342e0) /* 26 */, U64_C(0x5f6b343c9d2e7d04) /* 27 */,
84 U64_C(0xf2c28aeb600b0ec6) /* 28 */, U64_C(0x6c0ed85f7254bcac) /* 29 */,
85 U64_C(0x71592281a4db4fe5) /* 30 */, U64_C(0x1967fa69ce0fed9f) /* 31 */,
86 U64_C(0xfd5293f8b96545db) /* 32 */, U64_C(0xc879e9d7f2a7600b) /* 33 */,
87 U64_C(0x860248920193194e) /* 34 */, U64_C(0xa4f9533b2d9cc0b3) /* 35 */,
88 U64_C(0x9053836c15957613) /* 36 */, U64_C(0xdb6dcf8afc357bf1) /* 37 */,
89 U64_C(0x18beea7a7a370f57) /* 38 */, U64_C(0x037117ca50b99066) /* 39 */,
90 U64_C(0x6ab30a9774424a35) /* 40 */, U64_C(0xf4e92f02e325249b) /* 41 */,
91 U64_C(0x7739db07061ccae1) /* 42 */, U64_C(0xd8f3b49ceca42a05) /* 43 */,
92 U64_C(0xbd56be3f51382f73) /* 44 */, U64_C(0x45faed5843b0bb28) /* 45 */,
93 U64_C(0x1c813d5c11bf1f83) /* 46 */, U64_C(0x8af0e4b6d75fa169) /* 47 */,
94 U64_C(0x33ee18a487ad9999) /* 48 */, U64_C(0x3c26e8eab1c94410) /* 49 */,
95 U64_C(0xb510102bc0a822f9) /* 50 */, U64_C(0x141eef310ce6123b) /* 51 */,
96 U64_C(0xfc65b90059ddb154) /* 52 */, U64_C(0xe0158640c5e0e607) /* 53 */,
97 U64_C(0x884e079826c3a3cf) /* 54 */, U64_C(0x930d0d9523c535fd) /* 55 */,
98 U64_C(0x35638d754e9a2b00) /* 56 */, U64_C(0x4085fccf40469dd5) /* 57 */,
99 U64_C(0xc4b17ad28be23a4c) /* 58 */, U64_C(0xcab2f0fc6a3e6a2e) /* 59 */,
100 U64_C(0x2860971a6b943fcd) /* 60 */, U64_C(0x3dde6ee212e30446) /* 61 */,
101 U64_C(0x6222f32ae01765ae) /* 62 */, U64_C(0x5d550bb5478308fe) /* 63 */,
102 U64_C(0xa9efa98da0eda22a) /* 64 */, U64_C(0xc351a71686c40da7) /* 65 */,
103 U64_C(0x1105586d9c867c84) /* 66 */, U64_C(0xdcffee85fda22853) /* 67 */,
104 U64_C(0xccfbd0262c5eef76) /* 68 */, U64_C(0xbaf294cb8990d201) /* 69 */,
105 U64_C(0xe69464f52afad975) /* 70 */, U64_C(0x94b013afdf133e14) /* 71 */,
106 U64_C(0x06a7d1a32823c958) /* 72 */, U64_C(0x6f95fe5130f61119) /* 73 */,
107 U64_C(0xd92ab34e462c06c0) /* 74 */, U64_C(0xed7bde33887c71d2) /* 75 */,
108 U64_C(0x79746d6e6518393e) /* 76 */, U64_C(0x5ba419385d713329) /* 77 */,
109 U64_C(0x7c1ba6b948a97564) /* 78 */, U64_C(0x31987c197bfdac67) /* 79 */,
110 U64_C(0xde6c23c44b053d02) /* 80 */, U64_C(0x581c49fed002d64d) /* 81 */,
111 U64_C(0xdd474d6338261571) /* 82 */, U64_C(0xaa4546c3e473d062) /* 83 */,
112 U64_C(0x928fce349455f860) /* 84 */, U64_C(0x48161bbacaab94d9) /* 85 */,
113 U64_C(0x63912430770e6f68) /* 86 */, U64_C(0x6ec8a5e602c6641c) /* 87 */,
114 U64_C(0x87282515337ddd2b) /* 88 */, U64_C(0x2cda6b42034b701b) /* 89 */,
115 U64_C(0xb03d37c181cb096d) /* 90 */, U64_C(0xe108438266c71c6f) /* 91 */,
116 U64_C(0x2b3180c7eb51b255) /* 92 */, U64_C(0xdf92b82f96c08bbc) /* 93 */,
117 U64_C(0x5c68c8c0a632f3ba) /* 94 */, U64_C(0x5504cc861c3d0556) /* 95 */,
118 U64_C(0xabbfa4e55fb26b8f) /* 96 */, U64_C(0x41848b0ab3baceb4) /* 97 */,
119 U64_C(0xb334a273aa445d32) /* 98 */, U64_C(0xbca696f0a85ad881) /* 99 */,
120 U64_C(0x24f6ec65b528d56c) /* 100 */, U64_C(0x0ce1512e90f4524a) /* 101 */,
121 U64_C(0x4e9dd79d5506d35a) /* 102 */, U64_C(0x258905fac6ce9779) /* 103 */,
122 U64_C(0x2019295b3e109b33) /* 104 */, U64_C(0xf8a9478b73a054cc) /* 105 */,
123 U64_C(0x2924f2f934417eb0) /* 106 */, U64_C(0x3993357d536d1bc4) /* 107 */,
124 U64_C(0x38a81ac21db6ff8b) /* 108 */, U64_C(0x47c4fbf17d6016bf) /* 109 */,
125 U64_C(0x1e0faadd7667e3f5) /* 110 */, U64_C(0x7abcff62938beb96) /* 111 */,
126 U64_C(0xa78dad948fc179c9) /* 112 */, U64_C(0x8f1f98b72911e50d) /* 113 */,
127 U64_C(0x61e48eae27121a91) /* 114 */, U64_C(0x4d62f7ad31859808) /* 115 */,
128 U64_C(0xeceba345ef5ceaeb) /* 116 */, U64_C(0xf5ceb25ebc9684ce) /* 117 */,
129 U64_C(0xf633e20cb7f76221) /* 118 */, U64_C(0xa32cdf06ab8293e4) /* 119 */,
130 U64_C(0x985a202ca5ee2ca4) /* 120 */, U64_C(0xcf0b8447cc8a8fb1) /* 121 */,
131 U64_C(0x9f765244979859a3) /* 122 */, U64_C(0xa8d516b1a1240017) /* 123 */,
132 U64_C(0x0bd7ba3ebb5dc726) /* 124 */, U64_C(0xe54bca55b86adb39) /* 125 */,
133 U64_C(0x1d7a3afd6c478063) /* 126 */, U64_C(0x519ec608e7669edd) /* 127 */,
134 U64_C(0x0e5715a2d149aa23) /* 128 */, U64_C(0x177d4571848ff194) /* 129 */,
135 U64_C(0xeeb55f3241014c22) /* 130 */, U64_C(0x0f5e5ca13a6e2ec2) /* 131 */,
136 U64_C(0x8029927b75f5c361) /* 132 */, U64_C(0xad139fabc3d6e436) /* 133 */,
137 U64_C(0x0d5df1a94ccf402f) /* 134 */, U64_C(0x3e8bd948bea5dfc8) /* 135 */,
138 U64_C(0xa5a0d357bd3ff77e) /* 136 */, U64_C(0xa2d12e251f74f645) /* 137 */,
139 U64_C(0x66fd9e525e81a082) /* 138 */, U64_C(0x2e0c90ce7f687a49) /* 139 */,
140 U64_C(0xc2e8bcbeba973bc5) /* 140 */, U64_C(0x000001bce509745f) /* 141 */,
141 U64_C(0x423777bbe6dab3d6) /* 142 */, U64_C(0xd1661c7eaef06eb5) /* 143 */,
142 U64_C(0xa1781f354daacfd8) /* 144 */, U64_C(0x2d11284a2b16affc) /* 145 */,
143 U64_C(0xf1fc4f67fa891d1f) /* 146 */, U64_C(0x73ecc25dcb920ada) /* 147 */,
144 U64_C(0xae610c22c2a12651) /* 148 */, U64_C(0x96e0a810d356b78a) /* 149 */,
145 U64_C(0x5a9a381f2fe7870f) /* 150 */, U64_C(0xd5ad62ede94e5530) /* 151 */,
146 U64_C(0xd225e5e8368d1427) /* 152 */, U64_C(0x65977b70c7af4631) /* 153 */,
147 U64_C(0x99f889b2de39d74f) /* 154 */, U64_C(0x233f30bf54e1d143) /* 155 */,
148 U64_C(0x9a9675d3d9a63c97) /* 156 */, U64_C(0x5470554ff334f9a8) /* 157 */,
149 U64_C(0x166acb744a4f5688) /* 158 */, U64_C(0x70c74caab2e4aead) /* 159 */,
150 U64_C(0xf0d091646f294d12) /* 160 */, U64_C(0x57b82a89684031d1) /* 161 */,
151 U64_C(0xefd95a5a61be0b6b) /* 162 */, U64_C(0x2fbd12e969f2f29a) /* 163 */,
152 U64_C(0x9bd37013feff9fe8) /* 164 */, U64_C(0x3f9b0404d6085a06) /* 165 */,
153 U64_C(0x4940c1f3166cfe15) /* 166 */, U64_C(0x09542c4dcdf3defb) /* 167 */,
154 U64_C(0xb4c5218385cd5ce3) /* 168 */, U64_C(0xc935b7dc4462a641) /* 169 */,
155 U64_C(0x3417f8a68ed3b63f) /* 170 */, U64_C(0xb80959295b215b40) /* 171 */,
156 U64_C(0xf99cdaef3b8c8572) /* 172 */, U64_C(0x018c0614f8fcb95d) /* 173 */,
157 U64_C(0x1b14accd1a3acdf3) /* 174 */, U64_C(0x84d471f200bb732d) /* 175 */,
158 U64_C(0xc1a3110e95e8da16) /* 176 */, U64_C(0x430a7220bf1a82b8) /* 177 */,
159 U64_C(0xb77e090d39df210e) /* 178 */, U64_C(0x5ef4bd9f3cd05e9d) /* 179 */,
160 U64_C(0x9d4ff6da7e57a444) /* 180 */, U64_C(0xda1d60e183d4a5f8) /* 181 */,
161 U64_C(0xb287c38417998e47) /* 182 */, U64_C(0xfe3edc121bb31886) /* 183 */,
162 U64_C(0xc7fe3ccc980ccbef) /* 184 */, U64_C(0xe46fb590189bfd03) /* 185 */,
163 U64_C(0x3732fd469a4c57dc) /* 186 */, U64_C(0x7ef700a07cf1ad65) /* 187 */,
164 U64_C(0x59c64468a31d8859) /* 188 */, U64_C(0x762fb0b4d45b61f6) /* 189 */,
165 U64_C(0x155baed099047718) /* 190 */, U64_C(0x68755e4c3d50baa6) /* 191 */,
166 U64_C(0xe9214e7f22d8b4df) /* 192 */, U64_C(0x2addbf532eac95f4) /* 193 */,
167 U64_C(0x32ae3909b4bd0109) /* 194 */, U64_C(0x834df537b08e3450) /* 195 */,
168 U64_C(0xfa209da84220728d) /* 196 */, U64_C(0x9e691d9b9efe23f7) /* 197 */,
169 U64_C(0x0446d288c4ae8d7f) /* 198 */, U64_C(0x7b4cc524e169785b) /* 199 */,
170 U64_C(0x21d87f0135ca1385) /* 200 */, U64_C(0xcebb400f137b8aa5) /* 201 */,
171 U64_C(0x272e2b66580796be) /* 202 */, U64_C(0x3612264125c2b0de) /* 203 */,
172 U64_C(0x057702bdad1efbb2) /* 204 */, U64_C(0xd4babb8eacf84be9) /* 205 */,
173 U64_C(0x91583139641bc67b) /* 206 */, U64_C(0x8bdc2de08036e024) /* 207 */,
174 U64_C(0x603c8156f49f68ed) /* 208 */, U64_C(0xf7d236f7dbef5111) /* 209 */,
175 U64_C(0x9727c4598ad21e80) /* 210 */, U64_C(0xa08a0896670a5fd7) /* 211 */,
176 U64_C(0xcb4a8f4309eba9cb) /* 212 */, U64_C(0x81af564b0f7036a1) /* 213 */,
177 U64_C(0xc0b99aa778199abd) /* 214 */, U64_C(0x959f1ec83fc8e952) /* 215 */,
178 U64_C(0x8c505077794a81b9) /* 216 */, U64_C(0x3acaaf8f056338f0) /* 217 */,
179 U64_C(0x07b43f50627a6778) /* 218 */, U64_C(0x4a44ab49f5eccc77) /* 219 */,
180 U64_C(0x3bc3d6e4b679ee98) /* 220 */, U64_C(0x9cc0d4d1cf14108c) /* 221 */,
181 U64_C(0x4406c00b206bc8a0) /* 222 */, U64_C(0x82a18854c8d72d89) /* 223 */,
182 U64_C(0x67e366b35c3c432c) /* 224 */, U64_C(0xb923dd61102b37f2) /* 225 */,
183 U64_C(0x56ab2779d884271d) /* 226 */, U64_C(0xbe83e1b0ff1525af) /* 227 */,
184 U64_C(0xfb7c65d4217e49a9) /* 228 */, U64_C(0x6bdbe0e76d48e7d4) /* 229 */,
185 U64_C(0x08df828745d9179e) /* 230 */, U64_C(0x22ea6a9add53bd34) /* 231 */,
186 U64_C(0xe36e141c5622200a) /* 232 */, U64_C(0x7f805d1b8cb750ee) /* 233 */,
187 U64_C(0xafe5c7a59f58e837) /* 234 */, U64_C(0xe27f996a4fb1c23c) /* 235 */,
188 U64_C(0xd3867dfb0775f0d0) /* 236 */, U64_C(0xd0e673de6e88891a) /* 237 */,
189 U64_C(0x123aeb9eafb86c25) /* 238 */, U64_C(0x30f1d5d5c145b895) /* 239 */,
190 U64_C(0xbb434a2dee7269e7) /* 240 */, U64_C(0x78cb67ecf931fa38) /* 241 */,
191 U64_C(0xf33b0372323bbf9c) /* 242 */, U64_C(0x52d66336fb279c74) /* 243 */,
192 U64_C(0x505f33ac0afb4eaa) /* 244 */, U64_C(0xe8a5cd99a2cce187) /* 245 */,
193 U64_C(0x534974801e2d30bb) /* 246 */, U64_C(0x8d2d5711d5876d90) /* 247 */,
194 U64_C(0x1f1a412891bc038e) /* 248 */, U64_C(0xd6e2e71d82e56648) /* 249 */,
195 U64_C(0x74036c3a497732b7) /* 250 */, U64_C(0x89b67ed96361f5ab) /* 251 */,
196 U64_C(0xffed95d8f1ea02a2) /* 252 */, U64_C(0xe72b3bd61464d43d) /* 253 */,
197 U64_C(0xa6300f170bdc4820) /* 254 */, U64_C(0xebc18760ed78a77a) /* 255 */
198};
199static u64 sbox2[256] = {
200 U64_C(0xe6a6be5a05a12138) /* 256 */, U64_C(0xb5a122a5b4f87c98) /* 257 */,
201 U64_C(0x563c6089140b6990) /* 258 */, U64_C(0x4c46cb2e391f5dd5) /* 259 */,
202 U64_C(0xd932addbc9b79434) /* 260 */, U64_C(0x08ea70e42015aff5) /* 261 */,
203 U64_C(0xd765a6673e478cf1) /* 262 */, U64_C(0xc4fb757eab278d99) /* 263 */,
204 U64_C(0xdf11c6862d6e0692) /* 264 */, U64_C(0xddeb84f10d7f3b16) /* 265 */,
205 U64_C(0x6f2ef604a665ea04) /* 266 */, U64_C(0x4a8e0f0ff0e0dfb3) /* 267 */,
206 U64_C(0xa5edeef83dbcba51) /* 268 */, U64_C(0xfc4f0a2a0ea4371e) /* 269 */,
207 U64_C(0xe83e1da85cb38429) /* 270 */, U64_C(0xdc8ff882ba1b1ce2) /* 271 */,
208 U64_C(0xcd45505e8353e80d) /* 272 */, U64_C(0x18d19a00d4db0717) /* 273 */,
209 U64_C(0x34a0cfeda5f38101) /* 274 */, U64_C(0x0be77e518887caf2) /* 275 */,
210 U64_C(0x1e341438b3c45136) /* 276 */, U64_C(0xe05797f49089ccf9) /* 277 */,
211 U64_C(0xffd23f9df2591d14) /* 278 */, U64_C(0x543dda228595c5cd) /* 279 */,
212 U64_C(0x661f81fd99052a33) /* 280 */, U64_C(0x8736e641db0f7b76) /* 281 */,
213 U64_C(0x15227725418e5307) /* 282 */, U64_C(0xe25f7f46162eb2fa) /* 283 */,
214 U64_C(0x48a8b2126c13d9fe) /* 284 */, U64_C(0xafdc541792e76eea) /* 285 */,
215 U64_C(0x03d912bfc6d1898f) /* 286 */, U64_C(0x31b1aafa1b83f51b) /* 287 */,
216 U64_C(0xf1ac2796e42ab7d9) /* 288 */, U64_C(0x40a3a7d7fcd2ebac) /* 289 */,
217 U64_C(0x1056136d0afbbcc5) /* 290 */, U64_C(0x7889e1dd9a6d0c85) /* 291 */,
218 U64_C(0xd33525782a7974aa) /* 292 */, U64_C(0xa7e25d09078ac09b) /* 293 */,
219 U64_C(0xbd4138b3eac6edd0) /* 294 */, U64_C(0x920abfbe71eb9e70) /* 295 */,
220 U64_C(0xa2a5d0f54fc2625c) /* 296 */, U64_C(0xc054e36b0b1290a3) /* 297 */,
221 U64_C(0xf6dd59ff62fe932b) /* 298 */, U64_C(0x3537354511a8ac7d) /* 299 */,
222 U64_C(0xca845e9172fadcd4) /* 300 */, U64_C(0x84f82b60329d20dc) /* 301 */,
223 U64_C(0x79c62ce1cd672f18) /* 302 */, U64_C(0x8b09a2add124642c) /* 303 */,
224 U64_C(0xd0c1e96a19d9e726) /* 304 */, U64_C(0x5a786a9b4ba9500c) /* 305 */,
225 U64_C(0x0e020336634c43f3) /* 306 */, U64_C(0xc17b474aeb66d822) /* 307 */,
226 U64_C(0x6a731ae3ec9baac2) /* 308 */, U64_C(0x8226667ae0840258) /* 309 */,
227 U64_C(0x67d4567691caeca5) /* 310 */, U64_C(0x1d94155c4875adb5) /* 311 */,
228 U64_C(0x6d00fd985b813fdf) /* 312 */, U64_C(0x51286efcb774cd06) /* 313 */,
229 U64_C(0x5e8834471fa744af) /* 314 */, U64_C(0xf72ca0aee761ae2e) /* 315 */,
230 U64_C(0xbe40e4cdaee8e09a) /* 316 */, U64_C(0xe9970bbb5118f665) /* 317 */,
231 U64_C(0x726e4beb33df1964) /* 318 */, U64_C(0x703b000729199762) /* 319 */,
232 U64_C(0x4631d816f5ef30a7) /* 320 */, U64_C(0xb880b5b51504a6be) /* 321 */,
233 U64_C(0x641793c37ed84b6c) /* 322 */, U64_C(0x7b21ed77f6e97d96) /* 323 */,
234 U64_C(0x776306312ef96b73) /* 324 */, U64_C(0xae528948e86ff3f4) /* 325 */,
235 U64_C(0x53dbd7f286a3f8f8) /* 326 */, U64_C(0x16cadce74cfc1063) /* 327 */,
236 U64_C(0x005c19bdfa52c6dd) /* 328 */, U64_C(0x68868f5d64d46ad3) /* 329 */,
237 U64_C(0x3a9d512ccf1e186a) /* 330 */, U64_C(0x367e62c2385660ae) /* 331 */,
238 U64_C(0xe359e7ea77dcb1d7) /* 332 */, U64_C(0x526c0773749abe6e) /* 333 */,
239 U64_C(0x735ae5f9d09f734b) /* 334 */, U64_C(0x493fc7cc8a558ba8) /* 335 */,
240 U64_C(0xb0b9c1533041ab45) /* 336 */, U64_C(0x321958ba470a59bd) /* 337 */,
241 U64_C(0x852db00b5f46c393) /* 338 */, U64_C(0x91209b2bd336b0e5) /* 339 */,
242 U64_C(0x6e604f7d659ef19f) /* 340 */, U64_C(0xb99a8ae2782ccb24) /* 341 */,
243 U64_C(0xccf52ab6c814c4c7) /* 342 */, U64_C(0x4727d9afbe11727b) /* 343 */,
244 U64_C(0x7e950d0c0121b34d) /* 344 */, U64_C(0x756f435670ad471f) /* 345 */,
245 U64_C(0xf5add442615a6849) /* 346 */, U64_C(0x4e87e09980b9957a) /* 347 */,
246 U64_C(0x2acfa1df50aee355) /* 348 */, U64_C(0xd898263afd2fd556) /* 349 */,
247 U64_C(0xc8f4924dd80c8fd6) /* 350 */, U64_C(0xcf99ca3d754a173a) /* 351 */,
248 U64_C(0xfe477bacaf91bf3c) /* 352 */, U64_C(0xed5371f6d690c12d) /* 353 */,
249 U64_C(0x831a5c285e687094) /* 354 */, U64_C(0xc5d3c90a3708a0a4) /* 355 */,
250 U64_C(0x0f7f903717d06580) /* 356 */, U64_C(0x19f9bb13b8fdf27f) /* 357 */,
251 U64_C(0xb1bd6f1b4d502843) /* 358 */, U64_C(0x1c761ba38fff4012) /* 359 */,
252 U64_C(0x0d1530c4e2e21f3b) /* 360 */, U64_C(0x8943ce69a7372c8a) /* 361 */,
253 U64_C(0xe5184e11feb5ce66) /* 362 */, U64_C(0x618bdb80bd736621) /* 363 */,
254 U64_C(0x7d29bad68b574d0b) /* 364 */, U64_C(0x81bb613e25e6fe5b) /* 365 */,
255 U64_C(0x071c9c10bc07913f) /* 366 */, U64_C(0xc7beeb7909ac2d97) /* 367 */,
256 U64_C(0xc3e58d353bc5d757) /* 368 */, U64_C(0xeb017892f38f61e8) /* 369 */,
257 U64_C(0xd4effb9c9b1cc21a) /* 370 */, U64_C(0x99727d26f494f7ab) /* 371 */,
258 U64_C(0xa3e063a2956b3e03) /* 372 */, U64_C(0x9d4a8b9a4aa09c30) /* 373 */,
259 U64_C(0x3f6ab7d500090fb4) /* 374 */, U64_C(0x9cc0f2a057268ac0) /* 375 */,
260 U64_C(0x3dee9d2dedbf42d1) /* 376 */, U64_C(0x330f49c87960a972) /* 377 */,
261 U64_C(0xc6b2720287421b41) /* 378 */, U64_C(0x0ac59ec07c00369c) /* 379 */,
262 U64_C(0xef4eac49cb353425) /* 380 */, U64_C(0xf450244eef0129d8) /* 381 */,
263 U64_C(0x8acc46e5caf4deb6) /* 382 */, U64_C(0x2ffeab63989263f7) /* 383 */,
264 U64_C(0x8f7cb9fe5d7a4578) /* 384 */, U64_C(0x5bd8f7644e634635) /* 385 */,
265 U64_C(0x427a7315bf2dc900) /* 386 */, U64_C(0x17d0c4aa2125261c) /* 387 */,
266 U64_C(0x3992486c93518e50) /* 388 */, U64_C(0xb4cbfee0a2d7d4c3) /* 389 */,
267 U64_C(0x7c75d6202c5ddd8d) /* 390 */, U64_C(0xdbc295d8e35b6c61) /* 391 */,
268 U64_C(0x60b369d302032b19) /* 392 */, U64_C(0xce42685fdce44132) /* 393 */,
269 U64_C(0x06f3ddb9ddf65610) /* 394 */, U64_C(0x8ea4d21db5e148f0) /* 395 */,
270 U64_C(0x20b0fce62fcd496f) /* 396 */, U64_C(0x2c1b912358b0ee31) /* 397 */,
271 U64_C(0xb28317b818f5a308) /* 398 */, U64_C(0xa89c1e189ca6d2cf) /* 399 */,
272 U64_C(0x0c6b18576aaadbc8) /* 400 */, U64_C(0xb65deaa91299fae3) /* 401 */,
273 U64_C(0xfb2b794b7f1027e7) /* 402 */, U64_C(0x04e4317f443b5beb) /* 403 */,
274 U64_C(0x4b852d325939d0a6) /* 404 */, U64_C(0xd5ae6beefb207ffc) /* 405 */,
275 U64_C(0x309682b281c7d374) /* 406 */, U64_C(0xbae309a194c3b475) /* 407 */,
276 U64_C(0x8cc3f97b13b49f05) /* 408 */, U64_C(0x98a9422ff8293967) /* 409 */,
277 U64_C(0x244b16b01076ff7c) /* 410 */, U64_C(0xf8bf571c663d67ee) /* 411 */,
278 U64_C(0x1f0d6758eee30da1) /* 412 */, U64_C(0xc9b611d97adeb9b7) /* 413 */,
279 U64_C(0xb7afd5887b6c57a2) /* 414 */, U64_C(0x6290ae846b984fe1) /* 415 */,
280 U64_C(0x94df4cdeacc1a5fd) /* 416 */, U64_C(0x058a5bd1c5483aff) /* 417 */,
281 U64_C(0x63166cc142ba3c37) /* 418 */, U64_C(0x8db8526eb2f76f40) /* 419 */,
282 U64_C(0xe10880036f0d6d4e) /* 420 */, U64_C(0x9e0523c9971d311d) /* 421 */,
283 U64_C(0x45ec2824cc7cd691) /* 422 */, U64_C(0x575b8359e62382c9) /* 423 */,
284 U64_C(0xfa9e400dc4889995) /* 424 */, U64_C(0xd1823ecb45721568) /* 425 */,
285 U64_C(0xdafd983b8206082f) /* 426 */, U64_C(0xaa7d29082386a8cb) /* 427 */,
286 U64_C(0x269fcd4403b87588) /* 428 */, U64_C(0x1b91f5f728bdd1e0) /* 429 */,
287 U64_C(0xe4669f39040201f6) /* 430 */, U64_C(0x7a1d7c218cf04ade) /* 431 */,
288 U64_C(0x65623c29d79ce5ce) /* 432 */, U64_C(0x2368449096c00bb1) /* 433 */,
289 U64_C(0xab9bf1879da503ba) /* 434 */, U64_C(0xbc23ecb1a458058e) /* 435 */,
290 U64_C(0x9a58df01bb401ecc) /* 436 */, U64_C(0xa070e868a85f143d) /* 437 */,
291 U64_C(0x4ff188307df2239e) /* 438 */, U64_C(0x14d565b41a641183) /* 439 */,
292 U64_C(0xee13337452701602) /* 440 */, U64_C(0x950e3dcf3f285e09) /* 441 */,
293 U64_C(0x59930254b9c80953) /* 442 */, U64_C(0x3bf299408930da6d) /* 443 */,
294 U64_C(0xa955943f53691387) /* 444 */, U64_C(0xa15edecaa9cb8784) /* 445 */,
295 U64_C(0x29142127352be9a0) /* 446 */, U64_C(0x76f0371fff4e7afb) /* 447 */,
296 U64_C(0x0239f450274f2228) /* 448 */, U64_C(0xbb073af01d5e868b) /* 449 */,
297 U64_C(0xbfc80571c10e96c1) /* 450 */, U64_C(0xd267088568222e23) /* 451 */,
298 U64_C(0x9671a3d48e80b5b0) /* 452 */, U64_C(0x55b5d38ae193bb81) /* 453 */,
299 U64_C(0x693ae2d0a18b04b8) /* 454 */, U64_C(0x5c48b4ecadd5335f) /* 455 */,
300 U64_C(0xfd743b194916a1ca) /* 456 */, U64_C(0x2577018134be98c4) /* 457 */,
301 U64_C(0xe77987e83c54a4ad) /* 458 */, U64_C(0x28e11014da33e1b9) /* 459 */,
302 U64_C(0x270cc59e226aa213) /* 460 */, U64_C(0x71495f756d1a5f60) /* 461 */,
303 U64_C(0x9be853fb60afef77) /* 462 */, U64_C(0xadc786a7f7443dbf) /* 463 */,
304 U64_C(0x0904456173b29a82) /* 464 */, U64_C(0x58bc7a66c232bd5e) /* 465 */,
305 U64_C(0xf306558c673ac8b2) /* 466 */, U64_C(0x41f639c6b6c9772a) /* 467 */,
306 U64_C(0x216defe99fda35da) /* 468 */, U64_C(0x11640cc71c7be615) /* 469 */,
307 U64_C(0x93c43694565c5527) /* 470 */, U64_C(0xea038e6246777839) /* 471 */,
308 U64_C(0xf9abf3ce5a3e2469) /* 472 */, U64_C(0x741e768d0fd312d2) /* 473 */,
309 U64_C(0x0144b883ced652c6) /* 474 */, U64_C(0xc20b5a5ba33f8552) /* 475 */,
310 U64_C(0x1ae69633c3435a9d) /* 476 */, U64_C(0x97a28ca4088cfdec) /* 477 */,
311 U64_C(0x8824a43c1e96f420) /* 478 */, U64_C(0x37612fa66eeea746) /* 479 */,
312 U64_C(0x6b4cb165f9cf0e5a) /* 480 */, U64_C(0x43aa1c06a0abfb4a) /* 481 */,
313 U64_C(0x7f4dc26ff162796b) /* 482 */, U64_C(0x6cbacc8e54ed9b0f) /* 483 */,
314 U64_C(0xa6b7ffefd2bb253e) /* 484 */, U64_C(0x2e25bc95b0a29d4f) /* 485 */,
315 U64_C(0x86d6a58bdef1388c) /* 486 */, U64_C(0xded74ac576b6f054) /* 487 */,
316 U64_C(0x8030bdbc2b45805d) /* 488 */, U64_C(0x3c81af70e94d9289) /* 489 */,
317 U64_C(0x3eff6dda9e3100db) /* 490 */, U64_C(0xb38dc39fdfcc8847) /* 491 */,
318 U64_C(0x123885528d17b87e) /* 492 */, U64_C(0xf2da0ed240b1b642) /* 493 */,
319 U64_C(0x44cefadcd54bf9a9) /* 494 */, U64_C(0x1312200e433c7ee6) /* 495 */,
320 U64_C(0x9ffcc84f3a78c748) /* 496 */, U64_C(0xf0cd1f72248576bb) /* 497 */,
321 U64_C(0xec6974053638cfe4) /* 498 */, U64_C(0x2ba7b67c0cec4e4c) /* 499 */,
322 U64_C(0xac2f4df3e5ce32ed) /* 500 */, U64_C(0xcb33d14326ea4c11) /* 501 */,
323 U64_C(0xa4e9044cc77e58bc) /* 502 */, U64_C(0x5f513293d934fcef) /* 503 */,
324 U64_C(0x5dc9645506e55444) /* 504 */, U64_C(0x50de418f317de40a) /* 505 */,
325 U64_C(0x388cb31a69dde259) /* 506 */, U64_C(0x2db4a83455820a86) /* 507 */,
326 U64_C(0x9010a91e84711ae9) /* 508 */, U64_C(0x4df7f0b7b1498371) /* 509 */,
327 U64_C(0xd62a2eabc0977179) /* 510 */, U64_C(0x22fac097aa8d5c0e) /* 511 */
328};
329static u64 sbox3[256] = {
330 U64_C(0xf49fcc2ff1daf39b) /* 512 */, U64_C(0x487fd5c66ff29281) /* 513 */,
331 U64_C(0xe8a30667fcdca83f) /* 514 */, U64_C(0x2c9b4be3d2fcce63) /* 515 */,
332 U64_C(0xda3ff74b93fbbbc2) /* 516 */, U64_C(0x2fa165d2fe70ba66) /* 517 */,
333 U64_C(0xa103e279970e93d4) /* 518 */, U64_C(0xbecdec77b0e45e71) /* 519 */,
334 U64_C(0xcfb41e723985e497) /* 520 */, U64_C(0xb70aaa025ef75017) /* 521 */,
335 U64_C(0xd42309f03840b8e0) /* 522 */, U64_C(0x8efc1ad035898579) /* 523 */,
336 U64_C(0x96c6920be2b2abc5) /* 524 */, U64_C(0x66af4163375a9172) /* 525 */,
337 U64_C(0x2174abdcca7127fb) /* 526 */, U64_C(0xb33ccea64a72ff41) /* 527 */,
338 U64_C(0xf04a4933083066a5) /* 528 */, U64_C(0x8d970acdd7289af5) /* 529 */,
339 U64_C(0x8f96e8e031c8c25e) /* 530 */, U64_C(0xf3fec02276875d47) /* 531 */,
340 U64_C(0xec7bf310056190dd) /* 532 */, U64_C(0xf5adb0aebb0f1491) /* 533 */,
341 U64_C(0x9b50f8850fd58892) /* 534 */, U64_C(0x4975488358b74de8) /* 535 */,
342 U64_C(0xa3354ff691531c61) /* 536 */, U64_C(0x0702bbe481d2c6ee) /* 537 */,
343 U64_C(0x89fb24057deded98) /* 538 */, U64_C(0xac3075138596e902) /* 539 */,
344 U64_C(0x1d2d3580172772ed) /* 540 */, U64_C(0xeb738fc28e6bc30d) /* 541 */,
345 U64_C(0x5854ef8f63044326) /* 542 */, U64_C(0x9e5c52325add3bbe) /* 543 */,
346 U64_C(0x90aa53cf325c4623) /* 544 */, U64_C(0xc1d24d51349dd067) /* 545 */,
347 U64_C(0x2051cfeea69ea624) /* 546 */, U64_C(0x13220f0a862e7e4f) /* 547 */,
348 U64_C(0xce39399404e04864) /* 548 */, U64_C(0xd9c42ca47086fcb7) /* 549 */,
349 U64_C(0x685ad2238a03e7cc) /* 550 */, U64_C(0x066484b2ab2ff1db) /* 551 */,
350 U64_C(0xfe9d5d70efbf79ec) /* 552 */, U64_C(0x5b13b9dd9c481854) /* 553 */,
351 U64_C(0x15f0d475ed1509ad) /* 554 */, U64_C(0x0bebcd060ec79851) /* 555 */,
352 U64_C(0xd58c6791183ab7f8) /* 556 */, U64_C(0xd1187c5052f3eee4) /* 557 */,
353 U64_C(0xc95d1192e54e82ff) /* 558 */, U64_C(0x86eea14cb9ac6ca2) /* 559 */,
354 U64_C(0x3485beb153677d5d) /* 560 */, U64_C(0xdd191d781f8c492a) /* 561 */,
355 U64_C(0xf60866baa784ebf9) /* 562 */, U64_C(0x518f643ba2d08c74) /* 563 */,
356 U64_C(0x8852e956e1087c22) /* 564 */, U64_C(0xa768cb8dc410ae8d) /* 565 */,
357 U64_C(0x38047726bfec8e1a) /* 566 */, U64_C(0xa67738b4cd3b45aa) /* 567 */,
358 U64_C(0xad16691cec0dde19) /* 568 */, U64_C(0xc6d4319380462e07) /* 569 */,
359 U64_C(0xc5a5876d0ba61938) /* 570 */, U64_C(0x16b9fa1fa58fd840) /* 571 */,
360 U64_C(0x188ab1173ca74f18) /* 572 */, U64_C(0xabda2f98c99c021f) /* 573 */,
361 U64_C(0x3e0580ab134ae816) /* 574 */, U64_C(0x5f3b05b773645abb) /* 575 */,
362 U64_C(0x2501a2be5575f2f6) /* 576 */, U64_C(0x1b2f74004e7e8ba9) /* 577 */,
363 U64_C(0x1cd7580371e8d953) /* 578 */, U64_C(0x7f6ed89562764e30) /* 579 */,
364 U64_C(0xb15926ff596f003d) /* 580 */, U64_C(0x9f65293da8c5d6b9) /* 581 */,
365 U64_C(0x6ecef04dd690f84c) /* 582 */, U64_C(0x4782275fff33af88) /* 583 */,
366 U64_C(0xe41433083f820801) /* 584 */, U64_C(0xfd0dfe409a1af9b5) /* 585 */,
367 U64_C(0x4325a3342cdb396b) /* 586 */, U64_C(0x8ae77e62b301b252) /* 587 */,
368 U64_C(0xc36f9e9f6655615a) /* 588 */, U64_C(0x85455a2d92d32c09) /* 589 */,
369 U64_C(0xf2c7dea949477485) /* 590 */, U64_C(0x63cfb4c133a39eba) /* 591 */,
370 U64_C(0x83b040cc6ebc5462) /* 592 */, U64_C(0x3b9454c8fdb326b0) /* 593 */,
371 U64_C(0x56f56a9e87ffd78c) /* 594 */, U64_C(0x2dc2940d99f42bc6) /* 595 */,
372 U64_C(0x98f7df096b096e2d) /* 596 */, U64_C(0x19a6e01e3ad852bf) /* 597 */,
373 U64_C(0x42a99ccbdbd4b40b) /* 598 */, U64_C(0xa59998af45e9c559) /* 599 */,
374 U64_C(0x366295e807d93186) /* 600 */, U64_C(0x6b48181bfaa1f773) /* 601 */,
375 U64_C(0x1fec57e2157a0a1d) /* 602 */, U64_C(0x4667446af6201ad5) /* 603 */,
376 U64_C(0xe615ebcacfb0f075) /* 604 */, U64_C(0xb8f31f4f68290778) /* 605 */,
377 U64_C(0x22713ed6ce22d11e) /* 606 */, U64_C(0x3057c1a72ec3c93b) /* 607 */,
378 U64_C(0xcb46acc37c3f1f2f) /* 608 */, U64_C(0xdbb893fd02aaf50e) /* 609 */,
379 U64_C(0x331fd92e600b9fcf) /* 610 */, U64_C(0xa498f96148ea3ad6) /* 611 */,
380 U64_C(0xa8d8426e8b6a83ea) /* 612 */, U64_C(0xa089b274b7735cdc) /* 613 */,
381 U64_C(0x87f6b3731e524a11) /* 614 */, U64_C(0x118808e5cbc96749) /* 615 */,
382 U64_C(0x9906e4c7b19bd394) /* 616 */, U64_C(0xafed7f7e9b24a20c) /* 617 */,
383 U64_C(0x6509eadeeb3644a7) /* 618 */, U64_C(0x6c1ef1d3e8ef0ede) /* 619 */,
384 U64_C(0xb9c97d43e9798fb4) /* 620 */, U64_C(0xa2f2d784740c28a3) /* 621 */,
385 U64_C(0x7b8496476197566f) /* 622 */, U64_C(0x7a5be3e6b65f069d) /* 623 */,
386 U64_C(0xf96330ed78be6f10) /* 624 */, U64_C(0xeee60de77a076a15) /* 625 */,
387 U64_C(0x2b4bee4aa08b9bd0) /* 626 */, U64_C(0x6a56a63ec7b8894e) /* 627 */,
388 U64_C(0x02121359ba34fef4) /* 628 */, U64_C(0x4cbf99f8283703fc) /* 629 */,
389 U64_C(0x398071350caf30c8) /* 630 */, U64_C(0xd0a77a89f017687a) /* 631 */,
390 U64_C(0xf1c1a9eb9e423569) /* 632 */, U64_C(0x8c7976282dee8199) /* 633 */,
391 U64_C(0x5d1737a5dd1f7abd) /* 634 */, U64_C(0x4f53433c09a9fa80) /* 635 */,
392 U64_C(0xfa8b0c53df7ca1d9) /* 636 */, U64_C(0x3fd9dcbc886ccb77) /* 637 */,
393 U64_C(0xc040917ca91b4720) /* 638 */, U64_C(0x7dd00142f9d1dcdf) /* 639 */,
394 U64_C(0x8476fc1d4f387b58) /* 640 */, U64_C(0x23f8e7c5f3316503) /* 641 */,
395 U64_C(0x032a2244e7e37339) /* 642 */, U64_C(0x5c87a5d750f5a74b) /* 643 */,
396 U64_C(0x082b4cc43698992e) /* 644 */, U64_C(0xdf917becb858f63c) /* 645 */,
397 U64_C(0x3270b8fc5bf86dda) /* 646 */, U64_C(0x10ae72bb29b5dd76) /* 647 */,
398 U64_C(0x576ac94e7700362b) /* 648 */, U64_C(0x1ad112dac61efb8f) /* 649 */,
399 U64_C(0x691bc30ec5faa427) /* 650 */, U64_C(0xff246311cc327143) /* 651 */,
400 U64_C(0x3142368e30e53206) /* 652 */, U64_C(0x71380e31e02ca396) /* 653 */,
401 U64_C(0x958d5c960aad76f1) /* 654 */, U64_C(0xf8d6f430c16da536) /* 655 */,
402 U64_C(0xc8ffd13f1be7e1d2) /* 656 */, U64_C(0x7578ae66004ddbe1) /* 657 */,
403 U64_C(0x05833f01067be646) /* 658 */, U64_C(0xbb34b5ad3bfe586d) /* 659 */,
404 U64_C(0x095f34c9a12b97f0) /* 660 */, U64_C(0x247ab64525d60ca8) /* 661 */,
405 U64_C(0xdcdbc6f3017477d1) /* 662 */, U64_C(0x4a2e14d4decad24d) /* 663 */,
406 U64_C(0xbdb5e6d9be0a1eeb) /* 664 */, U64_C(0x2a7e70f7794301ab) /* 665 */,
407 U64_C(0xdef42d8a270540fd) /* 666 */, U64_C(0x01078ec0a34c22c1) /* 667 */,
408 U64_C(0xe5de511af4c16387) /* 668 */, U64_C(0x7ebb3a52bd9a330a) /* 669 */,
409 U64_C(0x77697857aa7d6435) /* 670 */, U64_C(0x004e831603ae4c32) /* 671 */,
410 U64_C(0xe7a21020ad78e312) /* 672 */, U64_C(0x9d41a70c6ab420f2) /* 673 */,
411 U64_C(0x28e06c18ea1141e6) /* 674 */, U64_C(0xd2b28cbd984f6b28) /* 675 */,
412 U64_C(0x26b75f6c446e9d83) /* 676 */, U64_C(0xba47568c4d418d7f) /* 677 */,
413 U64_C(0xd80badbfe6183d8e) /* 678 */, U64_C(0x0e206d7f5f166044) /* 679 */,
414 U64_C(0xe258a43911cbca3e) /* 680 */, U64_C(0x723a1746b21dc0bc) /* 681 */,
415 U64_C(0xc7caa854f5d7cdd3) /* 682 */, U64_C(0x7cac32883d261d9c) /* 683 */,
416 U64_C(0x7690c26423ba942c) /* 684 */, U64_C(0x17e55524478042b8) /* 685 */,
417 U64_C(0xe0be477656a2389f) /* 686 */, U64_C(0x4d289b5e67ab2da0) /* 687 */,
418 U64_C(0x44862b9c8fbbfd31) /* 688 */, U64_C(0xb47cc8049d141365) /* 689 */,
419 U64_C(0x822c1b362b91c793) /* 690 */, U64_C(0x4eb14655fb13dfd8) /* 691 */,
420 U64_C(0x1ecbba0714e2a97b) /* 692 */, U64_C(0x6143459d5cde5f14) /* 693 */,
421 U64_C(0x53a8fbf1d5f0ac89) /* 694 */, U64_C(0x97ea04d81c5e5b00) /* 695 */,
422 U64_C(0x622181a8d4fdb3f3) /* 696 */, U64_C(0xe9bcd341572a1208) /* 697 */,
423 U64_C(0x1411258643cce58a) /* 698 */, U64_C(0x9144c5fea4c6e0a4) /* 699 */,
424 U64_C(0x0d33d06565cf620f) /* 700 */, U64_C(0x54a48d489f219ca1) /* 701 */,
425 U64_C(0xc43e5eac6d63c821) /* 702 */, U64_C(0xa9728b3a72770daf) /* 703 */,
426 U64_C(0xd7934e7b20df87ef) /* 704 */, U64_C(0xe35503b61a3e86e5) /* 705 */,
427 U64_C(0xcae321fbc819d504) /* 706 */, U64_C(0x129a50b3ac60bfa6) /* 707 */,
428 U64_C(0xcd5e68ea7e9fb6c3) /* 708 */, U64_C(0xb01c90199483b1c7) /* 709 */,
429 U64_C(0x3de93cd5c295376c) /* 710 */, U64_C(0xaed52edf2ab9ad13) /* 711 */,
430 U64_C(0x2e60f512c0a07884) /* 712 */, U64_C(0xbc3d86a3e36210c9) /* 713 */,
431 U64_C(0x35269d9b163951ce) /* 714 */, U64_C(0x0c7d6e2ad0cdb5fa) /* 715 */,
432 U64_C(0x59e86297d87f5733) /* 716 */, U64_C(0x298ef221898db0e7) /* 717 */,
433 U64_C(0x55000029d1a5aa7e) /* 718 */, U64_C(0x8bc08ae1b5061b45) /* 719 */,
434 U64_C(0xc2c31c2b6c92703a) /* 720 */, U64_C(0x94cc596baf25ef42) /* 721 */,
435 U64_C(0x0a1d73db22540456) /* 722 */, U64_C(0x04b6a0f9d9c4179a) /* 723 */,
436 U64_C(0xeffdafa2ae3d3c60) /* 724 */, U64_C(0xf7c8075bb49496c4) /* 725 */,
437 U64_C(0x9cc5c7141d1cd4e3) /* 726 */, U64_C(0x78bd1638218e5534) /* 727 */,
438 U64_C(0xb2f11568f850246a) /* 728 */, U64_C(0xedfabcfa9502bc29) /* 729 */,
439 U64_C(0x796ce5f2da23051b) /* 730 */, U64_C(0xaae128b0dc93537c) /* 731 */,
440 U64_C(0x3a493da0ee4b29ae) /* 732 */, U64_C(0xb5df6b2c416895d7) /* 733 */,
441 U64_C(0xfcabbd25122d7f37) /* 734 */, U64_C(0x70810b58105dc4b1) /* 735 */,
442 U64_C(0xe10fdd37f7882a90) /* 736 */, U64_C(0x524dcab5518a3f5c) /* 737 */,
443 U64_C(0x3c9e85878451255b) /* 738 */, U64_C(0x4029828119bd34e2) /* 739 */,
444 U64_C(0x74a05b6f5d3ceccb) /* 740 */, U64_C(0xb610021542e13eca) /* 741 */,
445 U64_C(0x0ff979d12f59e2ac) /* 742 */, U64_C(0x6037da27e4f9cc50) /* 743 */,
446 U64_C(0x5e92975a0df1847d) /* 744 */, U64_C(0xd66de190d3e623fe) /* 745 */,
447 U64_C(0x5032d6b87b568048) /* 746 */, U64_C(0x9a36b7ce8235216e) /* 747 */,
448 U64_C(0x80272a7a24f64b4a) /* 748 */, U64_C(0x93efed8b8c6916f7) /* 749 */,
449 U64_C(0x37ddbff44cce1555) /* 750 */, U64_C(0x4b95db5d4b99bd25) /* 751 */,
450 U64_C(0x92d3fda169812fc0) /* 752 */, U64_C(0xfb1a4a9a90660bb6) /* 753 */,
451 U64_C(0x730c196946a4b9b2) /* 754 */, U64_C(0x81e289aa7f49da68) /* 755 */,
452 U64_C(0x64669a0f83b1a05f) /* 756 */, U64_C(0x27b3ff7d9644f48b) /* 757 */,
453 U64_C(0xcc6b615c8db675b3) /* 758 */, U64_C(0x674f20b9bcebbe95) /* 759 */,
454 U64_C(0x6f31238275655982) /* 760 */, U64_C(0x5ae488713e45cf05) /* 761 */,
455 U64_C(0xbf619f9954c21157) /* 762 */, U64_C(0xeabac46040a8eae9) /* 763 */,
456 U64_C(0x454c6fe9f2c0c1cd) /* 764 */, U64_C(0x419cf6496412691c) /* 765 */,
457 U64_C(0xd3dc3bef265b0f70) /* 766 */, U64_C(0x6d0e60f5c3578a9e) /* 767 */
458};
459static u64 sbox4[256] = {
460 U64_C(0x5b0e608526323c55) /* 768 */, U64_C(0x1a46c1a9fa1b59f5) /* 769 */,
461 U64_C(0xa9e245a17c4c8ffa) /* 770 */, U64_C(0x65ca5159db2955d7) /* 771 */,
462 U64_C(0x05db0a76ce35afc2) /* 772 */, U64_C(0x81eac77ea9113d45) /* 773 */,
463 U64_C(0x528ef88ab6ac0a0d) /* 774 */, U64_C(0xa09ea253597be3ff) /* 775 */,
464 U64_C(0x430ddfb3ac48cd56) /* 776 */, U64_C(0xc4b3a67af45ce46f) /* 777 */,
465 U64_C(0x4ececfd8fbe2d05e) /* 778 */, U64_C(0x3ef56f10b39935f0) /* 779 */,
466 U64_C(0x0b22d6829cd619c6) /* 780 */, U64_C(0x17fd460a74df2069) /* 781 */,
467 U64_C(0x6cf8cc8e8510ed40) /* 782 */, U64_C(0xd6c824bf3a6ecaa7) /* 783 */,
468 U64_C(0x61243d581a817049) /* 784 */, U64_C(0x048bacb6bbc163a2) /* 785 */,
469 U64_C(0xd9a38ac27d44cc32) /* 786 */, U64_C(0x7fddff5baaf410ab) /* 787 */,
470 U64_C(0xad6d495aa804824b) /* 788 */, U64_C(0xe1a6a74f2d8c9f94) /* 789 */,
471 U64_C(0xd4f7851235dee8e3) /* 790 */, U64_C(0xfd4b7f886540d893) /* 791 */,
472 U64_C(0x247c20042aa4bfda) /* 792 */, U64_C(0x096ea1c517d1327c) /* 793 */,
473 U64_C(0xd56966b4361a6685) /* 794 */, U64_C(0x277da5c31221057d) /* 795 */,
474 U64_C(0x94d59893a43acff7) /* 796 */, U64_C(0x64f0c51ccdc02281) /* 797 */,
475 U64_C(0x3d33bcc4ff6189db) /* 798 */, U64_C(0xe005cb184ce66af1) /* 799 */,
476 U64_C(0xff5ccd1d1db99bea) /* 800 */, U64_C(0xb0b854a7fe42980f) /* 801 */,
477 U64_C(0x7bd46a6a718d4b9f) /* 802 */, U64_C(0xd10fa8cc22a5fd8c) /* 803 */,
478 U64_C(0xd31484952be4bd31) /* 804 */, U64_C(0xc7fa975fcb243847) /* 805 */,
479 U64_C(0x4886ed1e5846c407) /* 806 */, U64_C(0x28cddb791eb70b04) /* 807 */,
480 U64_C(0xc2b00be2f573417f) /* 808 */, U64_C(0x5c9590452180f877) /* 809 */,
481 U64_C(0x7a6bddfff370eb00) /* 810 */, U64_C(0xce509e38d6d9d6a4) /* 811 */,
482 U64_C(0xebeb0f00647fa702) /* 812 */, U64_C(0x1dcc06cf76606f06) /* 813 */,
483 U64_C(0xe4d9f28ba286ff0a) /* 814 */, U64_C(0xd85a305dc918c262) /* 815 */,
484 U64_C(0x475b1d8732225f54) /* 816 */, U64_C(0x2d4fb51668ccb5fe) /* 817 */,
485 U64_C(0xa679b9d9d72bba20) /* 818 */, U64_C(0x53841c0d912d43a5) /* 819 */,
486 U64_C(0x3b7eaa48bf12a4e8) /* 820 */, U64_C(0x781e0e47f22f1ddf) /* 821 */,
487 U64_C(0xeff20ce60ab50973) /* 822 */, U64_C(0x20d261d19dffb742) /* 823 */,
488 U64_C(0x16a12b03062a2e39) /* 824 */, U64_C(0x1960eb2239650495) /* 825 */,
489 U64_C(0x251c16fed50eb8b8) /* 826 */, U64_C(0x9ac0c330f826016e) /* 827 */,
490 U64_C(0xed152665953e7671) /* 828 */, U64_C(0x02d63194a6369570) /* 829 */,
491 U64_C(0x5074f08394b1c987) /* 830 */, U64_C(0x70ba598c90b25ce1) /* 831 */,
492 U64_C(0x794a15810b9742f6) /* 832 */, U64_C(0x0d5925e9fcaf8c6c) /* 833 */,
493 U64_C(0x3067716cd868744e) /* 834 */, U64_C(0x910ab077e8d7731b) /* 835 */,
494 U64_C(0x6a61bbdb5ac42f61) /* 836 */, U64_C(0x93513efbf0851567) /* 837 */,
495 U64_C(0xf494724b9e83e9d5) /* 838 */, U64_C(0xe887e1985c09648d) /* 839 */,
496 U64_C(0x34b1d3c675370cfd) /* 840 */, U64_C(0xdc35e433bc0d255d) /* 841 */,
497 U64_C(0xd0aab84234131be0) /* 842 */, U64_C(0x08042a50b48b7eaf) /* 843 */,
498 U64_C(0x9997c4ee44a3ab35) /* 844 */, U64_C(0x829a7b49201799d0) /* 845 */,
499 U64_C(0x263b8307b7c54441) /* 846 */, U64_C(0x752f95f4fd6a6ca6) /* 847 */,
500 U64_C(0x927217402c08c6e5) /* 848 */, U64_C(0x2a8ab754a795d9ee) /* 849 */,
501 U64_C(0xa442f7552f72943d) /* 850 */, U64_C(0x2c31334e19781208) /* 851 */,
502 U64_C(0x4fa98d7ceaee6291) /* 852 */, U64_C(0x55c3862f665db309) /* 853 */,
503 U64_C(0xbd0610175d53b1f3) /* 854 */, U64_C(0x46fe6cb840413f27) /* 855 */,
504 U64_C(0x3fe03792df0cfa59) /* 856 */, U64_C(0xcfe700372eb85e8f) /* 857 */,
505 U64_C(0xa7be29e7adbce118) /* 858 */, U64_C(0xe544ee5cde8431dd) /* 859 */,
506 U64_C(0x8a781b1b41f1873e) /* 860 */, U64_C(0xa5c94c78a0d2f0e7) /* 861 */,
507 U64_C(0x39412e2877b60728) /* 862 */, U64_C(0xa1265ef3afc9a62c) /* 863 */,
508 U64_C(0xbcc2770c6a2506c5) /* 864 */, U64_C(0x3ab66dd5dce1ce12) /* 865 */,
509 U64_C(0xe65499d04a675b37) /* 866 */, U64_C(0x7d8f523481bfd216) /* 867 */,
510 U64_C(0x0f6f64fcec15f389) /* 868 */, U64_C(0x74efbe618b5b13c8) /* 869 */,
511 U64_C(0xacdc82b714273e1d) /* 870 */, U64_C(0xdd40bfe003199d17) /* 871 */,
512 U64_C(0x37e99257e7e061f8) /* 872 */, U64_C(0xfa52626904775aaa) /* 873 */,
513 U64_C(0x8bbbf63a463d56f9) /* 874 */, U64_C(0xf0013f1543a26e64) /* 875 */,
514 U64_C(0xa8307e9f879ec898) /* 876 */, U64_C(0xcc4c27a4150177cc) /* 877 */,
515 U64_C(0x1b432f2cca1d3348) /* 878 */, U64_C(0xde1d1f8f9f6fa013) /* 879 */,
516 U64_C(0x606602a047a7ddd6) /* 880 */, U64_C(0xd237ab64cc1cb2c7) /* 881 */,
517 U64_C(0x9b938e7225fcd1d3) /* 882 */, U64_C(0xec4e03708e0ff476) /* 883 */,
518 U64_C(0xfeb2fbda3d03c12d) /* 884 */, U64_C(0xae0bced2ee43889a) /* 885 */,
519 U64_C(0x22cb8923ebfb4f43) /* 886 */, U64_C(0x69360d013cf7396d) /* 887 */,
520 U64_C(0x855e3602d2d4e022) /* 888 */, U64_C(0x073805bad01f784c) /* 889 */,
521 U64_C(0x33e17a133852f546) /* 890 */, U64_C(0xdf4874058ac7b638) /* 891 */,
522 U64_C(0xba92b29c678aa14a) /* 892 */, U64_C(0x0ce89fc76cfaadcd) /* 893 */,
523 U64_C(0x5f9d4e0908339e34) /* 894 */, U64_C(0xf1afe9291f5923b9) /* 895 */,
524 U64_C(0x6e3480f60f4a265f) /* 896 */, U64_C(0xeebf3a2ab29b841c) /* 897 */,
525 U64_C(0xe21938a88f91b4ad) /* 898 */, U64_C(0x57dfeff845c6d3c3) /* 899 */,
526 U64_C(0x2f006b0bf62caaf2) /* 900 */, U64_C(0x62f479ef6f75ee78) /* 901 */,
527 U64_C(0x11a55ad41c8916a9) /* 902 */, U64_C(0xf229d29084fed453) /* 903 */,
528 U64_C(0x42f1c27b16b000e6) /* 904 */, U64_C(0x2b1f76749823c074) /* 905 */,
529 U64_C(0x4b76eca3c2745360) /* 906 */, U64_C(0x8c98f463b91691bd) /* 907 */,
530 U64_C(0x14bcc93cf1ade66a) /* 908 */, U64_C(0x8885213e6d458397) /* 909 */,
531 U64_C(0x8e177df0274d4711) /* 910 */, U64_C(0xb49b73b5503f2951) /* 911 */,
532 U64_C(0x10168168c3f96b6b) /* 912 */, U64_C(0x0e3d963b63cab0ae) /* 913 */,
533 U64_C(0x8dfc4b5655a1db14) /* 914 */, U64_C(0xf789f1356e14de5c) /* 915 */,
534 U64_C(0x683e68af4e51dac1) /* 916 */, U64_C(0xc9a84f9d8d4b0fd9) /* 917 */,
535 U64_C(0x3691e03f52a0f9d1) /* 918 */, U64_C(0x5ed86e46e1878e80) /* 919 */,
536 U64_C(0x3c711a0e99d07150) /* 920 */, U64_C(0x5a0865b20c4e9310) /* 921 */,
537 U64_C(0x56fbfc1fe4f0682e) /* 922 */, U64_C(0xea8d5de3105edf9b) /* 923 */,
538 U64_C(0x71abfdb12379187a) /* 924 */, U64_C(0x2eb99de1bee77b9c) /* 925 */,
539 U64_C(0x21ecc0ea33cf4523) /* 926 */, U64_C(0x59a4d7521805c7a1) /* 927 */,
540 U64_C(0x3896f5eb56ae7c72) /* 928 */, U64_C(0xaa638f3db18f75dc) /* 929 */,
541 U64_C(0x9f39358dabe9808e) /* 930 */, U64_C(0xb7defa91c00b72ac) /* 931 */,
542 U64_C(0x6b5541fd62492d92) /* 932 */, U64_C(0x6dc6dee8f92e4d5b) /* 933 */,
543 U64_C(0x353f57abc4beea7e) /* 934 */, U64_C(0x735769d6da5690ce) /* 935 */,
544 U64_C(0x0a234aa642391484) /* 936 */, U64_C(0xf6f9508028f80d9d) /* 937 */,
545 U64_C(0xb8e319a27ab3f215) /* 938 */, U64_C(0x31ad9c1151341a4d) /* 939 */,
546 U64_C(0x773c22a57bef5805) /* 940 */, U64_C(0x45c7561a07968633) /* 941 */,
547 U64_C(0xf913da9e249dbe36) /* 942 */, U64_C(0xda652d9b78a64c68) /* 943 */,
548 U64_C(0x4c27a97f3bc334ef) /* 944 */, U64_C(0x76621220e66b17f4) /* 945 */,
549 U64_C(0x967743899acd7d0b) /* 946 */, U64_C(0xf3ee5bcae0ed6782) /* 947 */,
550 U64_C(0x409f753600c879fc) /* 948 */, U64_C(0x06d09a39b5926db6) /* 949 */,
551 U64_C(0x6f83aeb0317ac588) /* 950 */, U64_C(0x01e6ca4a86381f21) /* 951 */,
552 U64_C(0x66ff3462d19f3025) /* 952 */, U64_C(0x72207c24ddfd3bfb) /* 953 */,
553 U64_C(0x4af6b6d3e2ece2eb) /* 954 */, U64_C(0x9c994dbec7ea08de) /* 955 */,
554 U64_C(0x49ace597b09a8bc4) /* 956 */, U64_C(0xb38c4766cf0797ba) /* 957 */,
555 U64_C(0x131b9373c57c2a75) /* 958 */, U64_C(0xb1822cce61931e58) /* 959 */,
556 U64_C(0x9d7555b909ba1c0c) /* 960 */, U64_C(0x127fafdd937d11d2) /* 961 */,
557 U64_C(0x29da3badc66d92e4) /* 962 */, U64_C(0xa2c1d57154c2ecbc) /* 963 */,
558 U64_C(0x58c5134d82f6fe24) /* 964 */, U64_C(0x1c3ae3515b62274f) /* 965 */,
559 U64_C(0xe907c82e01cb8126) /* 966 */, U64_C(0xf8ed091913e37fcb) /* 967 */,
560 U64_C(0x3249d8f9c80046c9) /* 968 */, U64_C(0x80cf9bede388fb63) /* 969 */,
561 U64_C(0x1881539a116cf19e) /* 970 */, U64_C(0x5103f3f76bd52457) /* 971 */,
562 U64_C(0x15b7e6f5ae47f7a8) /* 972 */, U64_C(0xdbd7c6ded47e9ccf) /* 973 */,
563 U64_C(0x44e55c410228bb1a) /* 974 */, U64_C(0xb647d4255edb4e99) /* 975 */,
564 U64_C(0x5d11882bb8aafc30) /* 976 */, U64_C(0xf5098bbb29d3212a) /* 977 */,
565 U64_C(0x8fb5ea14e90296b3) /* 978 */, U64_C(0x677b942157dd025a) /* 979 */,
566 U64_C(0xfb58e7c0a390acb5) /* 980 */, U64_C(0x89d3674c83bd4a01) /* 981 */,
567 U64_C(0x9e2da4df4bf3b93b) /* 982 */, U64_C(0xfcc41e328cab4829) /* 983 */,
568 U64_C(0x03f38c96ba582c52) /* 984 */, U64_C(0xcad1bdbd7fd85db2) /* 985 */,
569 U64_C(0xbbb442c16082ae83) /* 986 */, U64_C(0xb95fe86ba5da9ab0) /* 987 */,
570 U64_C(0xb22e04673771a93f) /* 988 */, U64_C(0x845358c9493152d8) /* 989 */,
571 U64_C(0xbe2a488697b4541e) /* 990 */, U64_C(0x95a2dc2dd38e6966) /* 991 */,
572 U64_C(0xc02c11ac923c852b) /* 992 */, U64_C(0x2388b1990df2a87b) /* 993 */,
573 U64_C(0x7c8008fa1b4f37be) /* 994 */, U64_C(0x1f70d0c84d54e503) /* 995 */,
574 U64_C(0x5490adec7ece57d4) /* 996 */, U64_C(0x002b3c27d9063a3a) /* 997 */,
575 U64_C(0x7eaea3848030a2bf) /* 998 */, U64_C(0xc602326ded2003c0) /* 999 */,
576 U64_C(0x83a7287d69a94086) /* 1000 */, U64_C(0xc57a5fcb30f57a8a) /* 1001 */,
577 U64_C(0xb56844e479ebe779) /* 1002 */, U64_C(0xa373b40f05dcbce9) /* 1003 */,
578 U64_C(0xd71a786e88570ee2) /* 1004 */, U64_C(0x879cbacdbde8f6a0) /* 1005 */,
579 U64_C(0x976ad1bcc164a32f) /* 1006 */, U64_C(0xab21e25e9666d78b) /* 1007 */,
580 U64_C(0x901063aae5e5c33c) /* 1008 */, U64_C(0x9818b34448698d90) /* 1009 */,
581 U64_C(0xe36487ae3e1e8abb) /* 1010 */, U64_C(0xafbdf931893bdcb4) /* 1011 */,
582 U64_C(0x6345a0dc5fbbd519) /* 1012 */, U64_C(0x8628fe269b9465ca) /* 1013 */,
583 U64_C(0x1e5d01603f9c51ec) /* 1014 */, U64_C(0x4de44006a15049b7) /* 1015 */,
584 U64_C(0xbf6c70e5f776cbb1) /* 1016 */, U64_C(0x411218f2ef552bed) /* 1017 */,
585 U64_C(0xcb0c0708705a36a3) /* 1018 */, U64_C(0xe74d14754f986044) /* 1019 */,
586 U64_C(0xcd56d9430ea8280e) /* 1020 */, U64_C(0xc12591d7535f5065) /* 1021 */,
587 U64_C(0xc83223f1720aef96) /* 1022 */, U64_C(0xc3a0396f7363a51f) /* 1023 */
588};
589
590static void
591tiger_init( void *context )
592{
593 TIGER_CONTEXT *hd = context;
594
595 hd->a = 0x0123456789abcdefLL;
596 hd->b = 0xfedcba9876543210LL;
597 hd->c = 0xf096a5b4c3b2e187LL;
598 hd->nblocks = 0;
599 hd->count = 0;
600}
601
602static void
603round( u64 *ra, u64 *rb, u64 *rc, u64 x, int mul )
604{
605 u64 a = *ra;
606 u64 b = *rb;
607 u64 c = *rc;
608
609 c ^= x;
610 a -= ( sbox1[ c & 0xff ] ^ sbox2[ (c >> 16) & 0xff ]
611 ^ sbox3[ (c >> 32) & 0xff ] ^ sbox4[ (c >> 48) & 0xff ]);
612 b += ( sbox4[ (c >> 8) & 0xff ] ^ sbox3[ (c >> 24) & 0xff ]
613 ^ sbox2[ (c >> 40) & 0xff ] ^ sbox1[ (c >> 56) & 0xff ]);
614 b *= mul;
615
616 *ra = a;
617 *rb = b;
618 *rc = c;
619}
620
621
622static void
623pass( u64 *ra, u64 *rb, u64 *rc, u64 *x, int mul )
624{
625 u64 a = *ra;
626 u64 b = *rb;
627 u64 c = *rc;
628
629 round( &a, &b, &c, x[0], mul );
630 round( &b, &c, &a, x[1], mul );
631 round( &c, &a, &b, x[2], mul );
632 round( &a, &b, &c, x[3], mul );
633 round( &b, &c, &a, x[4], mul );
634 round( &c, &a, &b, x[5], mul );
635 round( &a, &b, &c, x[6], mul );
636 round( &b, &c, &a, x[7], mul );
637
638 *ra = a;
639 *rb = b;
640 *rc = c;
641}
642
643
644static void
645key_schedule( u64 *x )
646{
647 x[0] -= x[7] ^ 0xa5a5a5a5a5a5a5a5LL;
648 x[1] ^= x[0];
649 x[2] += x[1];
650 x[3] -= x[2] ^ ((~x[1]) << 19 );
651 x[4] ^= x[3];
652 x[5] += x[4];
653 x[6] -= x[5] ^ ((~x[4]) >> 23 );
654 x[7] ^= x[6];
655 x[0] += x[7];
656 x[1] -= x[0] ^ ((~x[7]) << 19 );
657 x[2] ^= x[1];
658 x[3] += x[2];
659 x[4] -= x[3] ^ ((~x[2]) >> 23 );
660 x[5] ^= x[4];
661 x[6] += x[5];
662 x[7] -= x[6] ^ 0x0123456789abcdefLL;
663}
664
665
666/****************
667 * Transform the message DATA which consists of 512 bytes (8 words)
668 */
669static void
670transform( TIGER_CONTEXT *hd, byte *data )
671{
672 u64 a,b,c,aa,bb,cc;
673 u64 x[8];
674#ifdef WORDS_BIGENDIAN
675#define MKWORD(d,n) \
676 ( ((u64)(d)[8*(n)+7]) << 56 | ((u64)(d)[8*(n)+6]) << 48 \
677 | ((u64)(d)[8*(n)+5]) << 40 | ((u64)(d)[8*(n)+4]) << 32 \
678 | ((u64)(d)[8*(n)+3]) << 24 | ((u64)(d)[8*(n)+2]) << 16 \
679 | ((u64)(d)[8*(n)+1]) << 8 | ((u64)(d)[8*(n) ]) )
680 x[0] = MKWORD(data, 0);
681 x[1] = MKWORD(data, 1);
682 x[2] = MKWORD(data, 2);
683 x[3] = MKWORD(data, 3);
684 x[4] = MKWORD(data, 4);
685 x[5] = MKWORD(data, 5);
686 x[6] = MKWORD(data, 6);
687 x[7] = MKWORD(data, 7);
688#undef MKWORD
689#else
690 memcpy( &x[0], data, 64 );
691#endif
692
693 /* save */
694 a = aa = hd->a;
695 b = bb = hd->b;
696 c = cc = hd->c;
697
698 pass( &a, &b, &c, x, 5);
699 key_schedule( x );
700 pass( &c, &a, &b, x, 7);
701 key_schedule( x );
702 pass( &b, &c, &a, x, 9);
703
704 /* feedforward */
705 a ^= aa;
706 b -= bb;
707 c += cc;
708 /* store */
709 hd->a = a;
710 hd->b = b;
711 hd->c = c;
712}
713
714
715
716/* Update the message digest with the contents
717 * of INBUF with length INLEN.
718 */
719static void
720tiger_write( void *context, byte *inbuf, size_t inlen)
721{
722 TIGER_CONTEXT *hd = context;
723
724 if( hd->count == 64 ) /* flush the buffer */
725 {
726 transform( hd, hd->buf );
727 _gcry_burn_stack (21*8+11*sizeof(void*));
728 hd->count = 0;
729 hd->nblocks++;
730 }
731 if( !inbuf )
732 return;
733 if( hd->count )
734 {
735 for( ; inlen && hd->count < 64; inlen-- )
736 hd->buf[hd->count++] = *inbuf++;
737 tiger_write( hd, NULL, 0 );
738 if( !inlen )
739 return;
740 }
741
742 while( inlen >= 64 )
743 {
744 transform( hd, inbuf );
745 hd->count = 0;
746 hd->nblocks++;
747 inlen -= 64;
748 inbuf += 64;
749 }
750 _gcry_burn_stack (21*8+11*sizeof(void*));
751 for( ; inlen && hd->count < 64; inlen-- )
752 hd->buf[hd->count++] = *inbuf++;
753}
754
755
756
757/* The routine terminates the computation
758 */
759static void
760tiger_final( void *context )
761{
762 TIGER_CONTEXT *hd = context;
763 u32 t, msb, lsb;
764 byte *p;
765
766 tiger_write(hd, NULL, 0); /* flush */;
767
768 t = hd->nblocks;
769 /* multiply by 64 to make a byte count */
770 lsb = t << 6;
771 msb = t >> 26;
772 /* add the count */
773 t = lsb;
774 if( (lsb += hd->count) < t )
775 msb++;
776 /* multiply by 8 to make a bit count */
777 t = lsb;
778 lsb <<= 3;
779 msb <<= 3;
780 msb |= t >> 29;
781
782 if( hd->count < 56 ) /* enough room */
783 {
784 hd->buf[hd->count++] = 0x01; /* pad */
785 while( hd->count < 56 )
786 hd->buf[hd->count++] = 0; /* pad */
787 }
788 else /* need one extra block */
789 {
790 hd->buf[hd->count++] = 0x01; /* pad character */
791 while( hd->count < 64 )
792 hd->buf[hd->count++] = 0;
793 tiger_write(hd, NULL, 0); /* flush */;
794 memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
795 }
796 /* append the 64 bit count */
797 hd->buf[56] = lsb ;
798 hd->buf[57] = lsb >> 8;
799 hd->buf[58] = lsb >> 16;
800 hd->buf[59] = lsb >> 24;
801 hd->buf[60] = msb ;
802 hd->buf[61] = msb >> 8;
803 hd->buf[62] = msb >> 16;
804 hd->buf[63] = msb >> 24;
805 transform( hd, hd->buf );
806 _gcry_burn_stack (21*8+11*sizeof(void*));
807
808 p = hd->buf;
809#ifdef WORDS_BIGENDIAN
810#define X(a) do { *(u64*)p = hd->a ; p += 8; } while(0)
811#else /* little endian */
812#define X(a) do { *p++ = hd->a >> 56; *p++ = hd->a >> 48; \
813 *p++ = hd->a >> 40; *p++ = hd->a >> 32; \
814 *p++ = hd->a >> 24; *p++ = hd->a >> 16; \
815 *p++ = hd->a >> 8; *p++ = hd->a; } while(0)
816#endif
817 X(a);
818 X(b);
819 X(c);
820#undef X
821}
822
823static byte *
824tiger_read( void *context )
825{
826 TIGER_CONTEXT *hd = context;
827
828 return hd->buf;
829}
830
831static byte asn[19] = /* Object ID is 1.3.6.1.4.1.11591.12.2 */
832 { 0x30, 0x29, 0x30, 0x0d, 0x06, 0x09, 0x2b, 0x06,
833 0x01, 0x04, 0x01, 0xda, 0x47, 0x0c, 0x02,
834 0x05, 0x00, 0x04, 0x18 };
835
836static gcry_md_oid_spec_t oid_spec_tiger[] =
837 {
838 /* GNU.digestAlgorithm TIGER */
839 { "1.3.6.1.4.1.11591.12.2" },
840 { NULL }
841 };
842
843gcry_md_spec_t _gcry_digest_spec_tiger =
844 {
845 "TIGER192", asn, DIM (asn), oid_spec_tiger, 24,
846 tiger_init, tiger_write, tiger_final, tiger_read,
847 sizeof (TIGER_CONTEXT)
848 };
849
850#endif /* HAVE_U64_TYPEDEF */
diff --git a/pwmanager/libcrypt/cipher/twofish.c b/pwmanager/libcrypt/cipher/twofish.c
new file mode 100644
index 0000000..32589a0
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/twofish.c
@@ -0,0 +1,1040 @@
1/* Twofish for GPG
2 * Copyright (C) 1998, 2002, 2003 Free Software Foundation, Inc.
3 * Written by Matthew Skala <mskala@ansuz.sooke.bc.ca>, July 26, 1998
4 * 256-bit key length added March 20, 1999
5 * Some modifications to reduce the text size by Werner Koch, April, 1998
6 *
7 * This file is part of Libgcrypt.
8 *
9 * Libgcrypt is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * Libgcrypt is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22 ********************************************************************
23 *
24 * This code is a "clean room" implementation, written from the paper
25 * _Twofish: A 128-Bit Block Cipher_ by Bruce Schneier, John Kelsey,
26 * Doug Whiting, David Wagner, Chris Hall, and Niels Ferguson, available
27 * through http://www.counterpane.com/twofish.html
28 *
29 * For background information on multiplication in finite fields, used for
30 * the matrix operations in the key schedule, see the book _Contemporary
31 * Abstract Algebra_ by Joseph A. Gallian, especially chapter 22 in the
32 * Third Edition.
33 *
34 * Only the 128- and 256-bit key sizes are supported. This code is intended
35 * for GNU C on a 32-bit system, but it should work almost anywhere. Loops
36 * are unrolled, precomputation tables are used, etc., for maximum speed at
37 * some cost in memory consumption. */
38
39#include <config.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h> /* for memcmp() */
43
44#include "types.h" /* for byte and u32 typedefs */
45#include "g10lib.h"
46#include "cipher.h"
47
48/* Prototype for the self-test function. */
49static const char *selftest(void);
50
51/* Structure for an expanded Twofish key. s contains the key-dependent
52 * S-boxes composed with the MDS matrix; w contains the eight "whitening"
53 * subkeys, K[0] through K[7].k holds the remaining, "round" subkeys. Note
54 * that k[i] corresponds to what the Twofish paper calls K[i+8]. */
55typedef struct {
56 u32 s[4][256], w[8], k[32];
57} TWOFISH_context;
58
59/* These two tables are the q0 and q1 permutations, exactly as described in
60 * the Twofish paper. */
61
62static const byte q0[256] = {
63 0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76, 0x9A, 0x92, 0x80, 0x78,
64 0xE4, 0xDD, 0xD1, 0x38, 0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C,
65 0x43, 0x75, 0x37, 0x26, 0xFA, 0x13, 0x94, 0x48, 0xF2, 0xD0, 0x8B, 0x30,
66 0x84, 0x54, 0xDF, 0x23, 0x19, 0x5B, 0x3D, 0x59, 0xF3, 0xAE, 0xA2, 0x82,
67 0x63, 0x01, 0x83, 0x2E, 0xD9, 0x51, 0x9B, 0x7C, 0xA6, 0xEB, 0xA5, 0xBE,
68 0x16, 0x0C, 0xE3, 0x61, 0xC0, 0x8C, 0x3A, 0xF5, 0x73, 0x2C, 0x25, 0x0B,
69 0xBB, 0x4E, 0x89, 0x6B, 0x53, 0x6A, 0xB4, 0xF1, 0xE1, 0xE6, 0xBD, 0x45,
70 0xE2, 0xF4, 0xB6, 0x66, 0xCC, 0x95, 0x03, 0x56, 0xD4, 0x1C, 0x1E, 0xD7,
71 0xFB, 0xC3, 0x8E, 0xB5, 0xE9, 0xCF, 0xBF, 0xBA, 0xEA, 0x77, 0x39, 0xAF,
72 0x33, 0xC9, 0x62, 0x71, 0x81, 0x79, 0x09, 0xAD, 0x24, 0xCD, 0xF9, 0xD8,
73 0xE5, 0xC5, 0xB9, 0x4D, 0x44, 0x08, 0x86, 0xE7, 0xA1, 0x1D, 0xAA, 0xED,
74 0x06, 0x70, 0xB2, 0xD2, 0x41, 0x7B, 0xA0, 0x11, 0x31, 0xC2, 0x27, 0x90,
75 0x20, 0xF6, 0x60, 0xFF, 0x96, 0x5C, 0xB1, 0xAB, 0x9E, 0x9C, 0x52, 0x1B,
76 0x5F, 0x93, 0x0A, 0xEF, 0x91, 0x85, 0x49, 0xEE, 0x2D, 0x4F, 0x8F, 0x3B,
77 0x47, 0x87, 0x6D, 0x46, 0xD6, 0x3E, 0x69, 0x64, 0x2A, 0xCE, 0xCB, 0x2F,
78 0xFC, 0x97, 0x05, 0x7A, 0xAC, 0x7F, 0xD5, 0x1A, 0x4B, 0x0E, 0xA7, 0x5A,
79 0x28, 0x14, 0x3F, 0x29, 0x88, 0x3C, 0x4C, 0x02, 0xB8, 0xDA, 0xB0, 0x17,
80 0x55, 0x1F, 0x8A, 0x7D, 0x57, 0xC7, 0x8D, 0x74, 0xB7, 0xC4, 0x9F, 0x72,
81 0x7E, 0x15, 0x22, 0x12, 0x58, 0x07, 0x99, 0x34, 0x6E, 0x50, 0xDE, 0x68,
82 0x65, 0xBC, 0xDB, 0xF8, 0xC8, 0xA8, 0x2B, 0x40, 0xDC, 0xFE, 0x32, 0xA4,
83 0xCA, 0x10, 0x21, 0xF0, 0xD3, 0x5D, 0x0F, 0x00, 0x6F, 0x9D, 0x36, 0x42,
84 0x4A, 0x5E, 0xC1, 0xE0
85};
86
87static const byte q1[256] = {
88 0x75, 0xF3, 0xC6, 0xF4, 0xDB, 0x7B, 0xFB, 0xC8, 0x4A, 0xD3, 0xE6, 0x6B,
89 0x45, 0x7D, 0xE8, 0x4B, 0xD6, 0x32, 0xD8, 0xFD, 0x37, 0x71, 0xF1, 0xE1,
90 0x30, 0x0F, 0xF8, 0x1B, 0x87, 0xFA, 0x06, 0x3F, 0x5E, 0xBA, 0xAE, 0x5B,
91 0x8A, 0x00, 0xBC, 0x9D, 0x6D, 0xC1, 0xB1, 0x0E, 0x80, 0x5D, 0xD2, 0xD5,
92 0xA0, 0x84, 0x07, 0x14, 0xB5, 0x90, 0x2C, 0xA3, 0xB2, 0x73, 0x4C, 0x54,
93 0x92, 0x74, 0x36, 0x51, 0x38, 0xB0, 0xBD, 0x5A, 0xFC, 0x60, 0x62, 0x96,
94 0x6C, 0x42, 0xF7, 0x10, 0x7C, 0x28, 0x27, 0x8C, 0x13, 0x95, 0x9C, 0xC7,
95 0x24, 0x46, 0x3B, 0x70, 0xCA, 0xE3, 0x85, 0xCB, 0x11, 0xD0, 0x93, 0xB8,
96 0xA6, 0x83, 0x20, 0xFF, 0x9F, 0x77, 0xC3, 0xCC, 0x03, 0x6F, 0x08, 0xBF,
97 0x40, 0xE7, 0x2B, 0xE2, 0x79, 0x0C, 0xAA, 0x82, 0x41, 0x3A, 0xEA, 0xB9,
98 0xE4, 0x9A, 0xA4, 0x97, 0x7E, 0xDA, 0x7A, 0x17, 0x66, 0x94, 0xA1, 0x1D,
99 0x3D, 0xF0, 0xDE, 0xB3, 0x0B, 0x72, 0xA7, 0x1C, 0xEF, 0xD1, 0x53, 0x3E,
100 0x8F, 0x33, 0x26, 0x5F, 0xEC, 0x76, 0x2A, 0x49, 0x81, 0x88, 0xEE, 0x21,
101 0xC4, 0x1A, 0xEB, 0xD9, 0xC5, 0x39, 0x99, 0xCD, 0xAD, 0x31, 0x8B, 0x01,
102 0x18, 0x23, 0xDD, 0x1F, 0x4E, 0x2D, 0xF9, 0x48, 0x4F, 0xF2, 0x65, 0x8E,
103 0x78, 0x5C, 0x58, 0x19, 0x8D, 0xE5, 0x98, 0x57, 0x67, 0x7F, 0x05, 0x64,
104 0xAF, 0x63, 0xB6, 0xFE, 0xF5, 0xB7, 0x3C, 0xA5, 0xCE, 0xE9, 0x68, 0x44,
105 0xE0, 0x4D, 0x43, 0x69, 0x29, 0x2E, 0xAC, 0x15, 0x59, 0xA8, 0x0A, 0x9E,
106 0x6E, 0x47, 0xDF, 0x34, 0x35, 0x6A, 0xCF, 0xDC, 0x22, 0xC9, 0xC0, 0x9B,
107 0x89, 0xD4, 0xED, 0xAB, 0x12, 0xA2, 0x0D, 0x52, 0xBB, 0x02, 0x2F, 0xA9,
108 0xD7, 0x61, 0x1E, 0xB4, 0x50, 0x04, 0xF6, 0xC2, 0x16, 0x25, 0x86, 0x56,
109 0x55, 0x09, 0xBE, 0x91
110};
111
112/* These MDS tables are actually tables of MDS composed with q0 and q1,
113 * because it is only ever used that way and we can save some time by
114 * precomputing. Of course the main saving comes from precomputing the
115 * GF(2^8) multiplication involved in the MDS matrix multiply; by looking
116 * things up in these tables we reduce the matrix multiply to four lookups
117 * and three XORs. Semi-formally, the definition of these tables is:
118 * mds[0][i] = MDS (q1[i] 0 0 0)^T mds[1][i] = MDS (0 q0[i] 0 0)^T
119 * mds[2][i] = MDS (0 0 q1[i] 0)^T mds[3][i] = MDS (0 0 0 q0[i])^T
120 * where ^T means "transpose", the matrix multiply is performed in GF(2^8)
121 * represented as GF(2)[x]/v(x) where v(x)=x^8+x^6+x^5+x^3+1 as described
122 * by Schneier et al, and I'm casually glossing over the byte/word
123 * conversion issues. */
124
125static const u32 mds[4][256] = {
126 {0xBCBC3275, 0xECEC21F3, 0x202043C6, 0xB3B3C9F4, 0xDADA03DB, 0x02028B7B,
127 0xE2E22BFB, 0x9E9EFAC8, 0xC9C9EC4A, 0xD4D409D3, 0x18186BE6, 0x1E1E9F6B,
128 0x98980E45, 0xB2B2387D, 0xA6A6D2E8, 0x2626B74B, 0x3C3C57D6, 0x93938A32,
129 0x8282EED8, 0x525298FD, 0x7B7BD437, 0xBBBB3771, 0x5B5B97F1, 0x474783E1,
130 0x24243C30, 0x5151E20F, 0xBABAC6F8, 0x4A4AF31B, 0xBFBF4887, 0x0D0D70FA,
131 0xB0B0B306, 0x7575DE3F, 0xD2D2FD5E, 0x7D7D20BA, 0x666631AE, 0x3A3AA35B,
132 0x59591C8A, 0x00000000, 0xCDCD93BC, 0x1A1AE09D, 0xAEAE2C6D, 0x7F7FABC1,
133 0x2B2BC7B1, 0xBEBEB90E, 0xE0E0A080, 0x8A8A105D, 0x3B3B52D2, 0x6464BAD5,
134 0xD8D888A0, 0xE7E7A584, 0x5F5FE807, 0x1B1B1114, 0x2C2CC2B5, 0xFCFCB490,
135 0x3131272C, 0x808065A3, 0x73732AB2, 0x0C0C8173, 0x79795F4C, 0x6B6B4154,
136 0x4B4B0292, 0x53536974, 0x94948F36, 0x83831F51, 0x2A2A3638, 0xC4C49CB0,
137 0x2222C8BD, 0xD5D5F85A, 0xBDBDC3FC, 0x48487860, 0xFFFFCE62, 0x4C4C0796,
138 0x4141776C, 0xC7C7E642, 0xEBEB24F7, 0x1C1C1410, 0x5D5D637C, 0x36362228,
139 0x6767C027, 0xE9E9AF8C, 0x4444F913, 0x1414EA95, 0xF5F5BB9C, 0xCFCF18C7,
140 0x3F3F2D24, 0xC0C0E346, 0x7272DB3B, 0x54546C70, 0x29294CCA, 0xF0F035E3,
141 0x0808FE85, 0xC6C617CB, 0xF3F34F11, 0x8C8CE4D0, 0xA4A45993, 0xCACA96B8,
142 0x68683BA6, 0xB8B84D83, 0x38382820, 0xE5E52EFF, 0xADAD569F, 0x0B0B8477,
143 0xC8C81DC3, 0x9999FFCC, 0x5858ED03, 0x19199A6F, 0x0E0E0A08, 0x95957EBF,
144 0x70705040, 0xF7F730E7, 0x6E6ECF2B, 0x1F1F6EE2, 0xB5B53D79, 0x09090F0C,
145 0x616134AA, 0x57571682, 0x9F9F0B41, 0x9D9D803A, 0x111164EA, 0x2525CDB9,
146 0xAFAFDDE4, 0x4545089A, 0xDFDF8DA4, 0xA3A35C97, 0xEAEAD57E, 0x353558DA,
147 0xEDEDD07A, 0x4343FC17, 0xF8F8CB66, 0xFBFBB194, 0x3737D3A1, 0xFAFA401D,
148 0xC2C2683D, 0xB4B4CCF0, 0x32325DDE, 0x9C9C71B3, 0x5656E70B, 0xE3E3DA72,
149 0x878760A7, 0x15151B1C, 0xF9F93AEF, 0x6363BFD1, 0x3434A953, 0x9A9A853E,
150 0xB1B1428F, 0x7C7CD133, 0x88889B26, 0x3D3DA65F, 0xA1A1D7EC, 0xE4E4DF76,
151 0x8181942A, 0x91910149, 0x0F0FFB81, 0xEEEEAA88, 0x161661EE, 0xD7D77321,
152 0x9797F5C4, 0xA5A5A81A, 0xFEFE3FEB, 0x6D6DB5D9, 0x7878AEC5, 0xC5C56D39,
153 0x1D1DE599, 0x7676A4CD, 0x3E3EDCAD, 0xCBCB6731, 0xB6B6478B, 0xEFEF5B01,
154 0x12121E18, 0x6060C523, 0x6A6AB0DD, 0x4D4DF61F, 0xCECEE94E, 0xDEDE7C2D,
155 0x55559DF9, 0x7E7E5A48, 0x2121B24F, 0x03037AF2, 0xA0A02665, 0x5E5E198E,
156 0x5A5A6678, 0x65654B5C, 0x62624E58, 0xFDFD4519, 0x0606F48D, 0x404086E5,
157 0xF2F2BE98, 0x3333AC57, 0x17179067, 0x05058E7F, 0xE8E85E05, 0x4F4F7D64,
158 0x89896AAF, 0x10109563, 0x74742FB6, 0x0A0A75FE, 0x5C5C92F5, 0x9B9B74B7,
159 0x2D2D333C, 0x3030D6A5, 0x2E2E49CE, 0x494989E9, 0x46467268, 0x77775544,
160 0xA8A8D8E0, 0x9696044D, 0x2828BD43, 0xA9A92969, 0xD9D97929, 0x8686912E,
161 0xD1D187AC, 0xF4F44A15, 0x8D8D1559, 0xD6D682A8, 0xB9B9BC0A, 0x42420D9E,
162 0xF6F6C16E, 0x2F2FB847, 0xDDDD06DF, 0x23233934, 0xCCCC6235, 0xF1F1C46A,
163 0xC1C112CF, 0x8585EBDC, 0x8F8F9E22, 0x7171A1C9, 0x9090F0C0, 0xAAAA539B,
164 0x0101F189, 0x8B8BE1D4, 0x4E4E8CED, 0x8E8E6FAB, 0xABABA212, 0x6F6F3EA2,
165 0xE6E6540D, 0xDBDBF252, 0x92927BBB, 0xB7B7B602, 0x6969CA2F, 0x3939D9A9,
166 0xD3D30CD7, 0xA7A72361, 0xA2A2AD1E, 0xC3C399B4, 0x6C6C4450, 0x07070504,
167 0x04047FF6, 0x272746C2, 0xACACA716, 0xD0D07625, 0x50501386, 0xDCDCF756,
168 0x84841A55, 0xE1E15109, 0x7A7A25BE, 0x1313EF91},
169
170 {0xA9D93939, 0x67901717, 0xB3719C9C, 0xE8D2A6A6, 0x04050707, 0xFD985252,
171 0xA3658080, 0x76DFE4E4, 0x9A084545, 0x92024B4B, 0x80A0E0E0, 0x78665A5A,
172 0xE4DDAFAF, 0xDDB06A6A, 0xD1BF6363, 0x38362A2A, 0x0D54E6E6, 0xC6432020,
173 0x3562CCCC, 0x98BEF2F2, 0x181E1212, 0xF724EBEB, 0xECD7A1A1, 0x6C774141,
174 0x43BD2828, 0x7532BCBC, 0x37D47B7B, 0x269B8888, 0xFA700D0D, 0x13F94444,
175 0x94B1FBFB, 0x485A7E7E, 0xF27A0303, 0xD0E48C8C, 0x8B47B6B6, 0x303C2424,
176 0x84A5E7E7, 0x54416B6B, 0xDF06DDDD, 0x23C56060, 0x1945FDFD, 0x5BA33A3A,
177 0x3D68C2C2, 0x59158D8D, 0xF321ECEC, 0xAE316666, 0xA23E6F6F, 0x82165757,
178 0x63951010, 0x015BEFEF, 0x834DB8B8, 0x2E918686, 0xD9B56D6D, 0x511F8383,
179 0x9B53AAAA, 0x7C635D5D, 0xA63B6868, 0xEB3FFEFE, 0xA5D63030, 0xBE257A7A,
180 0x16A7ACAC, 0x0C0F0909, 0xE335F0F0, 0x6123A7A7, 0xC0F09090, 0x8CAFE9E9,
181 0x3A809D9D, 0xF5925C5C, 0x73810C0C, 0x2C273131, 0x2576D0D0, 0x0BE75656,
182 0xBB7B9292, 0x4EE9CECE, 0x89F10101, 0x6B9F1E1E, 0x53A93434, 0x6AC4F1F1,
183 0xB499C3C3, 0xF1975B5B, 0xE1834747, 0xE66B1818, 0xBDC82222, 0x450E9898,
184 0xE26E1F1F, 0xF4C9B3B3, 0xB62F7474, 0x66CBF8F8, 0xCCFF9999, 0x95EA1414,
185 0x03ED5858, 0x56F7DCDC, 0xD4E18B8B, 0x1C1B1515, 0x1EADA2A2, 0xD70CD3D3,
186 0xFB2BE2E2, 0xC31DC8C8, 0x8E195E5E, 0xB5C22C2C, 0xE9894949, 0xCF12C1C1,
187 0xBF7E9595, 0xBA207D7D, 0xEA641111, 0x77840B0B, 0x396DC5C5, 0xAF6A8989,
188 0x33D17C7C, 0xC9A17171, 0x62CEFFFF, 0x7137BBBB, 0x81FB0F0F, 0x793DB5B5,
189 0x0951E1E1, 0xADDC3E3E, 0x242D3F3F, 0xCDA47676, 0xF99D5555, 0xD8EE8282,
190 0xE5864040, 0xC5AE7878, 0xB9CD2525, 0x4D049696, 0x44557777, 0x080A0E0E,
191 0x86135050, 0xE730F7F7, 0xA1D33737, 0x1D40FAFA, 0xAA346161, 0xED8C4E4E,
192 0x06B3B0B0, 0x706C5454, 0xB22A7373, 0xD2523B3B, 0x410B9F9F, 0x7B8B0202,
193 0xA088D8D8, 0x114FF3F3, 0x3167CBCB, 0xC2462727, 0x27C06767, 0x90B4FCFC,
194 0x20283838, 0xF67F0404, 0x60784848, 0xFF2EE5E5, 0x96074C4C, 0x5C4B6565,
195 0xB1C72B2B, 0xAB6F8E8E, 0x9E0D4242, 0x9CBBF5F5, 0x52F2DBDB, 0x1BF34A4A,
196 0x5FA63D3D, 0x9359A4A4, 0x0ABCB9B9, 0xEF3AF9F9, 0x91EF1313, 0x85FE0808,
197 0x49019191, 0xEE611616, 0x2D7CDEDE, 0x4FB22121, 0x8F42B1B1, 0x3BDB7272,
198 0x47B82F2F, 0x8748BFBF, 0x6D2CAEAE, 0x46E3C0C0, 0xD6573C3C, 0x3E859A9A,
199 0x6929A9A9, 0x647D4F4F, 0x2A948181, 0xCE492E2E, 0xCB17C6C6, 0x2FCA6969,
200 0xFCC3BDBD, 0x975CA3A3, 0x055EE8E8, 0x7AD0EDED, 0xAC87D1D1, 0x7F8E0505,
201 0xD5BA6464, 0x1AA8A5A5, 0x4BB72626, 0x0EB9BEBE, 0xA7608787, 0x5AF8D5D5,
202 0x28223636, 0x14111B1B, 0x3FDE7575, 0x2979D9D9, 0x88AAEEEE, 0x3C332D2D,
203 0x4C5F7979, 0x02B6B7B7, 0xB896CACA, 0xDA583535, 0xB09CC4C4, 0x17FC4343,
204 0x551A8484, 0x1FF64D4D, 0x8A1C5959, 0x7D38B2B2, 0x57AC3333, 0xC718CFCF,
205 0x8DF40606, 0x74695353, 0xB7749B9B, 0xC4F59797, 0x9F56ADAD, 0x72DAE3E3,
206 0x7ED5EAEA, 0x154AF4F4, 0x229E8F8F, 0x12A2ABAB, 0x584E6262, 0x07E85F5F,
207 0x99E51D1D, 0x34392323, 0x6EC1F6F6, 0x50446C6C, 0xDE5D3232, 0x68724646,
208 0x6526A0A0, 0xBC93CDCD, 0xDB03DADA, 0xF8C6BABA, 0xC8FA9E9E, 0xA882D6D6,
209 0x2BCF6E6E, 0x40507070, 0xDCEB8585, 0xFE750A0A, 0x328A9393, 0xA48DDFDF,
210 0xCA4C2929, 0x10141C1C, 0x2173D7D7, 0xF0CCB4B4, 0xD309D4D4, 0x5D108A8A,
211 0x0FE25151, 0x00000000, 0x6F9A1919, 0x9DE01A1A, 0x368F9494, 0x42E6C7C7,
212 0x4AECC9C9, 0x5EFDD2D2, 0xC1AB7F7F, 0xE0D8A8A8},
213
214 {0xBC75BC32, 0xECF3EC21, 0x20C62043, 0xB3F4B3C9, 0xDADBDA03, 0x027B028B,
215 0xE2FBE22B, 0x9EC89EFA, 0xC94AC9EC, 0xD4D3D409, 0x18E6186B, 0x1E6B1E9F,
216 0x9845980E, 0xB27DB238, 0xA6E8A6D2, 0x264B26B7, 0x3CD63C57, 0x9332938A,
217 0x82D882EE, 0x52FD5298, 0x7B377BD4, 0xBB71BB37, 0x5BF15B97, 0x47E14783,
218 0x2430243C, 0x510F51E2, 0xBAF8BAC6, 0x4A1B4AF3, 0xBF87BF48, 0x0DFA0D70,
219 0xB006B0B3, 0x753F75DE, 0xD25ED2FD, 0x7DBA7D20, 0x66AE6631, 0x3A5B3AA3,
220 0x598A591C, 0x00000000, 0xCDBCCD93, 0x1A9D1AE0, 0xAE6DAE2C, 0x7FC17FAB,
221 0x2BB12BC7, 0xBE0EBEB9, 0xE080E0A0, 0x8A5D8A10, 0x3BD23B52, 0x64D564BA,
222 0xD8A0D888, 0xE784E7A5, 0x5F075FE8, 0x1B141B11, 0x2CB52CC2, 0xFC90FCB4,
223 0x312C3127, 0x80A38065, 0x73B2732A, 0x0C730C81, 0x794C795F, 0x6B546B41,
224 0x4B924B02, 0x53745369, 0x9436948F, 0x8351831F, 0x2A382A36, 0xC4B0C49C,
225 0x22BD22C8, 0xD55AD5F8, 0xBDFCBDC3, 0x48604878, 0xFF62FFCE, 0x4C964C07,
226 0x416C4177, 0xC742C7E6, 0xEBF7EB24, 0x1C101C14, 0x5D7C5D63, 0x36283622,
227 0x672767C0, 0xE98CE9AF, 0x441344F9, 0x149514EA, 0xF59CF5BB, 0xCFC7CF18,
228 0x3F243F2D, 0xC046C0E3, 0x723B72DB, 0x5470546C, 0x29CA294C, 0xF0E3F035,
229 0x088508FE, 0xC6CBC617, 0xF311F34F, 0x8CD08CE4, 0xA493A459, 0xCAB8CA96,
230 0x68A6683B, 0xB883B84D, 0x38203828, 0xE5FFE52E, 0xAD9FAD56, 0x0B770B84,
231 0xC8C3C81D, 0x99CC99FF, 0x580358ED, 0x196F199A, 0x0E080E0A, 0x95BF957E,
232 0x70407050, 0xF7E7F730, 0x6E2B6ECF, 0x1FE21F6E, 0xB579B53D, 0x090C090F,
233 0x61AA6134, 0x57825716, 0x9F419F0B, 0x9D3A9D80, 0x11EA1164, 0x25B925CD,
234 0xAFE4AFDD, 0x459A4508, 0xDFA4DF8D, 0xA397A35C, 0xEA7EEAD5, 0x35DA3558,
235 0xED7AEDD0, 0x431743FC, 0xF866F8CB, 0xFB94FBB1, 0x37A137D3, 0xFA1DFA40,
236 0xC23DC268, 0xB4F0B4CC, 0x32DE325D, 0x9CB39C71, 0x560B56E7, 0xE372E3DA,
237 0x87A78760, 0x151C151B, 0xF9EFF93A, 0x63D163BF, 0x345334A9, 0x9A3E9A85,
238 0xB18FB142, 0x7C337CD1, 0x8826889B, 0x3D5F3DA6, 0xA1ECA1D7, 0xE476E4DF,
239 0x812A8194, 0x91499101, 0x0F810FFB, 0xEE88EEAA, 0x16EE1661, 0xD721D773,
240 0x97C497F5, 0xA51AA5A8, 0xFEEBFE3F, 0x6DD96DB5, 0x78C578AE, 0xC539C56D,
241 0x1D991DE5, 0x76CD76A4, 0x3EAD3EDC, 0xCB31CB67, 0xB68BB647, 0xEF01EF5B,
242 0x1218121E, 0x602360C5, 0x6ADD6AB0, 0x4D1F4DF6, 0xCE4ECEE9, 0xDE2DDE7C,
243 0x55F9559D, 0x7E487E5A, 0x214F21B2, 0x03F2037A, 0xA065A026, 0x5E8E5E19,
244 0x5A785A66, 0x655C654B, 0x6258624E, 0xFD19FD45, 0x068D06F4, 0x40E54086,
245 0xF298F2BE, 0x335733AC, 0x17671790, 0x057F058E, 0xE805E85E, 0x4F644F7D,
246 0x89AF896A, 0x10631095, 0x74B6742F, 0x0AFE0A75, 0x5CF55C92, 0x9BB79B74,
247 0x2D3C2D33, 0x30A530D6, 0x2ECE2E49, 0x49E94989, 0x46684672, 0x77447755,
248 0xA8E0A8D8, 0x964D9604, 0x284328BD, 0xA969A929, 0xD929D979, 0x862E8691,
249 0xD1ACD187, 0xF415F44A, 0x8D598D15, 0xD6A8D682, 0xB90AB9BC, 0x429E420D,
250 0xF66EF6C1, 0x2F472FB8, 0xDDDFDD06, 0x23342339, 0xCC35CC62, 0xF16AF1C4,
251 0xC1CFC112, 0x85DC85EB, 0x8F228F9E, 0x71C971A1, 0x90C090F0, 0xAA9BAA53,
252 0x018901F1, 0x8BD48BE1, 0x4EED4E8C, 0x8EAB8E6F, 0xAB12ABA2, 0x6FA26F3E,
253 0xE60DE654, 0xDB52DBF2, 0x92BB927B, 0xB702B7B6, 0x692F69CA, 0x39A939D9,
254 0xD3D7D30C, 0xA761A723, 0xA21EA2AD, 0xC3B4C399, 0x6C506C44, 0x07040705,
255 0x04F6047F, 0x27C22746, 0xAC16ACA7, 0xD025D076, 0x50865013, 0xDC56DCF7,
256 0x8455841A, 0xE109E151, 0x7ABE7A25, 0x139113EF},
257
258 {0xD939A9D9, 0x90176790, 0x719CB371, 0xD2A6E8D2, 0x05070405, 0x9852FD98,
259 0x6580A365, 0xDFE476DF, 0x08459A08, 0x024B9202, 0xA0E080A0, 0x665A7866,
260 0xDDAFE4DD, 0xB06ADDB0, 0xBF63D1BF, 0x362A3836, 0x54E60D54, 0x4320C643,
261 0x62CC3562, 0xBEF298BE, 0x1E12181E, 0x24EBF724, 0xD7A1ECD7, 0x77416C77,
262 0xBD2843BD, 0x32BC7532, 0xD47B37D4, 0x9B88269B, 0x700DFA70, 0xF94413F9,
263 0xB1FB94B1, 0x5A7E485A, 0x7A03F27A, 0xE48CD0E4, 0x47B68B47, 0x3C24303C,
264 0xA5E784A5, 0x416B5441, 0x06DDDF06, 0xC56023C5, 0x45FD1945, 0xA33A5BA3,
265 0x68C23D68, 0x158D5915, 0x21ECF321, 0x3166AE31, 0x3E6FA23E, 0x16578216,
266 0x95106395, 0x5BEF015B, 0x4DB8834D, 0x91862E91, 0xB56DD9B5, 0x1F83511F,
267 0x53AA9B53, 0x635D7C63, 0x3B68A63B, 0x3FFEEB3F, 0xD630A5D6, 0x257ABE25,
268 0xA7AC16A7, 0x0F090C0F, 0x35F0E335, 0x23A76123, 0xF090C0F0, 0xAFE98CAF,
269 0x809D3A80, 0x925CF592, 0x810C7381, 0x27312C27, 0x76D02576, 0xE7560BE7,
270 0x7B92BB7B, 0xE9CE4EE9, 0xF10189F1, 0x9F1E6B9F, 0xA93453A9, 0xC4F16AC4,
271 0x99C3B499, 0x975BF197, 0x8347E183, 0x6B18E66B, 0xC822BDC8, 0x0E98450E,
272 0x6E1FE26E, 0xC9B3F4C9, 0x2F74B62F, 0xCBF866CB, 0xFF99CCFF, 0xEA1495EA,
273 0xED5803ED, 0xF7DC56F7, 0xE18BD4E1, 0x1B151C1B, 0xADA21EAD, 0x0CD3D70C,
274 0x2BE2FB2B, 0x1DC8C31D, 0x195E8E19, 0xC22CB5C2, 0x8949E989, 0x12C1CF12,
275 0x7E95BF7E, 0x207DBA20, 0x6411EA64, 0x840B7784, 0x6DC5396D, 0x6A89AF6A,
276 0xD17C33D1, 0xA171C9A1, 0xCEFF62CE, 0x37BB7137, 0xFB0F81FB, 0x3DB5793D,
277 0x51E10951, 0xDC3EADDC, 0x2D3F242D, 0xA476CDA4, 0x9D55F99D, 0xEE82D8EE,
278 0x8640E586, 0xAE78C5AE, 0xCD25B9CD, 0x04964D04, 0x55774455, 0x0A0E080A,
279 0x13508613, 0x30F7E730, 0xD337A1D3, 0x40FA1D40, 0x3461AA34, 0x8C4EED8C,
280 0xB3B006B3, 0x6C54706C, 0x2A73B22A, 0x523BD252, 0x0B9F410B, 0x8B027B8B,
281 0x88D8A088, 0x4FF3114F, 0x67CB3167, 0x4627C246, 0xC06727C0, 0xB4FC90B4,
282 0x28382028, 0x7F04F67F, 0x78486078, 0x2EE5FF2E, 0x074C9607, 0x4B655C4B,
283 0xC72BB1C7, 0x6F8EAB6F, 0x0D429E0D, 0xBBF59CBB, 0xF2DB52F2, 0xF34A1BF3,
284 0xA63D5FA6, 0x59A49359, 0xBCB90ABC, 0x3AF9EF3A, 0xEF1391EF, 0xFE0885FE,
285 0x01914901, 0x6116EE61, 0x7CDE2D7C, 0xB2214FB2, 0x42B18F42, 0xDB723BDB,
286 0xB82F47B8, 0x48BF8748, 0x2CAE6D2C, 0xE3C046E3, 0x573CD657, 0x859A3E85,
287 0x29A96929, 0x7D4F647D, 0x94812A94, 0x492ECE49, 0x17C6CB17, 0xCA692FCA,
288 0xC3BDFCC3, 0x5CA3975C, 0x5EE8055E, 0xD0ED7AD0, 0x87D1AC87, 0x8E057F8E,
289 0xBA64D5BA, 0xA8A51AA8, 0xB7264BB7, 0xB9BE0EB9, 0x6087A760, 0xF8D55AF8,
290 0x22362822, 0x111B1411, 0xDE753FDE, 0x79D92979, 0xAAEE88AA, 0x332D3C33,
291 0x5F794C5F, 0xB6B702B6, 0x96CAB896, 0x5835DA58, 0x9CC4B09C, 0xFC4317FC,
292 0x1A84551A, 0xF64D1FF6, 0x1C598A1C, 0x38B27D38, 0xAC3357AC, 0x18CFC718,
293 0xF4068DF4, 0x69537469, 0x749BB774, 0xF597C4F5, 0x56AD9F56, 0xDAE372DA,
294 0xD5EA7ED5, 0x4AF4154A, 0x9E8F229E, 0xA2AB12A2, 0x4E62584E, 0xE85F07E8,
295 0xE51D99E5, 0x39233439, 0xC1F66EC1, 0x446C5044, 0x5D32DE5D, 0x72466872,
296 0x26A06526, 0x93CDBC93, 0x03DADB03, 0xC6BAF8C6, 0xFA9EC8FA, 0x82D6A882,
297 0xCF6E2BCF, 0x50704050, 0xEB85DCEB, 0x750AFE75, 0x8A93328A, 0x8DDFA48D,
298 0x4C29CA4C, 0x141C1014, 0x73D72173, 0xCCB4F0CC, 0x09D4D309, 0x108A5D10,
299 0xE2510FE2, 0x00000000, 0x9A196F9A, 0xE01A9DE0, 0x8F94368F, 0xE6C742E6,
300 0xECC94AEC, 0xFDD25EFD, 0xAB7FC1AB, 0xD8A8E0D8}
301};
302
303/* The exp_to_poly and poly_to_exp tables are used to perform efficient
304 * operations in GF(2^8) represented as GF(2)[x]/w(x) where
305 * w(x)=x^8+x^6+x^3+x^2+1. We care about doing that because it's part of the
306 * definition of the RS matrix in the key schedule. Elements of that field
307 * are polynomials of degree not greater than 7 and all coefficients 0 or 1,
308 * which can be represented naturally by bytes (just substitute x=2). In that
309 * form, GF(2^8) addition is the same as bitwise XOR, but GF(2^8)
310 * multiplication is inefficient without hardware support. To multiply
311 * faster, I make use of the fact x is a generator for the nonzero elements,
312 * so that every element p of GF(2)[x]/w(x) is either 0 or equal to (x)^n for
313 * some n in 0..254. Note that that caret is exponentiation in GF(2^8),
314 * *not* polynomial notation. So if I want to compute pq where p and q are
315 * in GF(2^8), I can just say:
316 * 1. if p=0 or q=0 then pq=0
317 * 2. otherwise, find m and n such that p=x^m and q=x^n
318 * 3. pq=(x^m)(x^n)=x^(m+n), so add m and n and find pq
319 * The translations in steps 2 and 3 are looked up in the tables
320 * poly_to_exp (for step 2) and exp_to_poly (for step 3). To see this
321 * in action, look at the CALC_S macro. As additional wrinkles, note that
322 * one of my operands is always a constant, so the poly_to_exp lookup on it
323 * is done in advance; I included the original values in the comments so
324 * readers can have some chance of recognizing that this *is* the RS matrix
325 * from the Twofish paper. I've only included the table entries I actually
326 * need; I never do a lookup on a variable input of zero and the biggest
327 * exponents I'll ever see are 254 (variable) and 237 (constant), so they'll
328 * never sum to more than 491.I'm repeating part of the exp_to_poly table
329 * so that I don't have to do mod-255 reduction in the exponent arithmetic.
330 * Since I know my constant operands are never zero, I only have to worry
331 * about zero values in the variable operand, and I do it with a simple
332 * conditional branch.I know conditionals are expensive, but I couldn't
333 * see a non-horrible way of avoiding them, and I did manage to group the
334 * statements so that each if covers four group multiplications. */
335
336static const byte poly_to_exp[255] = {
337 0x00, 0x01, 0x17, 0x02, 0x2E, 0x18, 0x53, 0x03, 0x6A, 0x2F, 0x93, 0x19,
338 0x34, 0x54, 0x45, 0x04, 0x5C, 0x6B, 0xB6, 0x30, 0xA6, 0x94, 0x4B, 0x1A,
339 0x8C, 0x35, 0x81, 0x55, 0xAA, 0x46, 0x0D, 0x05, 0x24, 0x5D, 0x87, 0x6C,
340 0x9B, 0xB7, 0xC1, 0x31, 0x2B, 0xA7, 0xA3, 0x95, 0x98, 0x4C, 0xCA, 0x1B,
341 0xE6, 0x8D, 0x73, 0x36, 0xCD, 0x82, 0x12, 0x56, 0x62, 0xAB, 0xF0, 0x47,
342 0x4F, 0x0E, 0xBD, 0x06, 0xD4, 0x25, 0xD2, 0x5E, 0x27, 0x88, 0x66, 0x6D,
343 0xD6, 0x9C, 0x79, 0xB8, 0x08, 0xC2, 0xDF, 0x32, 0x68, 0x2C, 0xFD, 0xA8,
344 0x8A, 0xA4, 0x5A, 0x96, 0x29, 0x99, 0x22, 0x4D, 0x60, 0xCB, 0xE4, 0x1C,
345 0x7B, 0xE7, 0x3B, 0x8E, 0x9E, 0x74, 0xF4, 0x37, 0xD8, 0xCE, 0xF9, 0x83,
346 0x6F, 0x13, 0xB2, 0x57, 0xE1, 0x63, 0xDC, 0xAC, 0xC4, 0xF1, 0xAF, 0x48,
347 0x0A, 0x50, 0x42, 0x0F, 0xBA, 0xBE, 0xC7, 0x07, 0xDE, 0xD5, 0x78, 0x26,
348 0x65, 0xD3, 0xD1, 0x5F, 0xE3, 0x28, 0x21, 0x89, 0x59, 0x67, 0xFC, 0x6E,
349 0xB1, 0xD7, 0xF8, 0x9D, 0xF3, 0x7A, 0x3A, 0xB9, 0xC6, 0x09, 0x41, 0xC3,
350 0xAE, 0xE0, 0xDB, 0x33, 0x44, 0x69, 0x92, 0x2D, 0x52, 0xFE, 0x16, 0xA9,
351 0x0C, 0x8B, 0x80, 0xA5, 0x4A, 0x5B, 0xB5, 0x97, 0xC9, 0x2A, 0xA2, 0x9A,
352 0xC0, 0x23, 0x86, 0x4E, 0xBC, 0x61, 0xEF, 0xCC, 0x11, 0xE5, 0x72, 0x1D,
353 0x3D, 0x7C, 0xEB, 0xE8, 0xE9, 0x3C, 0xEA, 0x8F, 0x7D, 0x9F, 0xEC, 0x75,
354 0x1E, 0xF5, 0x3E, 0x38, 0xF6, 0xD9, 0x3F, 0xCF, 0x76, 0xFA, 0x1F, 0x84,
355 0xA0, 0x70, 0xED, 0x14, 0x90, 0xB3, 0x7E, 0x58, 0xFB, 0xE2, 0x20, 0x64,
356 0xD0, 0xDD, 0x77, 0xAD, 0xDA, 0xC5, 0x40, 0xF2, 0x39, 0xB0, 0xF7, 0x49,
357 0xB4, 0x0B, 0x7F, 0x51, 0x15, 0x43, 0x91, 0x10, 0x71, 0xBB, 0xEE, 0xBF,
358 0x85, 0xC8, 0xA1
359};
360
361static const byte exp_to_poly[492] = {
362 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x4D, 0x9A, 0x79, 0xF2,
363 0xA9, 0x1F, 0x3E, 0x7C, 0xF8, 0xBD, 0x37, 0x6E, 0xDC, 0xF5, 0xA7, 0x03,
364 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0xCD, 0xD7, 0xE3, 0x8B, 0x5B, 0xB6,
365 0x21, 0x42, 0x84, 0x45, 0x8A, 0x59, 0xB2, 0x29, 0x52, 0xA4, 0x05, 0x0A,
366 0x14, 0x28, 0x50, 0xA0, 0x0D, 0x1A, 0x34, 0x68, 0xD0, 0xED, 0x97, 0x63,
367 0xC6, 0xC1, 0xCF, 0xD3, 0xEB, 0x9B, 0x7B, 0xF6, 0xA1, 0x0F, 0x1E, 0x3C,
368 0x78, 0xF0, 0xAD, 0x17, 0x2E, 0x5C, 0xB8, 0x3D, 0x7A, 0xF4, 0xA5, 0x07,
369 0x0E, 0x1C, 0x38, 0x70, 0xE0, 0x8D, 0x57, 0xAE, 0x11, 0x22, 0x44, 0x88,
370 0x5D, 0xBA, 0x39, 0x72, 0xE4, 0x85, 0x47, 0x8E, 0x51, 0xA2, 0x09, 0x12,
371 0x24, 0x48, 0x90, 0x6D, 0xDA, 0xF9, 0xBF, 0x33, 0x66, 0xCC, 0xD5, 0xE7,
372 0x83, 0x4B, 0x96, 0x61, 0xC2, 0xC9, 0xDF, 0xF3, 0xAB, 0x1B, 0x36, 0x6C,
373 0xD8, 0xFD, 0xB7, 0x23, 0x46, 0x8C, 0x55, 0xAA, 0x19, 0x32, 0x64, 0xC8,
374 0xDD, 0xF7, 0xA3, 0x0B, 0x16, 0x2C, 0x58, 0xB0, 0x2D, 0x5A, 0xB4, 0x25,
375 0x4A, 0x94, 0x65, 0xCA, 0xD9, 0xFF, 0xB3, 0x2B, 0x56, 0xAC, 0x15, 0x2A,
376 0x54, 0xA8, 0x1D, 0x3A, 0x74, 0xE8, 0x9D, 0x77, 0xEE, 0x91, 0x6F, 0xDE,
377 0xF1, 0xAF, 0x13, 0x26, 0x4C, 0x98, 0x7D, 0xFA, 0xB9, 0x3F, 0x7E, 0xFC,
378 0xB5, 0x27, 0x4E, 0x9C, 0x75, 0xEA, 0x99, 0x7F, 0xFE, 0xB1, 0x2F, 0x5E,
379 0xBC, 0x35, 0x6A, 0xD4, 0xE5, 0x87, 0x43, 0x86, 0x41, 0x82, 0x49, 0x92,
380 0x69, 0xD2, 0xE9, 0x9F, 0x73, 0xE6, 0x81, 0x4F, 0x9E, 0x71, 0xE2, 0x89,
381 0x5F, 0xBE, 0x31, 0x62, 0xC4, 0xC5, 0xC7, 0xC3, 0xCB, 0xDB, 0xFB, 0xBB,
382 0x3B, 0x76, 0xEC, 0x95, 0x67, 0xCE, 0xD1, 0xEF, 0x93, 0x6B, 0xD6, 0xE1,
383 0x8F, 0x53, 0xA6, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x4D,
384 0x9A, 0x79, 0xF2, 0xA9, 0x1F, 0x3E, 0x7C, 0xF8, 0xBD, 0x37, 0x6E, 0xDC,
385 0xF5, 0xA7, 0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0xCD, 0xD7, 0xE3,
386 0x8B, 0x5B, 0xB6, 0x21, 0x42, 0x84, 0x45, 0x8A, 0x59, 0xB2, 0x29, 0x52,
387 0xA4, 0x05, 0x0A, 0x14, 0x28, 0x50, 0xA0, 0x0D, 0x1A, 0x34, 0x68, 0xD0,
388 0xED, 0x97, 0x63, 0xC6, 0xC1, 0xCF, 0xD3, 0xEB, 0x9B, 0x7B, 0xF6, 0xA1,
389 0x0F, 0x1E, 0x3C, 0x78, 0xF0, 0xAD, 0x17, 0x2E, 0x5C, 0xB8, 0x3D, 0x7A,
390 0xF4, 0xA5, 0x07, 0x0E, 0x1C, 0x38, 0x70, 0xE0, 0x8D, 0x57, 0xAE, 0x11,
391 0x22, 0x44, 0x88, 0x5D, 0xBA, 0x39, 0x72, 0xE4, 0x85, 0x47, 0x8E, 0x51,
392 0xA2, 0x09, 0x12, 0x24, 0x48, 0x90, 0x6D, 0xDA, 0xF9, 0xBF, 0x33, 0x66,
393 0xCC, 0xD5, 0xE7, 0x83, 0x4B, 0x96, 0x61, 0xC2, 0xC9, 0xDF, 0xF3, 0xAB,
394 0x1B, 0x36, 0x6C, 0xD8, 0xFD, 0xB7, 0x23, 0x46, 0x8C, 0x55, 0xAA, 0x19,
395 0x32, 0x64, 0xC8, 0xDD, 0xF7, 0xA3, 0x0B, 0x16, 0x2C, 0x58, 0xB0, 0x2D,
396 0x5A, 0xB4, 0x25, 0x4A, 0x94, 0x65, 0xCA, 0xD9, 0xFF, 0xB3, 0x2B, 0x56,
397 0xAC, 0x15, 0x2A, 0x54, 0xA8, 0x1D, 0x3A, 0x74, 0xE8, 0x9D, 0x77, 0xEE,
398 0x91, 0x6F, 0xDE, 0xF1, 0xAF, 0x13, 0x26, 0x4C, 0x98, 0x7D, 0xFA, 0xB9,
399 0x3F, 0x7E, 0xFC, 0xB5, 0x27, 0x4E, 0x9C, 0x75, 0xEA, 0x99, 0x7F, 0xFE,
400 0xB1, 0x2F, 0x5E, 0xBC, 0x35, 0x6A, 0xD4, 0xE5, 0x87, 0x43, 0x86, 0x41,
401 0x82, 0x49, 0x92, 0x69, 0xD2, 0xE9, 0x9F, 0x73, 0xE6, 0x81, 0x4F, 0x9E,
402 0x71, 0xE2, 0x89, 0x5F, 0xBE, 0x31, 0x62, 0xC4, 0xC5, 0xC7, 0xC3, 0xCB
403};
404
405
406/* The table constants are indices of
407 * S-box entries, preprocessed through q0 and q1. */
408static byte calc_sb_tbl[512] = {
409 0xA9, 0x75, 0x67, 0xF3, 0xB3, 0xC6, 0xE8, 0xF4,
410 0x04, 0xDB, 0xFD, 0x7B, 0xA3, 0xFB, 0x76, 0xC8,
411 0x9A, 0x4A, 0x92, 0xD3, 0x80, 0xE6, 0x78, 0x6B,
412 0xE4, 0x45, 0xDD, 0x7D, 0xD1, 0xE8, 0x38, 0x4B,
413 0x0D, 0xD6, 0xC6, 0x32, 0x35, 0xD8, 0x98, 0xFD,
414 0x18, 0x37, 0xF7, 0x71, 0xEC, 0xF1, 0x6C, 0xE1,
415 0x43, 0x30, 0x75, 0x0F, 0x37, 0xF8, 0x26, 0x1B,
416 0xFA, 0x87, 0x13, 0xFA, 0x94, 0x06, 0x48, 0x3F,
417 0xF2, 0x5E, 0xD0, 0xBA, 0x8B, 0xAE, 0x30, 0x5B,
418 0x84, 0x8A, 0x54, 0x00, 0xDF, 0xBC, 0x23, 0x9D,
419 0x19, 0x6D, 0x5B, 0xC1, 0x3D, 0xB1, 0x59, 0x0E,
420 0xF3, 0x80, 0xAE, 0x5D, 0xA2, 0xD2, 0x82, 0xD5,
421 0x63, 0xA0, 0x01, 0x84, 0x83, 0x07, 0x2E, 0x14,
422 0xD9, 0xB5, 0x51, 0x90, 0x9B, 0x2C, 0x7C, 0xA3,
423 0xA6, 0xB2, 0xEB, 0x73, 0xA5, 0x4C, 0xBE, 0x54,
424 0x16, 0x92, 0x0C, 0x74, 0xE3, 0x36, 0x61, 0x51,
425 0xC0, 0x38, 0x8C, 0xB0, 0x3A, 0xBD, 0xF5, 0x5A,
426 0x73, 0xFC, 0x2C, 0x60, 0x25, 0x62, 0x0B, 0x96,
427 0xBB, 0x6C, 0x4E, 0x42, 0x89, 0xF7, 0x6B, 0x10,
428 0x53, 0x7C, 0x6A, 0x28, 0xB4, 0x27, 0xF1, 0x8C,
429 0xE1, 0x13, 0xE6, 0x95, 0xBD, 0x9C, 0x45, 0xC7,
430 0xE2, 0x24, 0xF4, 0x46, 0xB6, 0x3B, 0x66, 0x70,
431 0xCC, 0xCA, 0x95, 0xE3, 0x03, 0x85, 0x56, 0xCB,
432 0xD4, 0x11, 0x1C, 0xD0, 0x1E, 0x93, 0xD7, 0xB8,
433 0xFB, 0xA6, 0xC3, 0x83, 0x8E, 0x20, 0xB5, 0xFF,
434 0xE9, 0x9F, 0xCF, 0x77, 0xBF, 0xC3, 0xBA, 0xCC,
435 0xEA, 0x03, 0x77, 0x6F, 0x39, 0x08, 0xAF, 0xBF,
436 0x33, 0x40, 0xC9, 0xE7, 0x62, 0x2B, 0x71, 0xE2,
437 0x81, 0x79, 0x79, 0x0C, 0x09, 0xAA, 0xAD, 0x82,
438 0x24, 0x41, 0xCD, 0x3A, 0xF9, 0xEA, 0xD8, 0xB9,
439 0xE5, 0xE4, 0xC5, 0x9A, 0xB9, 0xA4, 0x4D, 0x97,
440 0x44, 0x7E, 0x08, 0xDA, 0x86, 0x7A, 0xE7, 0x17,
441 0xA1, 0x66, 0x1D, 0x94, 0xAA, 0xA1, 0xED, 0x1D,
442 0x06, 0x3D, 0x70, 0xF0, 0xB2, 0xDE, 0xD2, 0xB3,
443 0x41, 0x0B, 0x7B, 0x72, 0xA0, 0xA7, 0x11, 0x1C,
444 0x31, 0xEF, 0xC2, 0xD1, 0x27, 0x53, 0x90, 0x3E,
445 0x20, 0x8F, 0xF6, 0x33, 0x60, 0x26, 0xFF, 0x5F,
446 0x96, 0xEC, 0x5C, 0x76, 0xB1, 0x2A, 0xAB, 0x49,
447 0x9E, 0x81, 0x9C, 0x88, 0x52, 0xEE, 0x1B, 0x21,
448 0x5F, 0xC4, 0x93, 0x1A, 0x0A, 0xEB, 0xEF, 0xD9,
449 0x91, 0xC5, 0x85, 0x39, 0x49, 0x99, 0xEE, 0xCD,
450 0x2D, 0xAD, 0x4F, 0x31, 0x8F, 0x8B, 0x3B, 0x01,
451 0x47, 0x18, 0x87, 0x23, 0x6D, 0xDD, 0x46, 0x1F,
452 0xD6, 0x4E, 0x3E, 0x2D, 0x69, 0xF9, 0x64, 0x48,
453 0x2A, 0x4F, 0xCE, 0xF2, 0xCB, 0x65, 0x2F, 0x8E,
454 0xFC, 0x78, 0x97, 0x5C, 0x05, 0x58, 0x7A, 0x19,
455 0xAC, 0x8D, 0x7F, 0xE5, 0xD5, 0x98, 0x1A, 0x57,
456 0x4B, 0x67, 0x0E, 0x7F, 0xA7, 0x05, 0x5A, 0x64,
457 0x28, 0xAF, 0x14, 0x63, 0x3F, 0xB6, 0x29, 0xFE,
458 0x88, 0xF5, 0x3C, 0xB7, 0x4C, 0x3C, 0x02, 0xA5,
459 0xB8, 0xCE, 0xDA, 0xE9, 0xB0, 0x68, 0x17, 0x44,
460 0x55, 0xE0, 0x1F, 0x4D, 0x8A, 0x43, 0x7D, 0x69,
461 0x57, 0x29, 0xC7, 0x2E, 0x8D, 0xAC, 0x74, 0x15,
462 0xB7, 0x59, 0xC4, 0xA8, 0x9F, 0x0A, 0x72, 0x9E,
463 0x7E, 0x6E, 0x15, 0x47, 0x22, 0xDF, 0x12, 0x34,
464 0x58, 0x35, 0x07, 0x6A, 0x99, 0xCF, 0x34, 0xDC,
465 0x6E, 0x22, 0x50, 0xC9, 0xDE, 0xC0, 0x68, 0x9B,
466 0x65, 0x89, 0xBC, 0xD4, 0xDB, 0xED, 0xF8, 0xAB,
467 0xC8, 0x12, 0xA8, 0xA2, 0x2B, 0x0D, 0x40, 0x52,
468 0xDC, 0xBB, 0xFE, 0x02, 0x32, 0x2F, 0xA4, 0xA9,
469 0xCA, 0xD7, 0x10, 0x61, 0x21, 0x1E, 0xF0, 0xB4,
470 0xD3, 0x50, 0x5D, 0x04, 0x0F, 0xF6, 0x00, 0xC2,
471 0x6F, 0x16, 0x9D, 0x25, 0x36, 0x86, 0x42, 0x56,
472 0x4A, 0x55, 0x5E, 0x09, 0xC1, 0xBE, 0xE0, 0x91
473};
474/* Macro to perform one column of the RS matrix multiplication. The
475 * parameters a, b, c, and d are the four bytes of output; i is the index
476 * of the key bytes, and w, x, y, and z, are the column of constants from
477 * the RS matrix, preprocessed through the poly_to_exp table. */
478
479#define CALC_S(a, b, c, d, i, w, x, y, z) \
480 if (key[i]) { \
481 tmp = poly_to_exp[key[i] - 1]; \
482 (a) ^= exp_to_poly[tmp + (w)]; \
483 (b) ^= exp_to_poly[tmp + (x)]; \
484 (c) ^= exp_to_poly[tmp + (y)]; \
485 (d) ^= exp_to_poly[tmp + (z)]; \
486 }
487
488/* Macros to calculate the key-dependent S-boxes for a 128-bit key using
489 * the S vector from CALC_S. CALC_SB_2 computes a single entry in all
490 * four S-boxes, where i is the index of the entry to compute, and a and b
491 * are the index numbers preprocessed through the q0 and q1 tables
492 * respectively. CALC_SB is simply a convenience to make the code shorter;
493 * it calls CALC_SB_2 four times with consecutive indices from i to i+3,
494 * using the remaining parameters two by two. */
495
496#define CALC_SB_2(i, a, b) \
497 ctx->s[0][i] = mds[0][q0[(a) ^ sa] ^ se]; \
498 ctx->s[1][i] = mds[1][q0[(b) ^ sb] ^ sf]; \
499 ctx->s[2][i] = mds[2][q1[(a) ^ sc] ^ sg]; \
500 ctx->s[3][i] = mds[3][q1[(b) ^ sd] ^ sh]
501
502#define CALC_SB(i, a, b, c, d, e, f, g, h) \
503 CALC_SB_2 (i, a, b); CALC_SB_2 ((i)+1, c, d); \
504 CALC_SB_2 ((i)+2, e, f); CALC_SB_2 ((i)+3, g, h)
505
506/* Macros exactly like CALC_SB and CALC_SB_2, but for 256-bit keys. */
507
508#define CALC_SB256_2(i, a, b) \
509 ctx->s[0][i] = mds[0][q0[q0[q1[(b) ^ sa] ^ se] ^ si] ^ sm]; \
510 ctx->s[1][i] = mds[1][q0[q1[q1[(a) ^ sb] ^ sf] ^ sj] ^ sn]; \
511 ctx->s[2][i] = mds[2][q1[q0[q0[(a) ^ sc] ^ sg] ^ sk] ^ so]; \
512 ctx->s[3][i] = mds[3][q1[q1[q0[(b) ^ sd] ^ sh] ^ sl] ^ sp];
513
514#define CALC_SB256(i, a, b, c, d, e, f, g, h) \
515 CALC_SB256_2 (i, a, b); CALC_SB256_2 ((i)+1, c, d); \
516 CALC_SB256_2 ((i)+2, e, f); CALC_SB256_2 ((i)+3, g, h)
517
518/* Macros to calculate the whitening and round subkeys. CALC_K_2 computes the
519 * last two stages of the h() function for a given index (either 2i or 2i+1).
520 * a, b, c, and d are the four bytes going into the last two stages. For
521 * 128-bit keys, this is the entire h() function and a and c are the index
522 * preprocessed through q0 and q1 respectively; for longer keys they are the
523 * output of previous stages. j is the index of the first key byte to use.
524 * CALC_K computes a pair of subkeys for 128-bit Twofish, by calling CALC_K_2
525 * twice, doing the Psuedo-Hadamard Transform, and doing the necessary
526 * rotations. Its parameters are: a, the array to write the results into,
527 * j, the index of the first output entry, k and l, the preprocessed indices
528 * for index 2i, and m and n, the preprocessed indices for index 2i+1.
529 * CALC_K256_2 expands CALC_K_2 to handle 256-bit keys, by doing two
530 * additional lookup-and-XOR stages. The parameters a and b are the index
531 * preprocessed through q0 and q1 respectively; j is the index of the first
532 * key byte to use. CALC_K256 is identical to CALC_K but for using the
533 * CALC_K256_2 macro instead of CALC_K_2. */
534
535#define CALC_K_2(a, b, c, d, j) \
536 mds[0][q0[a ^ key[(j) + 8]] ^ key[j]] \
537 ^ mds[1][q0[b ^ key[(j) + 9]] ^ key[(j) + 1]] \
538 ^ mds[2][q1[c ^ key[(j) + 10]] ^ key[(j) + 2]] \
539 ^ mds[3][q1[d ^ key[(j) + 11]] ^ key[(j) + 3]]
540
541#define CALC_K(a, j, k, l, m, n) \
542 x = CALC_K_2 (k, l, k, l, 0); \
543 y = CALC_K_2 (m, n, m, n, 4); \
544 y = (y << 8) + (y >> 24); \
545 x += y; y += x; ctx->a[j] = x; \
546 ctx->a[(j) + 1] = (y << 9) + (y >> 23)
547
548#define CALC_K256_2(a, b, j) \
549 CALC_K_2 (q0[q1[b ^ key[(j) + 24]] ^ key[(j) + 16]], \
550 q1[q1[a ^ key[(j) + 25]] ^ key[(j) + 17]], \
551 q0[q0[a ^ key[(j) + 26]] ^ key[(j) + 18]], \
552 q1[q0[b ^ key[(j) + 27]] ^ key[(j) + 19]], j)
553
554#define CALC_K256(a, j, k, l, m, n) \
555 x = CALC_K256_2 (k, l, 0); \
556 y = CALC_K256_2 (m, n, 4); \
557 y = (y << 8) + (y >> 24); \
558 x += y; y += x; ctx->a[j] = x; \
559 ctx->a[(j) + 1] = (y << 9) + (y >> 23)
560
561
562
563/* Perform the key setup. Note that this works only with 128- and 256-bit
564 * keys, despite the API that looks like it might support other sizes. */
565
566static gcry_err_code_t
567do_twofish_setkey (TWOFISH_context *ctx, const byte *key, const unsigned keylen)
568{
569 int i, j, k;
570
571 /* Temporaries for CALC_K. */
572 u32 x, y;
573
574 /* The S vector used to key the S-boxes, split up into individual bytes.
575 * 128-bit keys use only sa through sh; 256-bit use all of them. */
576 byte sa = 0, sb = 0, sc = 0, sd = 0, se = 0, sf = 0, sg = 0, sh = 0;
577 byte si = 0, sj = 0, sk = 0, sl = 0, sm = 0, sn = 0, so = 0, sp = 0;
578
579 /* Temporary for CALC_S. */
580 byte tmp;
581
582 /* Flags for self-test. */
583 static int initialized = 0;
584 static const char *selftest_failed=0;
585
586 /* Check key length. */
587 if( ( ( keylen - 16 ) | 16 ) != 16 )
588 return GPG_ERR_INV_KEYLEN;
589
590 /* Do self-test if necessary. */
591 if (!initialized)
592 {
593 initialized = 1;
594 selftest_failed = selftest ();
595 if( selftest_failed )
596 log_error("%s\n", selftest_failed );
597 }
598 if( selftest_failed )
599 return GPG_ERR_SELFTEST_FAILED;
600
601 /* Compute the first two words of the S vector. The magic numbers are
602 * the entries of the RS matrix, preprocessed through poly_to_exp.The
603 * numbers in the comments are the original (polynomial form) matrix
604 * entries. */
605 CALC_S (sa, sb, sc, sd, 0, 0x00, 0x2D, 0x01, 0x2D); /* 01 A4 02 A4 */
606 CALC_S (sa, sb, sc, sd, 1, 0x2D, 0xA4, 0x44, 0x8A); /* A4 56 A1 55 */
607 CALC_S (sa, sb, sc, sd, 2, 0x8A, 0xD5, 0xBF, 0xD1); /* 55 82 FC 87 */
608 CALC_S (sa, sb, sc, sd, 3, 0xD1, 0x7F, 0x3D, 0x99); /* 87 F3 C1 5A */
609 CALC_S (sa, sb, sc, sd, 4, 0x99, 0x46, 0x66, 0x96); /* 5A 1E 47 58 */
610 CALC_S (sa, sb, sc, sd, 5, 0x96, 0x3C, 0x5B, 0xED); /* 58 C6 AE DB */
611 CALC_S (sa, sb, sc, sd, 6, 0xED, 0x37, 0x4F, 0xE0); /* DB 68 3D 9E */
612 CALC_S (sa, sb, sc, sd, 7, 0xE0, 0xD0, 0x8C, 0x17); /* 9E E5 19 03 */
613 CALC_S (se, sf, sg, sh, 8, 0x00, 0x2D, 0x01, 0x2D); /* 01 A4 02 A4 */
614 CALC_S (se, sf, sg, sh, 9, 0x2D, 0xA4, 0x44, 0x8A); /* A4 56 A1 55 */
615 CALC_S (se, sf, sg, sh, 10, 0x8A, 0xD5, 0xBF, 0xD1); /* 55 82 FC 87 */
616 CALC_S (se, sf, sg, sh, 11, 0xD1, 0x7F, 0x3D, 0x99); /* 87 F3 C1 5A */
617 CALC_S (se, sf, sg, sh, 12, 0x99, 0x46, 0x66, 0x96); /* 5A 1E 47 58 */
618 CALC_S (se, sf, sg, sh, 13, 0x96, 0x3C, 0x5B, 0xED); /* 58 C6 AE DB */
619 CALC_S (se, sf, sg, sh, 14, 0xED, 0x37, 0x4F, 0xE0); /* DB 68 3D 9E */
620 CALC_S (se, sf, sg, sh, 15, 0xE0, 0xD0, 0x8C, 0x17); /* 9E E5 19 03 */
621
622 if (keylen == 32) /* 256-bit key */
623 {
624 /* Calculate the remaining two words of the S vector */
625 CALC_S (si, sj, sk, sl, 16, 0x00, 0x2D, 0x01, 0x2D); /* 01 A4 02 A4 */
626 CALC_S (si, sj, sk, sl, 17, 0x2D, 0xA4, 0x44, 0x8A); /* A4 56 A1 55 */
627 CALC_S (si, sj, sk, sl, 18, 0x8A, 0xD5, 0xBF, 0xD1); /* 55 82 FC 87 */
628 CALC_S (si, sj, sk, sl, 19, 0xD1, 0x7F, 0x3D, 0x99); /* 87 F3 C1 5A */
629 CALC_S (si, sj, sk, sl, 20, 0x99, 0x46, 0x66, 0x96); /* 5A 1E 47 58 */
630 CALC_S (si, sj, sk, sl, 21, 0x96, 0x3C, 0x5B, 0xED); /* 58 C6 AE DB */
631 CALC_S (si, sj, sk, sl, 22, 0xED, 0x37, 0x4F, 0xE0); /* DB 68 3D 9E */
632 CALC_S (si, sj, sk, sl, 23, 0xE0, 0xD0, 0x8C, 0x17); /* 9E E5 19 03 */
633 CALC_S (sm, sn, so, sp, 24, 0x00, 0x2D, 0x01, 0x2D); /* 01 A4 02 A4 */
634 CALC_S (sm, sn, so, sp, 25, 0x2D, 0xA4, 0x44, 0x8A); /* A4 56 A1 55 */
635 CALC_S (sm, sn, so, sp, 26, 0x8A, 0xD5, 0xBF, 0xD1); /* 55 82 FC 87 */
636 CALC_S (sm, sn, so, sp, 27, 0xD1, 0x7F, 0x3D, 0x99); /* 87 F3 C1 5A */
637 CALC_S (sm, sn, so, sp, 28, 0x99, 0x46, 0x66, 0x96); /* 5A 1E 47 58 */
638 CALC_S (sm, sn, so, sp, 29, 0x96, 0x3C, 0x5B, 0xED); /* 58 C6 AE DB */
639 CALC_S (sm, sn, so, sp, 30, 0xED, 0x37, 0x4F, 0xE0); /* DB 68 3D 9E */
640 CALC_S (sm, sn, so, sp, 31, 0xE0, 0xD0, 0x8C, 0x17); /* 9E E5 19 03 */
641
642 /* Compute the S-boxes. */
643 for(i=j=0,k=1; i < 256; i++, j += 2, k += 2 )
644 {
645 CALC_SB256_2( i, calc_sb_tbl[j], calc_sb_tbl[k] );
646 }
647
648 /* Calculate whitening and round subkeys. The constants are
649 * indices of subkeys, preprocessed through q0 and q1. */
650 CALC_K256 (w, 0, 0xA9, 0x75, 0x67, 0xF3);
651 CALC_K256 (w, 2, 0xB3, 0xC6, 0xE8, 0xF4);
652 CALC_K256 (w, 4, 0x04, 0xDB, 0xFD, 0x7B);
653 CALC_K256 (w, 6, 0xA3, 0xFB, 0x76, 0xC8);
654 CALC_K256 (k, 0, 0x9A, 0x4A, 0x92, 0xD3);
655 CALC_K256 (k, 2, 0x80, 0xE6, 0x78, 0x6B);
656 CALC_K256 (k, 4, 0xE4, 0x45, 0xDD, 0x7D);
657 CALC_K256 (k, 6, 0xD1, 0xE8, 0x38, 0x4B);
658 CALC_K256 (k, 8, 0x0D, 0xD6, 0xC6, 0x32);
659 CALC_K256 (k, 10, 0x35, 0xD8, 0x98, 0xFD);
660 CALC_K256 (k, 12, 0x18, 0x37, 0xF7, 0x71);
661 CALC_K256 (k, 14, 0xEC, 0xF1, 0x6C, 0xE1);
662 CALC_K256 (k, 16, 0x43, 0x30, 0x75, 0x0F);
663 CALC_K256 (k, 18, 0x37, 0xF8, 0x26, 0x1B);
664 CALC_K256 (k, 20, 0xFA, 0x87, 0x13, 0xFA);
665 CALC_K256 (k, 22, 0x94, 0x06, 0x48, 0x3F);
666 CALC_K256 (k, 24, 0xF2, 0x5E, 0xD0, 0xBA);
667 CALC_K256 (k, 26, 0x8B, 0xAE, 0x30, 0x5B);
668 CALC_K256 (k, 28, 0x84, 0x8A, 0x54, 0x00);
669 CALC_K256 (k, 30, 0xDF, 0xBC, 0x23, 0x9D);
670 }
671 else
672 {
673 /* Compute the S-boxes. */
674 for(i=j=0,k=1; i < 256; i++, j += 2, k += 2 )
675 {
676 CALC_SB_2( i, calc_sb_tbl[j], calc_sb_tbl[k] );
677 }
678
679 /* Calculate whitening and round subkeys. The constants are
680 * indices of subkeys, preprocessed through q0 and q1. */
681 CALC_K (w, 0, 0xA9, 0x75, 0x67, 0xF3);
682 CALC_K (w, 2, 0xB3, 0xC6, 0xE8, 0xF4);
683 CALC_K (w, 4, 0x04, 0xDB, 0xFD, 0x7B);
684 CALC_K (w, 6, 0xA3, 0xFB, 0x76, 0xC8);
685 CALC_K (k, 0, 0x9A, 0x4A, 0x92, 0xD3);
686 CALC_K (k, 2, 0x80, 0xE6, 0x78, 0x6B);
687 CALC_K (k, 4, 0xE4, 0x45, 0xDD, 0x7D);
688 CALC_K (k, 6, 0xD1, 0xE8, 0x38, 0x4B);
689 CALC_K (k, 8, 0x0D, 0xD6, 0xC6, 0x32);
690 CALC_K (k, 10, 0x35, 0xD8, 0x98, 0xFD);
691 CALC_K (k, 12, 0x18, 0x37, 0xF7, 0x71);
692 CALC_K (k, 14, 0xEC, 0xF1, 0x6C, 0xE1);
693 CALC_K (k, 16, 0x43, 0x30, 0x75, 0x0F);
694 CALC_K (k, 18, 0x37, 0xF8, 0x26, 0x1B);
695 CALC_K (k, 20, 0xFA, 0x87, 0x13, 0xFA);
696 CALC_K (k, 22, 0x94, 0x06, 0x48, 0x3F);
697 CALC_K (k, 24, 0xF2, 0x5E, 0xD0, 0xBA);
698 CALC_K (k, 26, 0x8B, 0xAE, 0x30, 0x5B);
699 CALC_K (k, 28, 0x84, 0x8A, 0x54, 0x00);
700 CALC_K (k, 30, 0xDF, 0xBC, 0x23, 0x9D);
701 }
702
703 return 0;
704}
705
706static gcry_err_code_t
707twofish_setkey (void *context, const byte *key, unsigned int keylen)
708{
709 TWOFISH_context *ctx = context;
710 int rc = do_twofish_setkey (ctx, key, keylen);
711 _gcry_burn_stack (23+6*sizeof(void*));
712 return rc;
713}
714
715
716
717/* Macros to compute the g() function in the encryption and decryption
718 * rounds. G1 is the straight g() function; G2 includes the 8-bit
719 * rotation for the high 32-bit word. */
720
721#define G1(a) \
722 (ctx->s[0][(a) & 0xFF]) ^ (ctx->s[1][((a) >> 8) & 0xFF]) \
723 ^ (ctx->s[2][((a) >> 16) & 0xFF]) ^ (ctx->s[3][(a) >> 24])
724
725#define G2(b) \
726 (ctx->s[1][(b) & 0xFF]) ^ (ctx->s[2][((b) >> 8) & 0xFF]) \
727 ^ (ctx->s[3][((b) >> 16) & 0xFF]) ^ (ctx->s[0][(b) >> 24])
728
729/* Encryption and decryption Feistel rounds. Each one calls the two g()
730 * macros, does the PHT, and performs the XOR and the appropriate bit
731 * rotations. The parameters are the round number (used to select subkeys),
732 * and the four 32-bit chunks of the text. */
733
734#define ENCROUND(n, a, b, c, d) \
735 x = G1 (a); y = G2 (b); \
736 x += y; y += x + ctx->k[2 * (n) + 1]; \
737 (c) ^= x + ctx->k[2 * (n)]; \
738 (c) = ((c) >> 1) + ((c) << 31); \
739 (d) = (((d) << 1)+((d) >> 31)) ^ y
740
741#define DECROUND(n, a, b, c, d) \
742 x = G1 (a); y = G2 (b); \
743 x += y; y += x; \
744 (d) ^= y + ctx->k[2 * (n) + 1]; \
745 (d) = ((d) >> 1) + ((d) << 31); \
746 (c) = (((c) << 1)+((c) >> 31)); \
747 (c) ^= (x + ctx->k[2 * (n)])
748
749/* Encryption and decryption cycles; each one is simply two Feistel rounds
750 * with the 32-bit chunks re-ordered to simulate the "swap" */
751
752#define ENCCYCLE(n) \
753 ENCROUND (2 * (n), a, b, c, d); \
754 ENCROUND (2 * (n) + 1, c, d, a, b)
755
756#define DECCYCLE(n) \
757 DECROUND (2 * (n) + 1, c, d, a, b); \
758 DECROUND (2 * (n), a, b, c, d)
759
760/* Macros to convert the input and output bytes into 32-bit words,
761 * and simultaneously perform the whitening step. INPACK packs word
762 * number n into the variable named by x, using whitening subkey number m.
763 * OUTUNPACK unpacks word number n from the variable named by x, using
764 * whitening subkey number m. */
765
766#define INPACK(n, x, m) \
767 x = in[4 * (n)] ^ (in[4 * (n) + 1] << 8) \
768 ^ (in[4 * (n) + 2] << 16) ^ (in[4 * (n) + 3] << 24) ^ ctx->w[m]
769
770#define OUTUNPACK(n, x, m) \
771 x ^= ctx->w[m]; \
772 out[4 * (n)] = x; out[4 * (n) + 1] = x >> 8; \
773 out[4 * (n) + 2] = x >> 16; out[4 * (n) + 3] = x >> 24
774
775/* Encrypt one block. in and out may be the same. */
776
777static void
778do_twofish_encrypt (const TWOFISH_context *ctx, byte *out, const byte *in)
779{
780 /* The four 32-bit chunks of the text. */
781 u32 a, b, c, d;
782
783 /* Temporaries used by the round function. */
784 u32 x, y;
785
786 /* Input whitening and packing. */
787 INPACK (0, a, 0);
788 INPACK (1, b, 1);
789 INPACK (2, c, 2);
790 INPACK (3, d, 3);
791
792 /* Encryption Feistel cycles. */
793 ENCCYCLE (0);
794 ENCCYCLE (1);
795 ENCCYCLE (2);
796 ENCCYCLE (3);
797 ENCCYCLE (4);
798 ENCCYCLE (5);
799 ENCCYCLE (6);
800 ENCCYCLE (7);
801
802 /* Output whitening and unpacking. */
803 OUTUNPACK (0, c, 4);
804 OUTUNPACK (1, d, 5);
805 OUTUNPACK (2, a, 6);
806 OUTUNPACK (3, b, 7);
807}
808
809static void
810twofish_encrypt (void *context, byte *out, const byte *in)
811{
812 TWOFISH_context *ctx = context;
813 do_twofish_encrypt (ctx, out, in);
814 _gcry_burn_stack (24+3*sizeof (void*));
815}
816
817
818/* Decrypt one block. in and out may be the same. */
819
820static void
821do_twofish_decrypt (const TWOFISH_context *ctx, byte *out, const byte *in)
822{
823 /* The four 32-bit chunks of the text. */
824 u32 a, b, c, d;
825
826 /* Temporaries used by the round function. */
827 u32 x, y;
828
829 /* Input whitening and packing. */
830 INPACK (0, c, 4);
831 INPACK (1, d, 5);
832 INPACK (2, a, 6);
833 INPACK (3, b, 7);
834
835 /* Encryption Feistel cycles. */
836 DECCYCLE (7);
837 DECCYCLE (6);
838 DECCYCLE (5);
839 DECCYCLE (4);
840 DECCYCLE (3);
841 DECCYCLE (2);
842 DECCYCLE (1);
843 DECCYCLE (0);
844
845 /* Output whitening and unpacking. */
846 OUTUNPACK (0, a, 0);
847 OUTUNPACK (1, b, 1);
848 OUTUNPACK (2, c, 2);
849 OUTUNPACK (3, d, 3);
850}
851
852static void
853twofish_decrypt (void *context, byte *out, const byte *in)
854{
855 TWOFISH_context *ctx = context;
856
857 do_twofish_decrypt (ctx, out, in);
858 _gcry_burn_stack (24+3*sizeof (void*));
859}
860
861
862/* Test a single encryption and decryption with each key size. */
863
864static const char*
865selftest (void)
866{
867 TWOFISH_context ctx; /* Expanded key. */
868 byte scratch[16];/* Encryption/decryption result buffer. */
869
870 /* Test vectors for single encryption/decryption. Note that I am using
871 * the vectors from the Twofish paper's "known answer test", I=3 for
872 * 128-bit and I=4 for 256-bit, instead of the all-0 vectors from the
873 * "intermediate value test", because an all-0 key would trigger all the
874 * special cases in the RS matrix multiply, leaving the math untested. */
875 static byte plaintext[16] = {
876 0xD4, 0x91, 0xDB, 0x16, 0xE7, 0xB1, 0xC3, 0x9E,
877 0x86, 0xCB, 0x08, 0x6B, 0x78, 0x9F, 0x54, 0x19
878 };
879 static byte key[16] = {
880 0x9F, 0x58, 0x9F, 0x5C, 0xF6, 0x12, 0x2C, 0x32,
881 0xB6, 0xBF, 0xEC, 0x2F, 0x2A, 0xE8, 0xC3, 0x5A
882 };
883 static const byte ciphertext[16] = {
884 0x01, 0x9F, 0x98, 0x09, 0xDE, 0x17, 0x11, 0x85,
885 0x8F, 0xAA, 0xC3, 0xA3, 0xBA, 0x20, 0xFB, 0xC3
886 };
887 static byte plaintext_256[16] = {
888 0x90, 0xAF, 0xE9, 0x1B, 0xB2, 0x88, 0x54, 0x4F,
889 0x2C, 0x32, 0xDC, 0x23, 0x9B, 0x26, 0x35, 0xE6
890 };
891 static byte key_256[32] = {
892 0xD4, 0x3B, 0xB7, 0x55, 0x6E, 0xA3, 0x2E, 0x46,
893 0xF2, 0xA2, 0x82, 0xB7, 0xD4, 0x5B, 0x4E, 0x0D,
894 0x57, 0xFF, 0x73, 0x9D, 0x4D, 0xC9, 0x2C, 0x1B,
895 0xD7, 0xFC, 0x01, 0x70, 0x0C, 0xC8, 0x21, 0x6F
896 };
897 static const byte ciphertext_256[16] = {
898 0x6C, 0xB4, 0x56, 0x1C, 0x40, 0xBF, 0x0A, 0x97,
899 0x05, 0x93, 0x1C, 0xB6, 0xD4, 0x08, 0xE7, 0xFA
900 };
901
902 twofish_setkey (&ctx, key, sizeof(key));
903 twofish_encrypt (&ctx, scratch, plaintext);
904 if (memcmp (scratch, ciphertext, sizeof (ciphertext)))
905 return "Twofish-128 test encryption failed.";
906 twofish_decrypt (&ctx, scratch, scratch);
907 if (memcmp (scratch, plaintext, sizeof (plaintext)))
908 return "Twofish-128 test decryption failed.";
909
910 twofish_setkey (&ctx, key_256, sizeof(key_256));
911 twofish_encrypt (&ctx, scratch, plaintext_256);
912 if (memcmp (scratch, ciphertext_256, sizeof (ciphertext_256)))
913 return "Twofish-256 test encryption failed.";
914 twofish_decrypt (&ctx, scratch, scratch);
915 if (memcmp (scratch, plaintext_256, sizeof (plaintext_256)))
916 return "Twofish-256 test decryption failed.";
917
918 return NULL;
919}
920
921 /* More complete test program.This does 1000 encryptions and decryptions
922 * with each of 250 128-bit keys and 2000 encryptions and decryptions with
923 * each of 125 256-bit keys, using a feedback scheme similar to a Feistel
924 * cipher, so as to be sure of testing all the table entries pretty
925 * thoroughly.We keep changing the keys so as to get a more meaningful
926 * performance number, since the key setup is non-trivial for Twofish. */
927
928#ifdef TEST
929
930#include <stdio.h>
931#include <string.h>
932#include <time.h>
933
934int
935main()
936{
937 TWOFISH_context ctx; /* Expanded key. */
938 int i, j; /* Loop counters. */
939
940 const char *encrypt_msg; /* Message to print regarding encryption test;
941 * the printf is done outside the loop to avoid
942 * stuffing up the timing. */
943 clock_t timer; /* For computing elapsed time. */
944
945 /* Test buffer. */
946 byte buffer[4][16] = {
947 {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
948 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF},
949 {0x0F, 0x1E, 0x2D, 0x3C, 0x4B, 0x5A, 0x69, 0x78,
950 0x87, 0x96, 0xA5, 0xB4, 0xC3, 0xD2 ,0xE1, 0xF0},
951 {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
952 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54 ,0x32, 0x10},
953 {0x01, 0x23, 0x45, 0x67, 0x76, 0x54 ,0x32, 0x10,
954 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98}
955 };
956
957 /* Expected outputs for the million-operation test */
958 static const byte test_encrypt[4][16] = {
959 {0xC8, 0x23, 0xB8, 0xB7, 0x6B, 0xFE, 0x91, 0x13,
960 0x2F, 0xA7, 0x5E, 0xE6, 0x94, 0x77, 0x6F, 0x6B},
961 {0x90, 0x36, 0xD8, 0x29, 0xD5, 0x96, 0xC2, 0x8E,
962 0xE4, 0xFF, 0x76, 0xBC, 0xE5, 0x77, 0x88, 0x27},
963 {0xB8, 0x78, 0x69, 0xAF, 0x42, 0x8B, 0x48, 0x64,
964 0xF7, 0xE9, 0xF3, 0x9C, 0x42, 0x18, 0x7B, 0x73},
965 {0x7A, 0x88, 0xFB, 0xEB, 0x90, 0xA4, 0xB4, 0xA8,
966 0x43, 0xA3, 0x1D, 0xF1, 0x26, 0xC4, 0x53, 0x57}
967 };
968 static const byte test_decrypt[4][16] = {
969 {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
970 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF},
971 {0x0F, 0x1E, 0x2D, 0x3C, 0x4B, 0x5A, 0x69, 0x78,
972 0x87, 0x96, 0xA5, 0xB4, 0xC3, 0xD2 ,0xE1, 0xF0},
973 {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
974 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54 ,0x32, 0x10},
975 {0x01, 0x23, 0x45, 0x67, 0x76, 0x54 ,0x32, 0x10,
976 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98}
977 };
978
979 /* Start the timer ticking. */
980 timer = clock ();
981
982 /* Encryption test. */
983 for (i = 0; i < 125; i++)
984 {
985 twofish_setkey (&ctx, buffer[0], sizeof (buffer[0]));
986 for (j = 0; j < 1000; j++)
987 twofish_encrypt (&ctx, buffer[2], buffer[2]);
988 twofish_setkey (&ctx, buffer[1], sizeof (buffer[1]));
989 for (j = 0; j < 1000; j++)
990 twofish_encrypt (&ctx, buffer[3], buffer[3]);
991 twofish_setkey (&ctx, buffer[2], sizeof (buffer[2])*2);
992 for (j = 0; j < 1000; j++) {
993 twofish_encrypt (&ctx, buffer[0], buffer[0]);
994 twofish_encrypt (&ctx, buffer[1], buffer[1]);
995 }
996 }
997 encrypt_msg = memcmp (buffer, test_encrypt, sizeof (test_encrypt)) ?
998 "encryption failure!\n" : "encryption OK!\n";
999
1000 /* Decryption test. */
1001 for (i = 0; i < 125; i++)
1002 {
1003 twofish_setkey (&ctx, buffer[2], sizeof (buffer[2])*2);
1004 for (j = 0; j < 1000; j++) {
1005 twofish_decrypt (&ctx, buffer[0], buffer[0]);
1006 twofish_decrypt (&ctx, buffer[1], buffer[1]);
1007 }
1008 twofish_setkey (&ctx, buffer[1], sizeof (buffer[1]));
1009 for (j = 0; j < 1000; j++)
1010 twofish_decrypt (&ctx, buffer[3], buffer[3]);
1011 twofish_setkey (&ctx, buffer[0], sizeof (buffer[0]));
1012 for (j = 0; j < 1000; j++)
1013 twofish_decrypt (&ctx, buffer[2], buffer[2]);
1014 }
1015
1016 /* Stop the timer, and print results. */
1017 timer = clock () - timer;
1018 printf (encrypt_msg);
1019 printf (memcmp (buffer, test_decrypt, sizeof (test_decrypt)) ?
1020 "decryption failure!\n" : "decryption OK!\n");
1021 printf ("elapsed time: %.1f s.\n", (float) timer / CLOCKS_PER_SEC);
1022
1023 return 0;
1024}
1025
1026#endif /* TEST */
1027
1028
1029
1030gcry_cipher_spec_t _gcry_cipher_spec_twofish =
1031 {
1032 "TWOFISH", NULL, NULL, 16, 256, sizeof (TWOFISH_context),
1033 twofish_setkey, twofish_encrypt, twofish_decrypt,
1034 };
1035
1036gcry_cipher_spec_t _gcry_cipher_spec_twofish128 =
1037 {
1038 "TWOFISH128", NULL, NULL, 16, 128, sizeof (TWOFISH_context),
1039 twofish_setkey, twofish_encrypt, twofish_decrypt,
1040 };
diff --git a/pwmanager/libcrypt/config.h b/pwmanager/libcrypt/config.h
new file mode 100644
index 0000000..bdb37ad
--- a/dev/null
+++ b/pwmanager/libcrypt/config.h
@@ -0,0 +1,314 @@
1/* config.h. Generated by configure. */
2/* config.h.in. Generated from configure.ac by autoheader. */
3
4
5/* need this, because some autoconf tests rely on this (e.g. stpcpy)
6 * and it should be used for new programs */
7#define _GNU_SOURCE 1
8
9
10/* Define if you don't want the default EGD socket name. For details see
11 cipher/rndegd.c */
12#define EGD_SOCKET_NAME ""
13
14/* The default error source for libgcrypt. */
15#define GPG_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_GCRYPT
16
17/* Define to 1 if you have the `atexit' function. */
18#define HAVE_ATEXIT 1
19
20/* Defined if the mlock() call does not work */
21/* #undef HAVE_BROKEN_MLOCK */
22
23/* Defined if a `byte' is typedef'd */
24/* #undef HAVE_BYTE_TYPEDEF */
25
26/* Define to 1 if you have the `clock_gettime' function. */
27/* #undef HAVE_CLOCK_GETTIME */
28
29/* Define to 1 if you have the declaration of `sys_siglist', and to 0 if you
30 don't. */
31#define HAVE_DECL_SYS_SIGLIST 1
32
33/* defined if the system supports a random device */
34#define HAVE_DEV_RANDOM 1
35
36/* Define to 1 if you have the <dlfcn.h> header file. */
37#define HAVE_DLFCN_H 1
38
39/* Define to 1 if you don't have `vprintf' but do have `_doprnt.' */
40/* #undef HAVE_DOPRNT */
41
42/* defined if we run on some of the PCDOS like systems (DOS, Windoze. OS/2)
43 with special properties like no file modes */
44/* #undef HAVE_DOSISH_SYSTEM */
45
46/* defined if we must run on a stupid file system */
47/* #undef HAVE_DRIVE_LETTERS */
48
49/* Define to 1 if you have the `gethrtime' function. */
50/* #undef HAVE_GETHRTIME */
51
52/* Define to 1 if you have the `getpagesize' function. */
53#define HAVE_GETPAGESIZE 1
54
55/* Define to 1 if you have the `getrusage' function. */
56#define HAVE_GETRUSAGE 1
57
58/* Define to 1 if you have the `gettimeofday' function. */
59#define HAVE_GETTIMEOFDAY 1
60
61/* Define to 1 if you have the <inttypes.h> header file. */
62#define HAVE_INTTYPES_H 1
63
64/* Define to 1 if you have the `nsl' library (-lnsl). */
65#define HAVE_LIBNSL 1
66
67/* Define to 1 if you have the `rt' library (-lrt). */
68/* #undef HAVE_LIBRT */
69
70/* Define to 1 if you have the `memmove' function. */
71#define HAVE_MEMMOVE 1
72
73/* Define to 1 if you have the <memory.h> header file. */
74#define HAVE_MEMORY_H 1
75
76/* Defined if the system supports an mlock() call */
77#define HAVE_MLOCK 1
78
79/* Define to 1 if you have the `mmap' function. */
80#define HAVE_MMAP 1
81
82/* Define to 1 if you have the `raise' function. */
83#define HAVE_RAISE 1
84
85/* Define to 1 if you have the `rand' function. */
86#define HAVE_RAND 1
87
88/* Define to 1 if you have the <stdint.h> header file. */
89#define HAVE_STDINT_H 1
90
91/* Define to 1 if you have the <stdlib.h> header file. */
92#define HAVE_STDLIB_H 1
93
94/* Define to 1 if you have the `stpcpy' function. */
95#define HAVE_STPCPY 1
96
97/* Define to 1 if you have the `strcasecmp' function. */
98#define HAVE_STRCASECMP 1
99
100/* Define to 1 if you have the `strerror' function. */
101#define HAVE_STRERROR 1
102
103/* Define to 1 if you have the `stricmp' function. */
104/* #undef HAVE_STRICMP */
105
106/* Define to 1 if you have the <strings.h> header file. */
107#define HAVE_STRINGS_H 1
108
109/* Define to 1 if you have the <string.h> header file. */
110#define HAVE_STRING_H 1
111
112/* Define to 1 if you have the `strtoul' function. */
113#define HAVE_STRTOUL 1
114
115/* Define to 1 if you have the <sys/capability.h> header file. */
116/* #undef HAVE_SYS_CAPABILITY_H */
117
118/* Define to 1 if you have the <sys/mman.h> header file. */
119/* #undef HAVE_SYS_MMAN_H */
120
121/* Define to 1 if you have the <sys/select.h> header file. */
122#define HAVE_SYS_SELECT_H 1
123
124/* Define to 1 if you have the <sys/stat.h> header file. */
125#define HAVE_SYS_STAT_H 1
126
127/* Define to 1 if you have the <sys/types.h> header file. */
128#define HAVE_SYS_TYPES_H 1
129
130/* Defined if a `u16' is typedef'd */
131/* #undef HAVE_U16_TYPEDEF */
132
133/* Defined if a `u32' is typedef'd */
134/* #undef HAVE_U32_TYPEDEF */
135
136/* Defined if a `ulong' is typedef'd */
137#define HAVE_ULONG_TYPEDEF 1
138
139/* Define to 1 if you have the <unistd.h> header file. */
140#define HAVE_UNISTD_H 1
141
142/* Defined if a `ushort' is typedef'd */
143#define HAVE_USHORT_TYPEDEF 1
144
145/* Define to 1 if you have the `vprintf' function. */
146#define HAVE_VPRINTF 1
147
148/* Define to 1 if you have the `wait4' function. */
149#define HAVE_WAIT4 1
150
151/* Define to 1 if you have the `waitpid' function. */
152#define HAVE_WAITPID 1
153
154/* Defined if this is not a regular release */
155/* #undef IS_DEVELOPMENT_VERSION */
156
157/* Define to use the (obsolete) malloc guarding feature */
158/* #undef M_GUARD */
159
160/* defined to the name of the strong random device */
161#define NAME_OF_DEV_RANDOM "/dev/random"
162
163/* defined to the name of the weaker random device */
164#define NAME_OF_DEV_URANDOM "/dev/urandom"
165
166/* Name of this package */
167#define PACKAGE "libgcrypt"
168
169/* Define to the address where bug reports for this package should be sent. */
170#define PACKAGE_BUGREPORT "bug-libgcrypt@gnupg.org"
171
172/* Define to the full name of this package. */
173#define PACKAGE_NAME "libgcrypt"
174
175/* Define to the full name and version of this package. */
176#define PACKAGE_STRING "libgcrypt 1.2.0"
177
178/* Define to the one symbol short name of this package. */
179#define PACKAGE_TARNAME "libgcrypt"
180
181/* Define to the version of this package. */
182#define PACKAGE_VERSION "1.2.0"
183
184/* A human readable text with the name of the OS */
185#define PRINTABLE_OS_NAME "GNU/Linux"
186
187/* Define as the return type of signal handlers (`int' or `void'). */
188#define RETSIGTYPE void
189
190/* The size of a `unsigned int', as computed by sizeof. */
191#define SIZEOF_UNSIGNED_INT 4
192
193/* The size of a `unsigned long', as computed by sizeof. */
194#define SIZEOF_UNSIGNED_LONG 4
195
196/* The size of a `unsigned long long', as computed by sizeof. */
197#define SIZEOF_UNSIGNED_LONG_LONG 8
198
199/* The size of a `unsigned short', as computed by sizeof. */
200#define SIZEOF_UNSIGNED_SHORT 2
201
202/* Define to 1 if you have the ANSI C header files. */
203#define STDC_HEADERS 1
204
205/* Defined if this module should be included */
206#define USE_AES 1
207
208/* Defined if this module should be included */
209#define USE_ARCFOUR 1
210
211/* Defined if this module should be included */
212#define USE_BLOWFISH 1
213
214/* define if capabilities should be used */
215/* #undef USE_CAPABILITIES */
216
217/* Defined if this module should be included */
218#define USE_CAST5 1
219
220/* Defined if this module should be included */
221#define USE_CRC 1
222
223/* Defined if this module should be included */
224#define USE_DES 1
225
226/* Defined if this module should be included */
227#define USE_DSA 1
228
229/* Defined if this module should be included */
230#define USE_ELGAMAL 1
231
232/* Defined if this module should be included */
233#define USE_MD4 1
234
235/* Defined if this module should be included */
236#define USE_MD5 1
237
238/* set this to limit filenames to the 8.3 format */
239/* #undef USE_ONLY_8DOT3 */
240
241/* Defined if this module should be included */
242#define USE_RFC2268 1
243
244/* Defined if this module should be included */
245#define USE_RMD160 1
246
247/* Defined if the EGD based RNG should be used. */
248#define USE_RNDEGD 0
249
250/* Defined if the /dev/random based RNG should be used. */
251#define USE_RNDLINUX 1
252
253/* Defined if the default Unix RNG should be used. */
254#define USE_RNDUNIX 0
255
256/* Defined if the Windows specific RNG should be used. */
257#define USE_RNDW32 0
258
259/* Defined if this module should be included */
260#define USE_RSA 1
261
262/* Defined if this module should be included */
263#define USE_SERPENT 1
264
265/* Defined if this module should be included */
266#define USE_SHA1 1
267
268/* Defined if this module should be included */
269#define USE_SHA256 1
270
271/* Defined if this module should be included */
272#define USE_SHA512 1
273
274/* Defined if this module should be included */
275#define USE_TIGER 1
276
277/* Defined if this module should be included */
278#define USE_TWOFISH 1
279
280/* Version of this package */
281#define VERSION "1.2.0"
282
283/* Defined if compiled symbols have a leading underscore */
284/* #undef WITH_SYMBOL_UNDERSCORE */
285
286/* Define to 1 if your processor stores words with the most significant byte
287 first (like Motorola and SPARC, unlike Intel and VAX). */
288/* #undef WORDS_BIGENDIAN */
289
290/* To allow the use of Libgcrypt in multithreaded programs we have to use
291 special features from the library. */
292#ifndef _REENTRANT
293# define _REENTRANT 1
294#endif
295
296
297/* Define to empty if `const' does not conform to ANSI C. */
298/* #undef const */
299
300/* Define to `__inline__' or `__inline' if that's what the C compiler
301 calls it, or to nothing if 'inline' is not supported under any name. */
302#ifndef __cplusplus
303/* #undef inline */
304#endif
305
306/* Define to `unsigned' if <sys/types.h> does not define. */
307/* #undef size_t */
308
309/* Define to `int' if unavailable. */
310/* #undef socklen_t */
311
312
313#define _GCRYPT_IN_LIBGCRYPT 1
314
diff --git a/pwmanager/libcrypt/crypt/Manifest b/pwmanager/libcrypt/crypt/Manifest
new file mode 100644
index 0000000..2d003d8
--- a/dev/null
+++ b/pwmanager/libcrypt/crypt/Manifest
@@ -0,0 +1,58 @@
1# Manifest - checksums of the src directory
2# Copyright 2004 Free Software Foundation, Inc.
3#
4# This file is part of Libgcrypt.
5#
6# Libgcrypt is free software; you can redistribute it and/or modify
7# it under the terms of the GNU Lesser general Public License as
8# published by the Free Software Foundation; either version 2.1 of
9# the License, or (at your option) any later version.
10#
11# Libgcrypt is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public
17# License along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19
20# Checksums for all source files in this directory. Format is
21# filename, blanks, base-64 part of an OpenPGP detached signature
22# without the header lines. Blank lines and lines beginning with a
23# hash mark are ignored. A tool to process this file is available by
24# cvs -d :pserver:anoncvs@cvs.gnupg.org:/cvs/wk co misc-scripts/manifest-tool
25#
26# The special entry "$names$" holds a signature over all sorted
27# filenames excluding itself.
28
29gcrypt.h iQCVAwUAQH5RsTEAnp832S/7AQK7xgP+Kc3NY9lipZkaAMrnHDkQVLdHYwTbZWuGOYdTLp8Xy7Auh9wtWV9hrWVUqs+kxDzT/2iF6XkO3WT3rf/PmQ/Q0TIGfOyjE3c/qvB/jVippaxoGda3tnGpODytdI3XPhfPS0Ss8nDzfCStPBGAEq0OVU7imnExrFzhRXt+Gljr0o0==Yagz
30gcrypt-module.h iQCVAwUAQH5UXzEAnp832S/7AQJMQgQAzumz9aaZelhw+FxTCeVadphBxt1bbNQvMrnddYYblyJv+AcxZ9ZxGz2oPeusN58Qg54DQcaW3lYhTgnWfXultsi+Ruxlz7400OUrzSXOl3At7KssdODAoscFzZIgh94G9lzQxEBr9lTXI9R3LsPFJP6muNG4frcNBAA42yckK7w==BBp5
31
32ath.c iQCVAwUAQH5E+DEAnp832S/7AQKFpgP+KSZHtVcnh9FFggIyHKbALUljW2FXauasZvFyN8Sk/mIMgKxyXFOG1THBAUzWLaKWIEWU+WkYU7uThqBtpnEImM5AenWzbQuJjftPC3gVHO8yjjmBWD4zmJj28htoKDoa/xDsoqumrHxae3FYcaCWtYGVjM/Pbl+OMRMOFAhp0ho==lQZ3
33ath.h iQCVAwUAQH5FODEAnp832S/7AQKiuQQAg4K+KOAn1LWBZN32MAhms4FeZKoce0fAuZW7BpyY4cCxIVgxqrtUC90CDykw8XegFfOyyYrgd0NmaMVdY7HZDncNOvIPxpgFQPCZrycsMOoAtoVwjK704RDeNo3zmeyxTKeDH+3M1J7JmLiafaEdSbOC8flX/W0icaV0Ol4dmBc==Ll6w
34
35cipher.h iQCVAwUAQH5FUzEAnp832S/7AQJKLgP9GSSk9f7EINIRqSQH1XKX+dYzt3phDHdqFTUGIfYNh7YzGdy0drvgFhG4k15nqDouKRuFVM/hKY3ZVY7JccmKXKGAH6+ZYShoG6LMFfIGgDX8zne0dNxc72PLfns3fVxNn/RlHmHBkrQ+ppjR9HnSthFmOqzbQaW1BKmc3Z2x5GU==lIeW
36g10lib.h iQCVAwUAQH5FejEAnp832S/7AQJ75wP/ZjOybwRix5eoXdfVeXPjoPygejzpYJJdMUGN3Y5UtkfBu9mPREsKfvZ6tH+Evjx+3xfeAb4bU/k2mRMp0tiWnk2koToS08vI9uxnioKQr9oulZH6r28S+NLSgMQuEGN1JNUky6RQ9TTNRndeTjKKSrEjZ7V6bv+rb8A1bYCKChs==P5mk
37mpi.h iQCVAwUAQH5FwzEAnp832S/7AQJJ4wP9E3jVkcO9M0YtSBHIbjG3hDWKWXzi86AlUh51qiE8/2XP0FfjA4TosyvmicZs7j48HitAByr9tHOSxnbeo7NBf17ICwAo6Eqty+wKDg+eyLeEGUy7VpVK3RJRQAA4H+kl3S2l3YMTKf3WJlbc7qkWSXZspdy5c9sAxeodCKrAubU==oALf
38
39global.c iQCVAwUAQH5HFzEAnp832S/7AQJc+QQAvi53ZkMCzLnVULHvhI6W+EX537zi9n8cplYguvIJqUhAZrP68yGAIyqyCONbZVDyB7wqeXdUMLzMk7W8fg+xuk5JSDpppAQf2m/bdQyze6XVqJso682eYBM8+b9z/IVEvLaFwhZcOKO1bcXudBlBCcJgVDpupfTtAWgPnewil9Q==Xwy1
40misc.c iQCVAwUAQH5IIjEAnp832S/7AQKNJAQAkEpyY3fCG7tvADJFAW9xA7DEQwLCa8YmiUhHvrEsWOI4YgvS7LUbWWc7VqK+ryORvXLKRAVieznbnHAuy0TKtqdnmA/kUmiurS0ah5SWqR/iuAeJtt0RGsmZaZ6oa2m4PZ2Y2GCHSTZqcclvwsetS9eq5AipxHxYFUltu5wGZNI==twM2
41missing-string.c iQCVAwUAQH5JfjEAnp832S/7AQI3ZQQAg55eEJbGQQHyBEJGxvt/FXpQiXcoDit3ZHzvdaQn/NUgdLjCHiWVzhyCXACGivLWMNModDaSaZk073NXxVkWfPcX9vkF//Wugwzidd5P3Bfu5k35o+Xxz82fsk5KuFGGq1mBUZ07xUYQ8KkKkhADUkr0QiQAuypp079Yq0uUC7Q==zvKn
42module.c iQCVAwUAQH5JvjEAnp832S/7AQKlMgQAjZYTXMpWb5kHxCMXzRi069Ku/4/xnWsD+S0dje1LiKzCnRpwTTxARzc/y10Y8OcygkMuR4unEaWedO+9syjjty3fBCcue/j7YlLitq5EC9UE4o23poWvWCuX9Tadm2DK5qf4p7smMJ22O22cLTYTVCyAoYTQ2xC8ajzBsBRkX80==yRRD
43secmem.c iQCVAwUAQH5LLDEAnp832S/7AQKtFwQAwY2wBr6WJC1cwqp/1DQoKzHx9C3plONxbZMazwR7VMI83NUbBAbv1mcxpeZWXmb2dRrnsR1VBbNPDSbJLN5T6czLQ2nIb6mnq9u8Ip4SAa+GCWfDV4AUtAJ4hN/yvWo8iEKu+KD5iJ6xJh31NdXjt5yk6vnk46SA6R4FkHdIEXc==UKVr
44secmem.h iQCVAwUAQH5LTDEAnp832S/7AQIsJwQAkZUu4hvmh9NXCLNm98+tGZFzWYvZO/NffC2wdPE8Q/OTa/m3g+oBbEhaV1ze3oY4t1F/p7ZHFx5CsIp4zVjyPkxlni8AAVMUOQr/LopyxouHn2OjKO+dVqecWQf01+nPWjklbL2FZ3mQ99k2qeWZlVSkz0nm8u39F3v7z3OTCss==AJqE
45sexp.c iQCVAwUAQH5LojEAnp832S/7AQKCTQQArlrj1KGwR2x93fcyN3M0iXuGkBq5R9KNu+1Bq04G4SLlpZ1RRY0OjV3L9To1BHTd01lXlO8MNz7NpRxWlG1Sw5FohbBlhWZQRcW8GdAawJPcfIY2Y8Ek6Yx8quZKbk9uD3bcBmStmg0P+TIA0nr20bmtfB3uX2KQVHQqWZQT5qU==P8FE
46stdmem.c iQCVAwUAQH5LzjEAnp832S/7AQLOUAP9FU16itXBBrkfRDGmhUjAOeEEKdd+brQ3XdT8xoLvP/IH/6U1Kq3ampP2/xcL4kwVdz2rw6NRzP7jlL/yM3tW722lSS/JPJkH+2+qUkcb0fYNoql/WYPMYp1/Mzu6ttXnjag1cQGlKIyYAD+G6h3FtpLwQy0hEJopnF9+Ovd8U7A==CkiZ
47stdmem.h iQCVAwUAQH5L8jEAnp832S/7AQIH0wP+Lyqh0tj++s2L79Tmf/gqgCK+HLMxTddcewF3XbsYf9T5FmLez1gz6Ggti4Ss9VjozOA3ti3trCiA/YNRmV9AYw4zLUPm+MsjJuveL/AgB9HdoD2v+RfJm0WwgSKiysp+8iyjg3Plopmhba4cGuOP5MJ3CWTqYwPmJVscUKC6g38==02MN
48
49types.h iQCVAwUAQH5MKTEAnp832S/7AQLqTAP6A3mUMD5MMkBkebq4bRY6Bq0KsgdKfZ8TLhc2o87gFay8YD0Uom3YJNG2LF/rAIct2ih4jYJaIb5dRfJ0KJoPi2ETd462J8OFCL4fjq9TaSjB2pXcB+kWoxzPasGNg2Ukk0dQ6lvF1tSYrtt32PVI7q/UaPsjTylgRmzLfX/VxrU==OMu3
50
51
52# Configuration
53Makefile.am iQCVAwUAQH5WVjEAnp832S/7AQLmsQP/bbI8/UWAC5yITVhGcCOCbN/FaMqXVKjxESzo6GTs02jxK1y3RuuaoNU1ssQZGAxpFiMJW8u933V3yTHFMxWpwHemDnEyv/a8YACxJBQ0tQgpgHS716BjMbHOfcuOis2WlCOOm0ErjhAYNa4NQ1q3jwkOvTDLFpdnqaWI2wWn08U==Yjun
54libgcrypt.m4 iQCVAwUAQH5MbTEAnp832S/7AQJ1uAQA1C6xI7qXiKVtUeXawhPytAldosrzcXmqz34xi7JklQqw83d68WtWHFMBEUa7MKfi4WCbuQb7FjGUvMRw5z/T9ez7CoDekHc63+cIIZLQ23weUK8GaA1uQLoD0scmT41J5RkBlJbH7ck1zRd3d04o75rWNEUNit6KBvrQ4Pd8oQ8==uMgB
55libgcrypt-config.in iQCVAwUAQH5UbzEAnp832S/7AQJISgP+Nbd2AQnDM/k8sQLbvz8YZjwX3LigZM+AkF1VAwyAm6YOU3nrXnz5t+cXkQD2dkz4L2F0AAsIkFiJsrgmZgCp2h1L6LeFnH+hoId9RhbYw4NkDaHb+MC9JcalpcfFvvxq6vM/W37bSFimM78P+5RLKypXCytVQNAAaIRgZjVfXY8==IGDS
56libgcrypt.vers iQCVAwUAQH5MjTEAnp832S/7AQKCdQQAotG6Z3zdcePI0V33YY2sh91uYkLBNhQw+PzyE3BRRAVhMGLOBD1nSWJHJvE3eyCVOqFY0ZmvpVex51Fa0D/TwsJOO4RVxf1L9bbAncu9OuEXaGXKytLZp54TliDTAWGDq0lvtx1TvDDgtM8TbbaXvMbjfQ4wXBxdLvaenFCTlR4==kgHq
57
58$names$ iQCVAwUAQH5UhDEAnp832S/7AQK/jwP9H7A3mI99M1NGuhD+16C+2gJIITB8GJeYeUd3vm8kWQ5n76WyMCdeA62qn0JUddIBjAbagtfvTL5aesnD9MlhEGaNlHauU7SINTIJ8njKf87EAAfDZrhS/tGDziC2nakMPweRxXQCLDWHkBPjYfrspSLLohjdegqBvTNyVM76+KE==3p9Z
diff --git a/pwmanager/libcrypt/crypt/ath.c b/pwmanager/libcrypt/crypt/ath.c
new file mode 100644
index 0000000..6788bba
--- a/dev/null
+++ b/pwmanager/libcrypt/crypt/ath.c
@@ -0,0 +1,287 @@
1/* ath.c - Thread-safeness library.
2 Copyright (C) 2002, 2003, 2004 g10 Code GmbH
3
4 This file is part of Libgcrypt.
5
6 Libgcrypt is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of
9 the License, or (at your option) any later version.
10
11 Libgcrypt is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with Libgcrypt; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25#include <assert.h>
26#include <unistd.h>
27#ifdef HAVE_SYS_SELECT_H
28# include <sys/select.h>
29#else
30# include <sys/time.h>
31#endif
32#include <sys/types.h>
33#include <sys/wait.h>
34#include <errno.h>
35
36#include "ath.h"
37
38
39/* The interface table. */
40static struct ath_ops ops;
41
42/* True if we should use the external callbacks. */
43static int ops_set;
44
45
46/* For the dummy interface. */
47 #define MUTEX_UNLOCKED((ath_mutex_t) 0)
48 #define MUTEX_LOCKED((ath_mutex_t) 1)
49 #define MUTEX_DESTROYED((ath_mutex_t) 2)
50
51
52
53/* The lock we take while checking for lazy lock initialization. */
54static ath_mutex_t check_init_lock = ATH_MUTEX_INITIALIZER;
55
56int
57ath_init (void)
58{
59 int err = 0;
60
61 if (ops_set)
62 {
63 if (ops.init)
64 err = (*ops.init) ();
65 if (err)
66 return err;
67 err = (*ops.mutex_init) (&check_init_lock);
68 }
69 return err;
70}
71
72
73/* Initialize the locking library. Returns 0 if the operation was
74 successful, EINVAL if the operation table was invalid and EBUSY if
75 we already were initialized. */
76gpg_err_code_t
77ath_install (struct ath_ops *ath_ops, int check_only)
78{
79 if (check_only)
80 {
81 enum ath_thread_option option = ATH_THREAD_OPTION_DEFAULT;
82
83 /* Check if the requested thread option is compatible to the
84 thread option we are already committed to. */
85 if (ath_ops)
86 option = ath_ops->option;
87
88 if (!ops_set && option)
89 return GPG_ERR_NOT_SUPPORTED;
90
91 if (ops.option == ATH_THREAD_OPTION_USER
92 || option == ATH_THREAD_OPTION_USER
93 || ops.option != option)
94 return GPG_ERR_NOT_SUPPORTED;
95
96 return 0;
97 }
98
99 if (ath_ops)
100 {
101 /* It is convenient to not require DESTROY. */
102 if (!ath_ops->mutex_init || !ath_ops->mutex_lock
103 || !ath_ops->mutex_unlock)
104 return GPG_ERR_INV_ARG;
105
106 ops = *ath_ops;
107 ops_set = 1;
108 }
109 else
110 ops_set = 0;
111
112 return 0;
113}
114
115
116static int
117mutex_init (ath_mutex_t *lock, int just_check)
118{
119 int err = 0;
120
121 if (just_check)
122 (*ops.mutex_lock) (&check_init_lock);
123 if (*lock == ATH_MUTEX_INITIALIZER || !just_check)
124 err = (*ops.mutex_init) (lock);
125 if (just_check)
126 (*ops.mutex_unlock) (&check_init_lock);
127 return err;
128}
129
130
131int
132ath_mutex_init (ath_mutex_t *lock)
133{
134 if (ops_set)
135 return mutex_init (lock, 0);
136
137#ifndef NDEBUG
138 *lock = MUTEX_UNLOCKED;
139#endif
140 return 0;
141}
142
143
144int
145ath_mutex_destroy (ath_mutex_t *lock)
146{
147 if (ops_set)
148 {
149 int err = mutex_init (lock, 1);
150
151 if (err)
152 return err;
153
154 if (ops.mutex_destroy)
155 return (*ops.mutex_destroy) (lock);
156 else
157 return 0;
158 }
159
160#ifndef NDEBUG
161 assert (*lock == MUTEX_UNLOCKED);
162
163 *lock = MUTEX_DESTROYED;
164#endif
165 return 0;
166}
167
168
169int
170ath_mutex_lock (ath_mutex_t *lock)
171{
172 if (ops_set)
173 {
174 int ret = mutex_init (lock, 1);
175 if (ret)
176 return ret;
177 return (*ops.mutex_lock) (lock);
178 }
179
180#ifndef NDEBUG
181 assert (*lock == MUTEX_UNLOCKED);
182
183 *lock = MUTEX_LOCKED;
184#endif
185 return 0;
186}
187
188
189int
190ath_mutex_unlock (ath_mutex_t *lock)
191{
192 if (ops_set)
193 {
194 int ret = mutex_init (lock, 1);
195 if (ret)
196 return ret;
197 return (*ops.mutex_unlock) (lock);
198 }
199
200#ifndef NDEBUG
201 assert (*lock == MUTEX_LOCKED);
202
203 *lock = MUTEX_UNLOCKED;
204#endif
205 return 0;
206}
207
208
209ssize_t
210ath_read (int fd, void *buf, size_t nbytes)
211{
212 if (ops_set && ops.read)
213 return (*ops.read) (fd, buf, nbytes);
214 else
215 return read (fd, buf, nbytes);
216}
217
218
219ssize_t
220ath_write (int fd, const void *buf, size_t nbytes)
221{
222 if (ops_set && ops.write)
223 return (*ops.write) (fd, buf, nbytes);
224 else
225 return write (fd, buf, nbytes);
226}
227
228
229ssize_t
230ath_select (int nfd, fd_set *rset, fd_set *wset, fd_set *eset,
231 struct timeval *timeout)
232{
233 if (ops_set && ops.select)
234 return (*ops.select) (nfd, rset, wset, eset, timeout);
235 else
236 return select (nfd, rset, wset, eset, timeout);
237}
238
239
240ssize_t
241ath_waitpid (pid_t pid, int *status, int options)
242{
243 if (ops_set && ops.waitpid)
244 return (*ops.waitpid) (pid, status, options);
245 else
246 return waitpid (pid, status, options);
247}
248
249
250int
251ath_accept (int s, struct sockaddr *addr, socklen_t *length_ptr)
252{
253 if (ops_set && ops.accept)
254 return (*ops.accept) (s, addr, length_ptr);
255 else
256 return accept (s, addr, length_ptr);
257}
258
259
260int
261ath_connect (int s, struct sockaddr *addr, socklen_t length)
262{
263 if (ops_set && ops.connect)
264 return (*ops.connect) (s, addr, length);
265 else
266 return connect (s, addr, length);
267}
268
269
270int
271ath_sendmsg (int s, const struct msghdr *msg, int flags)
272{
273 if (ops_set && ops.sendmsg)
274 return (*ops.sendmsg) (s, msg, flags);
275 else
276 return sendmsg (s, msg, flags);
277}
278
279
280int
281ath_recvmsg (int s, struct msghdr *msg, int flags)
282{
283 if (ops_set && ops.recvmsg)
284 return (*ops.recvmsg) (s, msg, flags);
285 else
286 return recvmsg (s, msg, flags);
287}
diff --git a/pwmanager/libcrypt/crypt/ath.h b/pwmanager/libcrypt/crypt/ath.h
new file mode 100644
index 0000000..d492c2a
--- a/dev/null
+++ b/pwmanager/libcrypt/crypt/ath.h
@@ -0,0 +1,116 @@
1/* ath.h - Thread-safeness library.
2 Copyright (C) 2002, 2003, 2004 g10 Code GmbH
3
4 This file is part of Libgcrypt.
5
6 Libgcrypt is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of
9 the License, or (at your option) any later version.
10
11 Libgcrypt is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with Libgcrypt; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21#ifndef ATH_H
22#define ATH_H
23
24#ifdef _WIN32
25#warning We need to replace these hacks by cleaner code.
26typedef int ssize_t;
27typedef int pid_t;
28#include <windows.h>
29#else
30#include <sys/types.h>
31#include <sys/socket.h>
32#endif
33#include <gpg-error.h>
34
35#include <config.h>
36
37
38/* Define _ATH_EXT_SYM_PREFIX if you want to give all external symbols
39 a prefix. */
40#define _ATH_EXT_SYM_PREFIX _gcry_
41
42#ifdef _ATH_EXT_SYM_PREFIX
43#define _ATH_PREFIX1(x,y) x ## y
44#define _ATH_PREFIX2(x,y) _ATH_PREFIX1(x,y)
45#define _ATH_PREFIX(x) _ATH_PREFIX2(_ATH_EXT_SYM_PREFIX,x)
46#define ath_install _ATH_PREFIX(ath_install)
47#define ath_init _ATH_PREFIX(ath_init)
48#define ath_mutex_init _ATH_PREFIX(ath_mutex_init)
49#define ath_mutex_destroy _ATH_PREFIX(ath_mutex_destroy)
50#define ath_mutex_lock _ATH_PREFIX(ath_mutex_lock)
51#define ath_mutex_unlock _ATH_PREFIX(ath_mutex_unlock)
52#define ath_read _ATH_PREFIX(ath_read)
53#define ath_write _ATH_PREFIX(ath_write)
54#define ath_select _ATH_PREFIX(ath_select)
55#define ath_waitpid _ATH_PREFIX(ath_waitpid)
56#define ath_connect _ATH_PREFIX(ath_connect)
57#define ath_accept _ATH_PREFIX(ath_accept)
58#define ath_sendmsg _ATH_PREFIX(ath_sendmsg)
59#define ath_recvmsg _ATH_PREFIX(ath_recvmsg)
60#endif
61
62
63enum ath_thread_option
64 {
65 ATH_THREAD_OPTION_DEFAULT = 0,
66 ATH_THREAD_OPTION_USER = 1,
67 ATH_THREAD_OPTION_PTH = 2,
68 ATH_THREAD_OPTION_PTHREAD = 3
69 };
70
71struct ath_ops
72{
73 enum ath_thread_option option;
74 int (*init) (void);
75 int (*mutex_init) (void **priv);
76 int (*mutex_destroy) (void *priv);
77 int (*mutex_lock) (void *priv);
78 int (*mutex_unlock) (void *priv);
79 ssize_t (*read) (int fd, void *buf, size_t nbytes);
80 ssize_t (*write) (int fd, const void *buf, size_t nbytes);
81 ssize_t (*select) (int nfd, fd_set *rset, fd_set *wset, fd_set *eset,
82 struct timeval *timeout);
83 ssize_t (*waitpid) (pid_t pid, int *status, int options);
84 int (*accept) (int s, struct sockaddr *addr, socklen_t *length_ptr);
85 int (*connect) (int s, struct sockaddr *addr, socklen_t length);
86 int (*sendmsg) (int s, const struct msghdr *msg, int flags);
87 int (*recvmsg) (int s, struct msghdr *msg, int flags);
88};
89
90gpg_err_code_t ath_install (struct ath_ops *ath_ops, int check_only);
91int ath_init (void);
92
93
94/* Functions for mutual exclusion. */
95typedef void *ath_mutex_t;
96#define ATH_MUTEX_INITIALIZER 0
97
98int ath_mutex_init (ath_mutex_t *mutex);
99int ath_mutex_destroy (ath_mutex_t *mutex);
100int ath_mutex_lock (ath_mutex_t *mutex);
101int ath_mutex_unlock (ath_mutex_t *mutex);
102
103
104/* Replacement for the POSIX functions, which can be used to allow
105 other (user-level) threads to run. */
106ssize_t ath_read (int fd, void *buf, size_t nbytes);
107ssize_t ath_write (int fd, const void *buf, size_t nbytes);
108ssize_t ath_select (int nfd, fd_set *rset, fd_set *wset, fd_set *eset,
109 struct timeval *timeout);
110ssize_t ath_waitpid (pid_t pid, int *status, int options);
111int ath_accept (int s, struct sockaddr *addr, socklen_t *length_ptr);
112int ath_connect (int s, struct sockaddr *addr, socklen_t length);
113int ath_sendmsg (int s, const struct msghdr *msg, int flags);
114int ath_recvmsg (int s, struct msghdr *msg, int flags);
115
116 #endif/* ATH_H */
diff --git a/pwmanager/libcrypt/crypt/cipher.h b/pwmanager/libcrypt/crypt/cipher.h
new file mode 100644
index 0000000..a5f194e
--- a/dev/null
+++ b/pwmanager/libcrypt/crypt/cipher.h
@@ -0,0 +1,80 @@
1/* cipher.h
2 *Copyright (C) 1998, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20#ifndef G10_CIPHER_H
21#define G10_CIPHER_H
22
23#include <gcrypt.h>
24
25#define DBG_CIPHER _gcry_get_debug_flag( 1 )
26
27#include "../cipher/random.h"
28
29#define PUBKEY_FLAG_NO_BLINDING (1 << 0)
30
31/*-- rmd160.c --*/
32void _gcry_rmd160_hash_buffer (char *outbuf, const char *buffer, size_t length);
33/*-- sha1.c --*/
34void _gcry_sha1_hash_buffer (char *outbuf, const char *buffer, size_t length);
35
36/*-- dsa.c --*/
37void _gcry_register_pk_dsa_progress (gcry_handler_progress_t cbc, void *cb_data);
38/*-- elgamal.c --*/
39void _gcry_register_pk_elg_progress (gcry_handler_progress_t cb, void *cb_data);
40/*-- primegen.c --*/
41void _gcry_register_primegen_progress (gcry_handler_progress_t cb, void *cb_data);
42
43/*-- pubkey.c --*/
44const char * _gcry_pk_aliased_algo_name (int algorithm);
45
46/* Declarations for the cipher specifications. */
47extern gcry_cipher_spec_t _gcry_cipher_spec_blowfish;
48extern gcry_cipher_spec_t _gcry_cipher_spec_des;
49extern gcry_cipher_spec_t _gcry_cipher_spec_tripledes;
50extern gcry_cipher_spec_t _gcry_cipher_spec_arcfour;
51extern gcry_cipher_spec_t _gcry_cipher_spec_cast5;
52extern gcry_cipher_spec_t _gcry_cipher_spec_aes;
53extern gcry_cipher_spec_t _gcry_cipher_spec_aes192;
54extern gcry_cipher_spec_t _gcry_cipher_spec_aes256;
55extern gcry_cipher_spec_t _gcry_cipher_spec_twofish;
56extern gcry_cipher_spec_t _gcry_cipher_spec_twofish128;
57extern gcry_cipher_spec_t _gcry_cipher_spec_serpent128;
58extern gcry_cipher_spec_t _gcry_cipher_spec_serpent192;
59extern gcry_cipher_spec_t _gcry_cipher_spec_serpent256;
60extern gcry_cipher_spec_t _gcry_cipher_spec_rfc2268_40;
61
62/* Declarations for the digest specifications. */
63extern gcry_md_spec_t _gcry_digest_spec_crc32;
64extern gcry_md_spec_t _gcry_digest_spec_crc32_rfc1510;
65extern gcry_md_spec_t _gcry_digest_spec_crc24_rfc2440;
66extern gcry_md_spec_t _gcry_digest_spec_md4;
67extern gcry_md_spec_t _gcry_digest_spec_md5;
68extern gcry_md_spec_t _gcry_digest_spec_rmd160;
69extern gcry_md_spec_t _gcry_digest_spec_sha1;
70extern gcry_md_spec_t _gcry_digest_spec_sha256;
71extern gcry_md_spec_t _gcry_digest_spec_sha512;
72extern gcry_md_spec_t _gcry_digest_spec_sha384;
73extern gcry_md_spec_t _gcry_digest_spec_tiger;
74
75/* Declarations for the pubkey cipher specifications. */
76extern gcry_pk_spec_t _gcry_pubkey_spec_rsa;
77extern gcry_pk_spec_t _gcry_pubkey_spec_elg;
78extern gcry_pk_spec_t _gcry_pubkey_spec_dsa;
79
80#endif /*G10_CIPHER_H*/
diff --git a/pwmanager/libcrypt/crypt/g10lib.h b/pwmanager/libcrypt/crypt/g10lib.h
new file mode 100644
index 0000000..0aef376
--- a/dev/null
+++ b/pwmanager/libcrypt/crypt/g10lib.h
@@ -0,0 +1,250 @@
1/* g10lib.h - internal defintions for libgcrypt
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This header is to be used inside of libgcrypt in place of gcrypt.h.
5 * This way we can better distinguish between internal and external
6 * usage of gcrypt.h
7 *
8 * This file is part of Libgcrypt.
9 *
10 * Libgcrypt is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser general Public License as
12 * published by the Free Software Foundation; either version 2.1 of
13 * the License, or (at your option) any later version.
14 *
15 * Libgcrypt is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
23 */
24
25#ifndef G10LIB_H
26#define G10LIB_H 1
27
28#ifdef _GCRYPT_H
29#error gcrypt.h already included
30#endif
31
32#ifndef _GCRYPT_IN_LIBGCRYPT
33#error something is wrong with config.h
34#endif
35
36#include <gcrypt.h>
37#include "types.h"
38
39#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 )
40#define JNLIB_GCC_M_FUNCTION 1
41 #define JNLIB_GCC_A_NR __attribute__ ((noreturn))
42#define JNLIB_GCC_A_PRINTF( f, a ) __attribute__ ((format (printf,f,a)))
43#define JNLIB_GCC_A_NR_PRINTF( f, a ) \
44 __attribute__ ((noreturn, format (printf,f,a)))
45#define GCC_ATTR_NORETURN __attribute__ ((__noreturn__))
46#else
47#define JNLIB_GCC_A_NR
48#define JNLIB_GCC_A_PRINTF( f, a )
49#define JNLIB_GCC_A_NR_PRINTF( f, a )
50#define GCC_ATTR_NORETURN
51#endif
52
53#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96 )
54#define GCC_ATTR_PURE __attribute__ ((__pure__))
55#else
56#define GCC_ATTR_PURE
57#endif
58
59/* (The malloc attribute might be defined prior to 3.2 - I am just not sure) */
60#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2 )
61#define GCC_ATTR_MALLOC __attribute__ ((__malloc__))
62#else
63#define GCC_ATTR_MALLOC
64#endif
65
66#ifdef G10_I18N_H
67#error i18n should not be included here
68#endif
69
70#define _(a) _gcry_gettext(a)
71#define N_(a) (a)
72
73void _gcry_check_heap( const void *a );
74
75int _gcry_get_debug_flag( unsigned int mask );
76
77
78/*-- gcrypt/misc.c --*/
79
80#ifdef JNLIB_GCC_M_FUNCTION
81void _gcry_bug (const char *file, int line,
82 const char *func) GCC_ATTR_NORETURN;
83#else
84void _gcry_bug (const char *file, int line);
85#endif
86
87const char *_gcry_gettext (const char *key);
88void _gcry_fatal_error(int rc, const char *text ) JNLIB_GCC_A_NR;
89void _gcry_log( int level, const char *fmt, ... ) JNLIB_GCC_A_PRINTF(2,3);
90void _gcry_log_bug( const char *fmt, ... ) JNLIB_GCC_A_NR_PRINTF(1,2);
91void _gcry_log_fatal( const char *fmt, ... ) JNLIB_GCC_A_NR_PRINTF(1,2);
92void _gcry_log_error( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2);
93void _gcry_log_info( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2);
94void _gcry_log_debug( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2);
95void _gcry_log_printf ( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2);
96
97void _gcry_set_log_verbosity( int level );
98int _gcry_log_verbosity( int level );
99
100#ifdef JNLIB_GCC_M_FUNCTION
101#define BUG() _gcry_bug( __FILE__ , __LINE__, __FUNCTION__ )
102#else
103#define BUG() _gcry_bug( __FILE__ , __LINE__ )
104#endif
105
106#define log_hexdump _gcry_log_hexdump
107#define log_bug _gcry_log_bug
108#define log_fatal _gcry_log_fatal
109#define log_error _gcry_log_error
110#define log_info _gcry_log_info
111#define log_debug _gcry_log_debug
112#define log_printf _gcry_log_printf
113
114
115
116
117/*-- cipher/pubkey.c --*/
118
119#ifndef mpi_powm
120#define mpi_powm(w,b,e,m) gcry_mpi_powm( (w), (b), (e), (m) )
121#endif
122
123/*-- primegen.c --*/
124gcry_mpi_t _gcry_generate_secret_prime (unsigned int nbits,
125 int (*extra_check)(void*, gcry_mpi_t),
126 void *extra_check_arg);
127gcry_mpi_t _gcry_generate_public_prime (unsigned int nbits,
128 int (*extra_check)(void*, gcry_mpi_t),
129 void *extra_check_arg);
130gcry_mpi_t _gcry_generate_elg_prime( int mode, unsigned pbits, unsigned qbits,
131 gcry_mpi_t g, gcry_mpi_t **factors );
132
133
134/* replacements of missing functions (missing-string.c)*/
135#ifndef HAVE_STPCPY
136char *stpcpy (char *a, const char *b);
137#endif
138#ifndef HAVE_STRCASECMP
139int strcasecmp (const char *a, const char *b) GCC_ATTR_PURE;
140#endif
141
142/* macros used to rename missing functions */
143#ifndef HAVE_STRTOUL
144#define strtoul(a,b,c) ((unsigned long)strtol((a),(b),(c)))
145#endif
146#ifndef HAVE_MEMMOVE
147#define memmove(d, s, n) bcopy((s), (d), (n))
148#endif
149#ifndef HAVE_STRICMP
150 #define stricmp(a,b) strcasecmp( (a), (b) )
151#endif
152#ifndef HAVE_ATEXIT
153#define atexit(a) (on_exit((a),0))
154#endif
155#ifndef HAVE_RAISE
156#define raise(a) kill(getpid(), (a))
157#endif
158
159
160/* some handy macros */
161#ifndef STR
162#define STR(v) #v
163#endif
164#define STR2(v) STR(v)
165#define DIM(v) (sizeof(v)/sizeof((v)[0]))
166#define DIMof(type,member) DIM(((type *)0)->member)
167
168/* Stack burning. */
169
170void _gcry_burn_stack (int bytes);
171
172
173/* To avoid that a compiler optimizes certain memset calls away, these
174 macros may be used instead. */
175#define wipememory2(_ptr,_set,_len) do { \
176 volatile char *_vptr=(volatile char *)(_ptr); \
177 size_t _vlen=(_len); \
178 while(_vlen) { *_vptr=(_set); _vptr++; _vlen--; } \
179 } while(0)
180#define wipememory(_ptr,_len) wipememory2(_ptr,0,_len)
181
182
183
184/* Digit predicates. */
185
186#define digitp(p) (*(p) >= '0' && *(p) <= '9')
187#define octdigitp(p) (*(p) >= '0' && *(p) <= '7')
188#define alphap(a) ( (*(a) >= 'A' && *(a) <= 'Z') \
189 || (*(a) >= 'a' && *(a) <= 'z'))
190#define hexdigitp(a) (digitp (a) \
191 || (*(a) >= 'A' && *(a) <= 'F') \
192 || (*(a) >= 'a' && *(a) <= 'f'))
193
194/* Management for ciphers/digests/pubkey-ciphers. */
195
196/* Structure for each registered `module'. */
197struct gcry_module
198{
199 struct gcry_module *next; /* List pointers. */
200 struct gcry_module **prevp;
201 void *spec; /* The acctual specs. */
202 int flags; /* Associated flags. */
203 int counter; /* Use counter. */
204 unsigned int mod_id; /* ID of this module. */
205};
206
207/* Flags for the `flags' member of gcry_module_t. */
208#define FLAG_MODULE_DISABLED 1 << 0
209
210gcry_err_code_t _gcry_module_add (gcry_module_t *entries,
211 unsigned int id,
212 void *spec,
213 gcry_module_t *module);
214
215typedef int (*gcry_module_lookup_t) (void *spec, void *data);
216
217/* Lookup a module specification by it's ID. After a successfull
218 lookup, the module has it's resource counter incremented. */
219gcry_module_t _gcry_module_lookup_id (gcry_module_t entries,
220 unsigned int id);
221
222/* Internal function. Lookup a module specification. */
223gcry_module_t _gcry_module_lookup (gcry_module_t entries, void *data,
224 gcry_module_lookup_t func);
225
226/* Release a module. In case the use-counter reaches zero, destroy
227 the module. */
228void _gcry_module_release (gcry_module_t entry);
229
230/* Add a reference to a module. */
231void _gcry_module_use (gcry_module_t module);
232
233/* Return a list of module IDs. */
234gcry_err_code_t _gcry_module_list (gcry_module_t modules,
235 int *list, int *list_length);
236
237gcry_err_code_t _gcry_cipher_init (void);
238gcry_err_code_t _gcry_md_init (void);
239gcry_err_code_t _gcry_pk_init (void);
240
241gcry_err_code_t _gcry_pk_module_lookup (int id, gcry_module_t *module);
242void _gcry_pk_module_release (gcry_module_t module);
243
244/* Memory management. */
245
246gcry_err_code_t _gcry_malloc (size_t n, unsigned int flags, void **mem);
247
248#define GCRY_ALLOC_FLAG_SECURE (1 << 0)
249
250#endif /* G10LIB_H */
diff --git a/pwmanager/libcrypt/crypt/gcrypt-module.h b/pwmanager/libcrypt/crypt/gcrypt-module.h
new file mode 100644
index 0000000..1099c92
--- a/dev/null
+++ b/pwmanager/libcrypt/crypt/gcrypt-module.h
@@ -0,0 +1,225 @@
1/* gcrypt-module.h - GNU cryptographic library interface
2 * Copyright (C) 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21/* This file contains the necessary declarations/definitions for
22 working with Libgcrypt modules. */
23
24#ifndef _GCRYPT_MODULE_H
25#define _GCRYPT_MODULE_H
26
27#ifdef __cplusplus
28extern "C" {
29#if 0 /* keep Emacsens's auto-indent happy */
30}
31#endif
32#endif
33
34/* This type represents a `module'. */
35typedef struct gcry_module *gcry_module_t;
36
37/* Check that the library fulfills the version requirement. */
38
39/* Type for the cipher_setkey function. */
40typedef gcry_err_code_t (*gcry_cipher_setkey_t) (void *c,
41 const unsigned char *key,
42 unsigned keylen);
43
44/* Type for the cipher_encrypt function. */
45typedef void (*gcry_cipher_encrypt_t) (void *c,
46 unsigned char *outbuf,
47 const unsigned char *inbuf);
48
49/* Type for the cipher_decrypt function. */
50typedef void (*gcry_cipher_decrypt_t) (void *c,
51 unsigned char *outbuf,
52 const unsigned char *inbuf);
53
54/* Type for the cipher_stencrypt function. */
55typedef void (*gcry_cipher_stencrypt_t) (void *c,
56 unsigned char *outbuf,
57 const unsigned char *inbuf,
58 unsigned int n);
59
60/* Type for the cipher_stdecrypt function. */
61typedef void (*gcry_cipher_stdecrypt_t) (void *c,
62 unsigned char *outbuf,
63 const unsigned char *inbuf,
64 unsigned int n);
65
66typedef struct gcry_cipher_oid_spec
67{
68 const char *oid;
69 int mode;
70} gcry_cipher_oid_spec_t;
71
72/* Module specification structure for ciphers. */
73typedef struct gcry_cipher_spec
74{
75 const char *name;
76 const char **aliases;
77 gcry_cipher_oid_spec_t *oids;
78 size_t blocksize;
79 size_t keylen;
80 size_t contextsize;
81 gcry_cipher_setkey_t setkey;
82 gcry_cipher_encrypt_t encrypt;
83 gcry_cipher_decrypt_t decrypt;
84 gcry_cipher_stencrypt_t stencrypt;
85 gcry_cipher_stdecrypt_t stdecrypt;
86} gcry_cipher_spec_t;
87
88/* Register a new cipher module whose specification can be found in
89 CIPHER. On success, a new algorithm ID is stored in ALGORITHM_ID
90 and a pointer representhing this module is stored in MODULE. */
91gcry_error_t gcry_cipher_register (gcry_cipher_spec_t *cipher,
92 unsigned int *algorithm_id,
93 gcry_module_t *module);
94
95/* Unregister the cipher identified by MODULE, which must have been
96 registered with gcry_cipher_register. */
97void gcry_cipher_unregister (gcry_module_t module);
98
99/* ********************** */
100
101/* Type for the pk_generate function. */
102typedef gcry_err_code_t (*gcry_pk_generate_t) (int algo,
103 unsigned int nbits,
104 unsigned long use_e,
105 gcry_mpi_t *skey,
106 gcry_mpi_t **retfactors);
107
108/* Type for the pk_check_secret_key function. */
109typedef gcry_err_code_t (*gcry_pk_check_secret_key_t) (int algo,
110 gcry_mpi_t *skey);
111
112/* Type for the pk_encrypt function. */
113typedef gcry_err_code_t (*gcry_pk_encrypt_t) (int algo,
114 gcry_mpi_t *resarr,
115 gcry_mpi_t data,
116 gcry_mpi_t *pkey,
117 int flags);
118
119/* Type for the pk_decrypt function. */
120typedef gcry_err_code_t (*gcry_pk_decrypt_t) (int algo,
121 gcry_mpi_t *result,
122 gcry_mpi_t *data,
123 gcry_mpi_t *skey,
124 int flags);
125
126/* Type for the pk_sign function. */
127typedef gcry_err_code_t (*gcry_pk_sign_t) (int algo,
128 gcry_mpi_t *resarr,
129 gcry_mpi_t data,
130 gcry_mpi_t *skey);
131
132/* Type for the pk_verify function. */
133typedef gcry_err_code_t (*gcry_pk_verify_t) (int algo,
134 gcry_mpi_t hash,
135 gcry_mpi_t *data,
136 gcry_mpi_t *pkey,
137 int (*cmp) (void *, gcry_mpi_t),
138 void *opaquev);
139
140/* Type for the pk_get_nbits function. */
141typedef unsigned (*gcry_pk_get_nbits_t) (int algo, gcry_mpi_t *pkey);
142
143/* Module specification structure for message digests. */
144typedef struct gcry_pk_spec
145{
146 const char *name;
147 char **aliases;
148 const char *elements_pkey;
149 const char *elements_skey;
150 const char *elements_enc;
151 const char *elements_sig;
152 const char *elements_grip;
153 int use;
154 gcry_pk_generate_t generate;
155 gcry_pk_check_secret_key_t check_secret_key;
156 gcry_pk_encrypt_t encrypt;
157 gcry_pk_decrypt_t decrypt;
158 gcry_pk_sign_t sign;
159 gcry_pk_verify_t verify;
160 gcry_pk_get_nbits_t get_nbits;
161} gcry_pk_spec_t;
162
163/* Register a new pubkey module whose specification can be found in
164 PUBKEY. On success, a new algorithm ID is stored in ALGORITHM_ID
165 and a pointer representhing this module is stored in MODULE. */
166gcry_error_t gcry_pk_register (gcry_pk_spec_t *pubkey,
167 unsigned int *algorithm_id,
168 gcry_module_t *module);
169
170/* Unregister the pubkey identified by ID, which must have been
171 registered with gcry_pk_register. */
172void gcry_pk_unregister (gcry_module_t module);
173
174/* ********************** */
175
176/* Type for the md_init function. */
177typedef void (*gcry_md_init_t) (void *c);
178
179/* Type for the md_write function. */
180typedef void (*gcry_md_write_t) (void *c, unsigned char *buf, size_t nbytes);
181
182/* Type for the md_final function. */
183typedef void (*gcry_md_final_t) (void *c);
184
185/* Type for the md_read function. */
186typedef unsigned char *(*gcry_md_read_t) (void *c);
187
188typedef struct gcry_md_oid_spec
189{
190 const char *oidstring;
191} gcry_md_oid_spec_t;
192
193/* Module specification structure for message digests. */
194typedef struct gcry_md_spec
195{
196 const char *name;
197 unsigned char *asnoid;
198 int asnlen;
199 gcry_md_oid_spec_t *oids;
200 int mdlen;
201 gcry_md_init_t init;
202 gcry_md_write_t write;
203 gcry_md_final_t final;
204 gcry_md_read_t read;
205 size_t contextsize; /* allocate this amount of context */
206} gcry_md_spec_t;
207
208/* Register a new digest module whose specification can be found in
209 DIGEST. On success, a new algorithm ID is stored in ALGORITHM_ID
210 and a pointer representhing this module is stored in MODULE. */
211gcry_error_t gcry_md_register (gcry_md_spec_t *digest,
212 unsigned int *algorithm_id,
213 gcry_module_t *module);
214
215/* Unregister the digest identified by ID, which must have been
216 registered with gcry_digest_register. */
217void gcry_md_unregister (gcry_module_t module);
218
219#if 0 /* keep Emacsens's auto-indent happy */
220{
221#endif
222#ifdef __cplusplus
223}
224#endif
225#endif
diff --git a/pwmanager/libcrypt/crypt/gcrypt.h b/pwmanager/libcrypt/crypt/gcrypt.h
new file mode 100644
index 0000000..49ac53f
--- a/dev/null
+++ b/pwmanager/libcrypt/crypt/gcrypt.h
@@ -0,0 +1,1497 @@
1/* gcrypt.h - GNU cryptographic library interface
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 * 2004 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 */
21
22#ifndef _GCRYPT_H
23#define _GCRYPT_H
24
25#include <stdarg.h>
26#include <string.h>
27
28#include <gpg-error.h>
29
30#include <sys/types.h>
31#include <sys/socket.h>
32
33/* This is required for error code compatibility. */
34#define _GCRY_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_GCRYPT
35
36#ifdef __cplusplus
37extern "C" {
38#if 0 /* keep Emacsens' auto-indent happy */
39}
40#endif
41#endif
42
43/* The version of this header should match the one of the library. It
44 should not be used by a program because gcry_check_version() should
45 return the same version. The purpose of this macro is to let
46 autoconf (using the AM_PATH_GCRYPT macro) check that this header
47 matches the installed library. Note: Do not edit the next line as
48 configure may fix the string here. */
49#define GCRYPT_VERSION "1.2.0"
50
51/* Internal: We can't use the convenience macros for the multi
52 precision integer functions when building this library. */
53#ifdef _GCRYPT_IN_LIBGCRYPT
54#ifndef GCRYPT_NO_MPI_MACROS
55#define GCRYPT_NO_MPI_MACROS 1
56#endif
57#endif
58
59/* We want to use gcc attributes when possible. Warning: Don't use
60 these macros in your programs: As indicated by the leading
61 underscore they are subject to change without notice. */
62#ifdef __GNUC__
63
64#define _GCRY_GCC_VERSION (__GNUC__ * 10000 \
65 + __GNUC_MINOR__ * 100 \
66 + __GNUC_PATCHLEVEL__)
67
68#if _GCRY_GCC_VERSION >= 30100
69#define _GCRY_GCC_ATTR_DEPRECATED __attribute__ ((__deprecated__))
70#endif
71
72#if _GCRY_GCC_VERSION >= 29600
73#define _GCRY_GCC_ATTR_PURE __attribute__ ((__pure__))
74#endif
75
76#if _GCRY_GCC_VERSION >= 300200
77#define _GCRY_GCC_ATTR_MALLOC __attribute__ ((__malloc__))
78#endif
79
80#endif /*__GNUC__*/
81
82#ifndef _GCRY_GCC_ATTR_DEPRECATED
83#define _GCRY_GCC_ATTR_DEPRECATED
84#endif
85#ifndef _GCRY_GCC_ATTR_PURE
86#define _GCRY_GCC_ATTR_PURE
87#endif
88#ifndef _GCRY_GCC_ATTR_MALLOC
89#define _GCRY_GCC_ATTR_MALLOC
90#endif
91
92/* Wrappers for the libgpg-error library. */
93
94typedef gpg_error_t gcry_error_t;
95typedef gpg_err_code_t gcry_err_code_t;
96typedef gpg_err_source_t gcry_err_source_t;
97
98static GPG_ERR_INLINE gcry_error_t
99gcry_err_make (gcry_err_source_t source, gcry_err_code_t code)
100{
101 return gpg_err_make (source, code);
102}
103
104/* The user can define GPG_ERR_SOURCE_DEFAULT before including this
105 file to specify a default source for gpg_error. */
106#ifndef GCRY_ERR_SOURCE_DEFAULT
107#define GCRY_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_USER_1
108#endif
109
110static GPG_ERR_INLINE gcry_error_t
111gcry_error (gcry_err_code_t code)
112{
113 return gcry_err_make (GCRY_ERR_SOURCE_DEFAULT, code);
114}
115
116static GPG_ERR_INLINE gcry_err_code_t
117gcry_err_code (gcry_error_t err)
118{
119 return gpg_err_code (err);
120}
121
122
123static GPG_ERR_INLINE gcry_err_source_t
124gcry_err_source (gcry_error_t err)
125{
126 return gpg_err_source (err);
127}
128
129/* Return a pointer to a string containing a description of the error
130 code in the error value ERR. */
131const char *gcry_strerror (gcry_error_t err);
132
133/* Return a pointer to a string containing a description of the error
134 source in the error value ERR. */
135const char *gcry_strsource (gcry_error_t err);
136
137/* Retrieve the error code for the system error ERR. This returns
138 GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report
139 this). */
140gcry_err_code_t gcry_err_code_from_errno (int err);
141
142/* Retrieve the system error for the error code CODE. This returns 0
143 if CODE is not a system error code. */
144int gcry_err_code_to_errno (gcry_err_code_t code);
145
146/* Return an error value with the error source SOURCE and the system
147 error ERR. */
148gcry_error_t gcry_err_make_from_errno (gcry_err_source_t source, int err);
149
150/* Return an error value with the system error ERR. */
151gcry_err_code_t gcry_error_from_errno (int err);
152
153
154enum gcry_thread_option
155 {
156 GCRY_THREAD_OPTION_DEFAULT = 0,
157 GCRY_THREAD_OPTION_USER = 1,
158 GCRY_THREAD_OPTION_PTH = 2,
159 GCRY_THREAD_OPTION_PTHREAD = 3
160 };
161
162/* Wrapper for struct ath_ops. */
163struct gcry_thread_cbs
164{
165 enum gcry_thread_option option;
166 int (*init) (void);
167 int (*mutex_init) (void **priv);
168 int (*mutex_destroy) (void **priv);
169 int (*mutex_lock) (void **priv);
170 int (*mutex_unlock) (void **priv);
171 ssize_t (*read) (int fd, void *buf, size_t nbytes);
172 ssize_t (*write) (int fd, const void *buf, size_t nbytes);
173 ssize_t (*select) (int nfd, fd_set *rset, fd_set *wset, fd_set *eset,
174 struct timeval *timeout);
175 ssize_t (*waitpid) (pid_t pid, int *status, int options);
176 int (*accept) (int s, struct sockaddr *addr, socklen_t *length_ptr);
177 int (*connect) (int s, struct sockaddr *addr, socklen_t length);
178 int (*sendmsg) (int s, const struct msghdr *msg, int flags);
179 int (*recvmsg) (int s, struct msghdr *msg, int flags);
180};
181
182 #define GCRY_THREAD_OPTION_PTH_IMPL \
183 static int gcry_pth_init (void) \
184 { return (pth_init () == FALSE) ? errno : 0; } \
185 static int gcry_pth_mutex_init (void **priv) \
186 { \
187 int err = 0; \
188 pth_mutex_t *lock = malloc (sizeof (pth_mutex_t)); \
189 \
190 if (!lock) \
191 err = ENOMEM; \
192 if (!err) \
193 { \
194 err = pth_mutex_init (lock); \
195 if (err == FALSE) \
196 err = errno; \
197 else \
198 err = 0; \
199 if (err) \
200 free (lock); \
201 else \
202 *priv = lock; \
203 } \
204 return err; \
205 } \
206 static int gcry_pth_mutex_destroy (void **lock) \
207 { /* GNU Pth has no destructor function. */ free (*lock); return 0; } \
208 static int gcry_pth_mutex_lock (void **lock) \
209 { return ((pth_mutex_acquire (*lock, 0, NULL)) == FALSE) \
210 ? errno : 0; } \
211 static int gcry_pth_mutex_unlock (void **lock) \
212 { return ((pth_mutex_release (*lock)) == FALSE) \
213 ? errno : 0; } \
214 static ssize_t gcry_pth_read (int fd, void *buf, size_t nbytes) \
215 { return pth_read (fd, buf, nbytes); } \
216 static ssize_t gcry_pth_write (int fd, const void *buf, size_t nbytes) \
217 { return pth_write (fd, buf, nbytes); } \
218 static ssize_t gcry_pth_select (int nfd, fd_set *rset, fd_set *wset, \
219 fd_set *eset, struct timeval *timeout) \
220 { return pth_select (nfd, rset, wset, eset, timeout); } \
221 static ssize_t gcry_pth_waitpid (pid_t pid, int *status, int options) \
222 { return pth_waitpid (pid, status, options); } \
223 static int gcry_pth_accept (int s, struct sockaddr *addr, \
224 socklen_t *length_ptr) \
225 { return pth_accept (s, addr, length_ptr); } \
226static int gcry_pth_connect (int s, struct sockaddr *addr, socklen_t length) \
227 { return pth_connect (s, addr, length); } \
228 \
229 /* FIXME: GNU Pth is missing pth_sendmsg and pth_recvmsg. */ \
230static struct gcry_thread_cbs gcry_threads_pth = { GCRY_THREAD_OPTION_PTH, \
231 gcry_pth_init, gcry_pth_mutex_init, gcry_pth_mutex_destroy, \
232 gcry_pth_mutex_lock, gcry_pth_mutex_unlock, gcry_pth_read, gcry_pth_write, \
233 gcry_pth_select, gcry_pth_waitpid, gcry_pth_accept, gcry_pth_connect }
234
235 #define GCRY_THREAD_OPTION_PTHREAD_IMPL \
236 static int gcry_pthread_mutex_init (void **priv) \
237 { \
238 int err = 0; \
239 pthread_mutex_t *lock = malloc (sizeof (pthread_mutex_t)); \
240 \
241 if (!lock) \
242 err = ENOMEM; \
243 if (!err) \
244 { \
245 err = pthread_mutex_init (lock, NULL); \
246 if (err) \
247 free (lock); \
248 else \
249 *priv = lock; \
250 } \
251 return err; \
252 } \
253 static int gcry_pthread_mutex_destroy (void **lock) \
254 { int err = pthread_mutex_destroy (*lock); free (*lock); return err; } \
255 static int gcry_pthread_mutex_lock (void **lock) \
256 { return pthread_mutex_lock (*lock); } \
257 static int gcry_pthread_mutex_unlock (void **lock) \
258 { return pthread_mutex_unlock (*lock); } \
259 \
260 static struct gcry_thread_cbs gcry_threads_pthread = \
261 { GCRY_THREAD_OPTION_PTHREAD, NULL, \
262 gcry_pthread_mutex_init, gcry_pthread_mutex_destroy, \
263 gcry_pthread_mutex_lock, gcry_pthread_mutex_unlock }
264
265
266/* The data object used to hold a multi precision integer. */
267struct gcry_mpi;
268typedef struct gcry_mpi *gcry_mpi_t;
269
270typedef struct gcry_mpi *GCRY_MPI _GCRY_GCC_ATTR_DEPRECATED;
271typedef struct gcry_mpi *GcryMPI _GCRY_GCC_ATTR_DEPRECATED;
272
273
274
275/* Check that the library fulfills the version requirement. */
276const char *gcry_check_version (const char *req_version);
277
278/* Codes for function dispatchers. */
279
280/* Codes used with the gcry_control function. */
281enum gcry_ctl_cmds
282 {
283 GCRYCTL_SET_KEY = 1,
284 GCRYCTL_SET_IV = 2,
285 GCRYCTL_CFB_SYNC = 3,
286 GCRYCTL_RESET = 4, /* e.g. for MDs */
287 GCRYCTL_FINALIZE = 5,
288 GCRYCTL_GET_KEYLEN = 6,
289 GCRYCTL_GET_BLKLEN = 7,
290 GCRYCTL_TEST_ALGO = 8,
291 GCRYCTL_IS_SECURE = 9,
292 GCRYCTL_GET_ASNOID = 10,
293 GCRYCTL_ENABLE_ALGO = 11,
294 GCRYCTL_DISABLE_ALGO = 12,
295 GCRYCTL_DUMP_RANDOM_STATS = 13,
296 GCRYCTL_DUMP_SECMEM_STATS = 14,
297 GCRYCTL_GET_ALGO_NPKEY = 15,
298 GCRYCTL_GET_ALGO_NSKEY = 16,
299 GCRYCTL_GET_ALGO_NSIGN = 17,
300 GCRYCTL_GET_ALGO_NENCR = 18,
301 GCRYCTL_SET_VERBOSITY = 19,
302 GCRYCTL_SET_DEBUG_FLAGS = 20,
303 GCRYCTL_CLEAR_DEBUG_FLAGS = 21,
304 GCRYCTL_USE_SECURE_RNDPOOL= 22,
305 GCRYCTL_DUMP_MEMORY_STATS = 23,
306 GCRYCTL_INIT_SECMEM = 24,
307 GCRYCTL_TERM_SECMEM = 25,
308 GCRYCTL_DISABLE_SECMEM_WARN = 27,
309 GCRYCTL_SUSPEND_SECMEM_WARN = 28,
310 GCRYCTL_RESUME_SECMEM_WARN = 29,
311 GCRYCTL_DROP_PRIVS = 30,
312 GCRYCTL_ENABLE_M_GUARD = 31,
313 GCRYCTL_START_DUMP = 32,
314 GCRYCTL_STOP_DUMP = 33,
315 GCRYCTL_GET_ALGO_USAGE = 34,
316 GCRYCTL_IS_ALGO_ENABLED = 35,
317 GCRYCTL_DISABLE_INTERNAL_LOCKING = 36,
318 GCRYCTL_DISABLE_SECMEM = 37,
319 GCRYCTL_INITIALIZATION_FINISHED = 38,
320 GCRYCTL_INITIALIZATION_FINISHED_P = 39,
321 GCRYCTL_ANY_INITIALIZATION_P = 40,
322 GCRYCTL_SET_CBC_CTS = 41,
323 GCRYCTL_SET_CBC_MAC = 42,
324 GCRYCTL_SET_CTR = 43,
325 GCRYCTL_ENABLE_QUICK_RANDOM = 44,
326 GCRYCTL_SET_RANDOM_SEED_FILE = 45,
327 GCRYCTL_UPDATE_RANDOM_SEED_FILE = 46,
328 GCRYCTL_SET_THREAD_CBS = 47
329 };
330
331/* Perform various operations defined by CMD. */
332gcry_error_t gcry_control (enum gcry_ctl_cmds CMD, ...);
333
334
335/* S-expression management. */
336
337/* The object to represent an S-expression as used with the public key
338 functions. */
339struct gcry_sexp;
340typedef struct gcry_sexp *gcry_sexp_t;
341
342typedef struct gcry_sexp *GCRY_SEXP _GCRY_GCC_ATTR_DEPRECATED;
343typedef struct gcry_sexp *GcrySexp _GCRY_GCC_ATTR_DEPRECATED;
344
345/* The possible values for the S-expression format. */
346enum gcry_sexp_format
347 {
348 GCRYSEXP_FMT_DEFAULT = 0,
349 GCRYSEXP_FMT_CANON = 1,
350 GCRYSEXP_FMT_BASE64 = 2,
351 GCRYSEXP_FMT_ADVANCED = 3
352 };
353
354/* Create an new S-expression object from BUFFER of size LENGTH and
355 return it in RETSEXP. With AUTODETECT set to 0 the data in BUFFER
356 is expected to be in canonized format. */
357gcry_error_t gcry_sexp_new (gcry_sexp_t *retsexp,
358 const void *buffer, size_t length,
359 int autodetect);
360
361 /* Same as gcry_sexp_new but allows to pass a FREEFNC which has the
362 effect to transfer ownership of BUFFER to the created object. */
363gcry_error_t gcry_sexp_create (gcry_sexp_t *retsexp,
364 void *buffer, size_t length,
365 int autodetect, void (*freefnc) (void *));
366
367/* Scan BUFFER and return a new S-expression object in RETSEXP. This
368 function expects a printf like string in BUFFER. */
369gcry_error_t gcry_sexp_sscan (gcry_sexp_t *retsexp, size_t *erroff,
370 const char *buffer, size_t length);
371
372/* Same as gcry_sexp_sscan but expects a string in FORMAT and can thus
373 only be used for certain encodings. */
374gcry_error_t gcry_sexp_build (gcry_sexp_t *retsexp, size_t *erroff,
375 const char *format, ...);
376
377/* Like gcry_sexp_build, but uses an array instead of variable
378 function arguments. */
379gcry_error_t gcry_sexp_build_array (gcry_sexp_t *retsexp, size_t *erroff,
380 const char *format, void **arg_list);
381
382/* Release the S-expression object SEXP */
383void gcry_sexp_release (gcry_sexp_t sexp);
384
385/* Calculate the length of an canonized S-expresion in BUFFER and
386 check for a valid encoding. */
387size_t gcry_sexp_canon_len (const unsigned char *buffer, size_t length,
388 size_t *erroff, gcry_error_t *errcode);
389
390/* Copies the S-expression object SEXP into BUFFER using the format
391 specified in MODE. */
392size_t gcry_sexp_sprint (gcry_sexp_t sexp, int mode, char *buffer,
393 size_t maxlength);
394
395/* Dumps the S-expression object A in a aformat suitable for debugging
396 to Libgcrypt's logging stream. */
397void gcry_sexp_dump (const gcry_sexp_t a);
398
399gcry_sexp_t gcry_sexp_cons (const gcry_sexp_t a, const gcry_sexp_t b);
400gcry_sexp_t gcry_sexp_alist (const gcry_sexp_t *array);
401gcry_sexp_t gcry_sexp_vlist (const gcry_sexp_t a, ...);
402gcry_sexp_t gcry_sexp_append (const gcry_sexp_t a, const gcry_sexp_t n);
403gcry_sexp_t gcry_sexp_prepend (const gcry_sexp_t a, const gcry_sexp_t n);
404
405/* Scan the S-expression for a sublist with a type (the car of the
406 list) matching the string TOKEN. If TOKLEN is not 0, the token is
407 assumed to be raw memory of this length. The function returns a
408 newly allocated S-expression consisting of the found sublist or
409 `NULL' when not found. */
410gcry_sexp_t gcry_sexp_find_token (gcry_sexp_t list,
411 const char *tok, size_t toklen);
412/* Return the length of the LIST. For a valid S-expression this
413 should be at least 1. */
414int gcry_sexp_length (const gcry_sexp_t list);
415
416/* Create and return a new S-expression from the element with index
417 NUMBER in LIST. Note that the first element has the index 0. If
418 there is no such element, `NULL' is returned. */
419gcry_sexp_t gcry_sexp_nth (const gcry_sexp_t list, int number);
420
421/* Create and return a new S-expression from the first element in
422 LIST; this called the "type" and should always exist and be a
423 string. `NULL' is returned in case of a problem. */
424gcry_sexp_t gcry_sexp_car (const gcry_sexp_t list);
425
426/* Create and return a new list form all elements except for the first
427 one. Note, that this function may return an invalid S-expression
428 because it is not guaranteed, that the type exists and is a string.
429 However, for parsing a complex S-expression it might be useful for
430 intermediate lists. Returns `NULL' on error. */
431gcry_sexp_t gcry_sexp_cdr (const gcry_sexp_t list);
432
433gcry_sexp_t gcry_sexp_cadr (const gcry_sexp_t list);
434
435
436/* This function is used to get data from a LIST. A pointer to the
437 actual data with index NUMBER is returned and the length of this
438 data will be stored to DATALEN. If there is no data at the given
439 index or the index represents another list, `NULL' is returned.
440 *Note:* The returned pointer is valid as long as LIST is not
441 modified or released. */
442const char *gcry_sexp_nth_data (const gcry_sexp_t list, int number,
443 size_t *datalen);
444
445/* This function is used to get and convert data from a LIST. This
446 data is assumed to be an MPI stored in the format described by
447 MPIFMT and returned as a standard Libgcrypt MPI. The caller must
448 release this returned value using `gcry_mpi_release'. If there is
449 no data at the given index, the index represents a list or the
450 value can't be converted to an MPI, `NULL' is returned. */
451gcry_mpi_t gcry_sexp_nth_mpi (gcry_sexp_t list, int number, int mpifmt);
452
453
454
455/*******************************************
456 * *
457 * multi precision integer functions *
458 * *
459 *******************************************/
460
461/* Different formats of external big integer representation. */
462enum gcry_mpi_format
463 {
464 GCRYMPI_FMT_NONE= 0,
465 GCRYMPI_FMT_STD = 1, /* twos complement stored without length */
466 GCRYMPI_FMT_PGP = 2, /* As used by OpenPGP (only defined as unsigned)*/
467 GCRYMPI_FMT_SSH = 3, /* As used by SSH (same as 1 but with length)*/
468 GCRYMPI_FMT_HEX = 4, /* hex format */
469 GCRYMPI_FMT_USG = 5 /* like STD but this is an unsigned one */
470 };
471
472/* Flags used for creating big integers. */
473enum gcry_mpi_flag
474 {
475 GCRYMPI_FLAG_SECURE = 1, /* Allocate the number in "secure" memory. */
476 GCRYMPI_FLAG_OPAQUE = 2 /* The number is not a real one but just a
477 way to store some bytes. This is
478 useful for encrypted big integers. */
479 };
480
481
482/* Allocate a new big integer object, initialize it with 0 and
483 initially allocate memory for a number of at least NBITS. */
484gcry_mpi_t gcry_mpi_new (unsigned int nbits);
485
486/* Same as gcry_mpi_new() but allocate in "secure" memory. */
487gcry_mpi_t gcry_mpi_snew (unsigned int nbits);
488
489/* Release the number A and free all associated resources. */
490void gcry_mpi_release (gcry_mpi_t a);
491
492/* Create a new number with the same value as A. */
493gcry_mpi_t gcry_mpi_copy (const gcry_mpi_t a);
494
495/* Store the big integer value U in W. */
496gcry_mpi_t gcry_mpi_set (gcry_mpi_t w, const gcry_mpi_t u);
497
498/* Store the unsigned integer value U in W. */
499gcry_mpi_t gcry_mpi_set_ui (gcry_mpi_t w, unsigned long u);
500
501/* Swap the values of A and B. */
502void gcry_mpi_swap (gcry_mpi_t a, gcry_mpi_t b);
503
504/* Compare the big integer number U and V returning 0 for equality, a
505 positive value for U > V and a negative for U < V. */
506int gcry_mpi_cmp (const gcry_mpi_t u, const gcry_mpi_t v);
507
508/* Compare the big integer number U with the unsigned integer V
509 returning 0 for equality, a positive value for U > V and a negative
510 for U < V. */
511int gcry_mpi_cmp_ui (const gcry_mpi_t u, unsigned long v);
512
513/* Convert the external representation of an integer stored in BUFFER
514 with a length of BUFLEN into a newly create MPI returned in
515 RET_MPI. If NSCANNED is not NULL, it will receive the number of
516 bytes actually scanned after a successful operation. */
517gcry_error_t gcry_mpi_scan (gcry_mpi_t *ret_mpi, enum gcry_mpi_format format,
518 const unsigned char *buffer, size_t buflen,
519 size_t *nscanned);
520
521/* Convert the big integer A into the external representation
522 described by FORMAT and store it in the provided BUFFER which has
523 been allocated by the user with a size of BUFLEN bytes. NWRITTEN
524 receives the actual length of the external representation unless it
525 has been passed as NULL. */
526gcry_error_t gcry_mpi_print (enum gcry_mpi_format format,
527 unsigned char *buffer, size_t buflen,
528 size_t *nwritten,
529 const gcry_mpi_t a);
530
531/* Convert the big integer A int the external representation described
532 by FORMAT and store it in a newly allocated buffer which address
533 will be put into BUFFER. NWRITTEN receives the actual lengths of the
534 external representation. */
535gcry_error_t gcry_mpi_aprint (enum gcry_mpi_format format,
536 unsigned char **buffer, size_t *nwritten,
537 const gcry_mpi_t a);
538
539/* Dump the value of A in a format suitable for debugging to
540 Libgcrypt's logging stream. Note that one leading space but no
541 trailing space or linefeed will be printed. It is okay to pass
542 NULL for A. */
543void gcry_mpi_dump (const gcry_mpi_t a);
544
545
546/* W = U + V. */
547void gcry_mpi_add (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v);
548
549/* W = U + V. V is an unsigned integer. */
550void gcry_mpi_add_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v);
551
552/* W = U + V mod M. */
553void gcry_mpi_addm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m);
554
555/* W = U - V. */
556void gcry_mpi_sub (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v);
557
558/* W = U - V. V is an unsigned integer. */
559void gcry_mpi_sub_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v );
560
561/* W = U - V mod M */
562void gcry_mpi_subm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m);
563
564/* W = U * V. */
565void gcry_mpi_mul (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v);
566
567/* W = U * V. V is an unsigned integer. */
568void gcry_mpi_mul_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v );
569
570/* W = U * V mod M. */
571void gcry_mpi_mulm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m);
572
573/* W = U * (2 ^ CNT). */
574void gcry_mpi_mul_2exp (gcry_mpi_t w, gcry_mpi_t u, unsigned long cnt);
575
576/* Q = DIVIDEND / DIVISOR, R = DIVIDEND % DIVISOR,
577 Q or R may be passed as NULL. ROUND should be negative or 0. */
578void gcry_mpi_div (gcry_mpi_t q, gcry_mpi_t r,
579 gcry_mpi_t dividend, gcry_mpi_t divisor, int round);
580
581/* R = DIVIDEND % DIVISOR */
582void gcry_mpi_mod (gcry_mpi_t r, gcry_mpi_t dividend, gcry_mpi_t divisor);
583
584/* W = B ^ E mod M. */
585void gcry_mpi_powm (gcry_mpi_t w,
586 const gcry_mpi_t b, const gcry_mpi_t e,
587 const gcry_mpi_t m);
588
589/* Set G to the greatest common divisor of A and B.
590 Return true if the G is 1. */
591int gcry_mpi_gcd (gcry_mpi_t g, gcry_mpi_t a, gcry_mpi_t b);
592
593/* Set X to the multiplicative inverse of A mod M.
594 Return true if the value exists. */
595int gcry_mpi_invm (gcry_mpi_t x, gcry_mpi_t a, gcry_mpi_t m);
596
597
598/* Return the number of bits required to represent A. */
599unsigned int gcry_mpi_get_nbits (gcry_mpi_t a);
600
601/* Return true when bit number N (counting from 0) is set in A. */
602int gcry_mpi_test_bit (gcry_mpi_t a, unsigned int n);
603
604/* Set bit number N in A. */
605void gcry_mpi_set_bit (gcry_mpi_t a, unsigned int n);
606
607/* Clear bit number N in A. */
608void gcry_mpi_clear_bit (gcry_mpi_t a, unsigned int n);
609
610/* Set bit number N in A and clear all bits greater than N. */
611void gcry_mpi_set_highbit (gcry_mpi_t a, unsigned int n);
612
613/* Clear bit number N in A and all bits greater than N. */
614void gcry_mpi_clear_highbit (gcry_mpi_t a, unsigned int n);
615
616/* Shift the value of A by N bits to the right and store the result in X. */
617void gcry_mpi_rshift (gcry_mpi_t x, gcry_mpi_t a, unsigned int n);
618
619/* Store NBITS of the value P points to in A and mark A as an opaque
620 value. WARNING: Never use an opaque MPI for anything thing else then
621 gcry_mpi_release, gcry_mpi_get_opaque. */
622gcry_mpi_t gcry_mpi_set_opaque (gcry_mpi_t a, void *p, unsigned int nbits);
623
624/* Return a pointer to an opaque value stored in A and return its size
625 in NBITS. Note that the returned pointer is still owned by A and
626 that the function should never be used for an non-opaque MPI. */
627void *gcry_mpi_get_opaque (gcry_mpi_t a, unsigned int *nbits);
628
629/* Set the FLAG for the big integer A. Currently only the flag
630 GCRYMPI_FLAG_SECURE is allowed to convert A into an big intger
631 stored in "secure" memory. */
632void gcry_mpi_set_flag (gcry_mpi_t a, enum gcry_mpi_flag flag);
633
634/* Clear FLAG for the big integer A. Note that this function is
635 currently useless as no flags are allowed. */
636void gcry_mpi_clear_flag (gcry_mpi_t a, enum gcry_mpi_flag flag);
637
638/* Return true when the FLAG is set for A. */
639int gcry_mpi_get_flag (gcry_mpi_t a, enum gcry_mpi_flag flag);
640
641/* Unless the GCRYPT_NO_MPI_MACROS is used, provide a couple of
642 convenience macors for the big integer functions. */
643#ifndef GCRYPT_NO_MPI_MACROS
644#define mpi_new(n) gcry_mpi_new( (n) )
645#define mpi_secure_new( n ) gcry_mpi_snew( (n) )
646#define mpi_release(a) \
647 do \
648 { \
649 gcry_mpi_release ((a)); \
650 (a) = NULL; \
651 } \
652 while (0)
653
654#define mpi_copy( a ) gcry_mpi_copy( (a) )
655#define mpi_set( w, u) gcry_mpi_set( (w), (u) )
656#define mpi_set_ui( w, u) gcry_mpi_set_ui( (w), (u) )
657#define mpi_cmp( u, v ) gcry_mpi_cmp( (u), (v) )
658#define mpi_cmp_ui( u, v ) gcry_mpi_cmp_ui( (u), (v) )
659
660#define mpi_add_ui(w,u,v) gcry_mpi_add_ui((w),(u),(v))
661#define mpi_add(w,u,v) gcry_mpi_add ((w),(u),(v))
662#define mpi_addm(w,u,v,m) gcry_mpi_addm ((w),(u),(v),(m))
663#define mpi_sub_ui(w,u,v) gcry_mpi_sub_ui ((w),(u),(v))
664#define mpi_sub(w,u,v) gcry_mpi_sub ((w),(u),(v))
665#define mpi_subm(w,u,v,m) gcry_mpi_subm ((w),(u),(v),(m))
666#define mpi_mul_ui(w,u,v) gcry_mpi_mul_ui ((w),(u),(v))
667#define mpi_mul_2exp(w,u,v) gcry_mpi_mul_2exp ((w),(u),(v))
668#define mpi_mul(w,u,v) gcry_mpi_mul ((w),(u),(v))
669#define mpi_mulm(w,u,v,m) gcry_mpi_mulm ((w),(u),(v),(m))
670#define mpi_powm(w,b,e,m) gcry_mpi_powm ( (w), (b), (e), (m) )
671#define mpi_tdiv(q,r,a,m) gcry_mpi_div ( (q), (r), (a), (m), 0)
672#define mpi_fdiv(q,r,a,m) gcry_mpi_div ( (q), (r), (a), (m), -1)
673#define mpi_mod(r,a,m) gcry_mpi_mod ((r), (a), (m))
674#define mpi_gcd(g,a,b) gcry_mpi_gcd ( (g), (a), (b) )
675#define mpi_invm(g,a,b) gcry_mpi_invm ( (g), (a), (b) )
676
677#define mpi_get_nbits(a) gcry_mpi_get_nbits ((a))
678#define mpi_test_bit(a,b) gcry_mpi_test_bit ((a),(b))
679#define mpi_set_bit(a,b) gcry_mpi_set_bit ((a),(b))
680#define mpi_set_highbit(a,b) gcry_mpi_set_highbit ((a),(b))
681#define mpi_clear_bit(a,b) gcry_mpi_clear_bit ((a),(b))
682#define mpi_clear_highbit(a,b) gcry_mpi_clear_highbit ((a),(b))
683#define mpi_rshift(a,b,c) gcry_mpi_rshift ((a),(b),(c))
684
685#define mpi_set_opaque(a,b,c) gcry_mpi_set_opaque( (a), (b), (c) )
686#define mpi_get_opaque(a,b) gcry_mpi_get_opaque( (a), (b) )
687#endif /* GCRYPT_NO_MPI_MACROS */
688
689
690
691/************************************
692 * *
693 * symmetric cipher functions *
694 * *
695 ************************************/
696
697/* The data object used to hold a handle to an encryption object. */
698struct gcry_cipher_handle;
699typedef struct gcry_cipher_handle *gcry_cipher_hd_t;
700
701typedef struct gcry_cipher_handle *GCRY_CIPHER_HD _GCRY_GCC_ATTR_DEPRECATED;
702typedef struct gcry_cipher_handle *GcryCipherHd _GCRY_GCC_ATTR_DEPRECATED;
703
704/* All symmetric encryption algorithms are identified by their IDs.
705 More IDs may be registered at runtime. */
706enum gcry_cipher_algos
707 {
708 GCRY_CIPHER_NONE = 0,
709 GCRY_CIPHER_IDEA = 1,
710 GCRY_CIPHER_3DES = 2,
711 GCRY_CIPHER_CAST5 = 3,
712 GCRY_CIPHER_BLOWFISH = 4,
713 GCRY_CIPHER_SAFER_SK128 = 5,
714 GCRY_CIPHER_DES_SK = 6,
715 GCRY_CIPHER_AES = 7,
716 GCRY_CIPHER_AES192 = 8,
717 GCRY_CIPHER_AES256 = 9,
718 GCRY_CIPHER_TWOFISH = 10,
719
720 /* Other cipher numbers are above 300 for OpenPGP reasons. */
721 GCRY_CIPHER_ARCFOUR = 301, /* Fully compatible with RSA's RC4 (tm). */
722 GCRY_CIPHER_DES = 302, /* Yes, this is single key 56 bit DES. */
723 GCRY_CIPHER_TWOFISH128 = 303,
724 GCRY_CIPHER_SERPENT128 = 304,
725 GCRY_CIPHER_SERPENT192 = 305,
726 GCRY_CIPHER_SERPENT256 = 306,
727 GCRY_CIPHER_RFC2268_40 = 307, /* Ron's Cipher 2 (40 bit). */
728 GCRY_CIPHER_RFC2268_128 = 308, /* Ron's Cipher 2 (128 bit). */
729 };
730
731/* The Rijndael algorithm is basically AES, so provide some macros. */
732#define GCRY_CIPHER_AES128 GCRY_CIPHER_AES
733#define GCRY_CIPHER_RIJNDAEL GCRY_CIPHER_AES
734#define GCRY_CIPHER_RIJNDAEL128 GCRY_CIPHER_AES128
735#define GCRY_CIPHER_RIJNDAEL192 GCRY_CIPHER_AES192
736#define GCRY_CIPHER_RIJNDAEL256 GCRY_CIPHER_AES256
737
738/* The supported encryption modes. Note that not all of them are
739 supported for each algorithm. */
740enum gcry_cipher_modes
741 {
742 GCRY_CIPHER_MODE_NONE = 0, /* Not yet specified. */
743 GCRY_CIPHER_MODE_ECB = 1, /* Electronic codebook. */
744 GCRY_CIPHER_MODE_CFB = 2, /* Cipher feedback. */
745 GCRY_CIPHER_MODE_CBC = 3, /* Cipher block chaining. */
746 GCRY_CIPHER_MODE_STREAM = 4, /* Used with stream ciphers. */
747 GCRY_CIPHER_MODE_OFB = 5, /* Outer feedback. */
748 GCRY_CIPHER_MODE_CTR = 6 /* Counter. */
749 };
750
751/* Flags used with the open function. */
752enum gcry_cipher_flags
753 {
754 GCRY_CIPHER_SECURE = 1, /* Allocate in secure memory. */
755 GCRY_CIPHER_ENABLE_SYNC = 2, /* Enable CFB sync mode. */
756 GCRY_CIPHER_CBC_CTS = 4, /* Enable CBC cipher text stealing (CTS). */
757 GCRY_CIPHER_CBC_MAC = 8 /* Enable CBC message auth. code (MAC). */
758 };
759
760
761/* Create a handle for algorithm ALGO to be used in MODE. FLAGS may
762 be given as an bitwise OR of the gcry_cipher_flags values. */
763gcry_error_t gcry_cipher_open (gcry_cipher_hd_t *handle,
764 int algo, int mode, unsigned int flags);
765
766/* Close the cioher handle H and release all resource. */
767void gcry_cipher_close (gcry_cipher_hd_t h);
768
769/* Perform various operations on the cipher object H. */
770gcry_error_t gcry_cipher_ctl (gcry_cipher_hd_t h, int cmd, void *buffer,
771 size_t buflen);
772
773/* Retrieve various information about the cipher object H. */
774gcry_error_t gcry_cipher_info (gcry_cipher_hd_t h, int what, void *buffer,
775 size_t *nbytes);
776
777/* Retrieve various information about the cipher algorithm ALGO. */
778gcry_error_t gcry_cipher_algo_info (int algo, int what, void *buffer,
779 size_t *nbytes);
780
781/* Map the cipher algorithm id ALGO to a string representation of that
782 algorithm name. For unknown algorithms this functions returns an
783 empty string. */
784const char *gcry_cipher_algo_name (int algo) _GCRY_GCC_ATTR_PURE;
785
786/* Map the algorithm name NAME to an cipher algorithm ID. Return 0 if
787 the algorithm name is not known. */
788int gcry_cipher_map_name (const char *name) _GCRY_GCC_ATTR_PURE;
789
790/* Given an ASN.1 object identifier in standard IETF dotted decimal
791 format in STRING, return the encryption mode associated with that
792 OID or 0 if not known or applicable. */
793int gcry_cipher_mode_from_oid (const char *string) _GCRY_GCC_ATTR_PURE;
794
795/* Encrypt the plaintext of size INLEN in IN using the cipher handle H
796 into the buffer OUT which has an allocated length of OUTSIZE. For
797 most algorithms it is possible to pass NULL for in and 0 for INLEN
798 and do a in-place decryption of the data provided in OUT. */
799gcry_error_t gcry_cipher_encrypt (gcry_cipher_hd_t h,
800 unsigned char *out, size_t outsize,
801 const unsigned char *in, size_t inlen);
802
803/* The counterpart to gcry_cipher_encrypt. */
804gcry_error_t gcry_cipher_decrypt (gcry_cipher_hd_t h,
805 unsigned char *out, size_t outsize,
806 const unsigned char *in, size_t inlen);
807
808/* Set key K of length L for the cipher handle H. (We have to cast
809 away a const char* here - this catch-all ctl function was probably
810 not the best choice) */
811#define gcry_cipher_setkey(h,k,l) gcry_cipher_ctl( (h), GCRYCTL_SET_KEY, \
812 (char*)(k), (l) )
813
814/* Set initialization vector K of length L for the cipher handle H. */
815#define gcry_cipher_setiv(h,k,l) gcry_cipher_ctl( (h), GCRYCTL_SET_IV, \
816 (char*)(k), (l) )
817
818/* Reset the handle to the state after open. */
819#define gcry_cipher_reset(h) gcry_cipher_ctl ((h), GCRYCTL_RESET, NULL, 0)
820
821/* Perform the the OpenPGP sync operation if this is enabled for the
822 cipher handle H. */
823#define gcry_cipher_sync(h) gcry_cipher_ctl( (h), GCRYCTL_CFB_SYNC, \
824 NULL, 0 )
825
826/* Enable or disable CTS in future calls to gcry_encrypt(). CBC mode only. */
827#define gcry_cipher_cts(h,on) gcry_cipher_ctl( (h), GCRYCTL_SET_CBC_CTS, \
828 NULL, on )
829
830/* Set counter for CTR mode. (K,L) must denote a buffer of block size
831 length, or (NULL,0) to set the CTR to the all-zero block. */
832#define gcry_cipher_setctr(h,k,l) gcry_cipher_ctl( (h), GCRYCTL_SET_CTR, \
833 (char*)(k), (l) )
834
835/* Retrieved the key length used with algorithm A. */
836size_t gcry_cipher_get_algo_keylen (int algo);
837
838/* Retrieve the block length used with algorithm A. */
839size_t gcry_cipher_get_algo_blklen (int algo);
840
841/* Return 0 if the algorithm A is available for use. */
842#define gcry_cipher_test_algo(a) \
843 gcry_cipher_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL )
844
845/* Get a list consisting of the IDs of the loaded cipher modules. If
846 LIST is zero, write the number of loaded cipher modules to
847 LIST_LENGTH and return. If LIST is non-zero, the first
848 *LIST_LENGTH algorithm IDs are stored in LIST, which must be of
849 according size. In case there are less cipher modules than
850 *LIST_LENGTH, *LIST_LENGTH is updated to the correct number. */
851gcry_error_t gcry_cipher_list (int *list, int *list_length);
852
853
854/************************************
855 * *
856 * asymmetric cipher functions *
857 * *
858 ************************************/
859
860/* The algorithms and their IDs we support. */
861enum gcry_pk_algos
862 {
863 GCRY_PK_RSA = 1,
864 GCRY_PK_RSA_E = 2, /* deprecated */
865 GCRY_PK_RSA_S = 3, /* deprecated */
866 GCRY_PK_ELG_E = 16, /* use only for OpenPGP */
867 GCRY_PK_DSA = 17,
868 GCRY_PK_ELG = 20
869 };
870
871/* Flags describing usage capabilities of a PK algorithm. */
872#define GCRY_PK_USAGE_SIGN 1
873#define GCRY_PK_USAGE_ENCR 2
874
875/* Encrypt the DATA using the public key PKEY and store the result as
876 a newly created S-expression at RESULT. */
877gcry_error_t gcry_pk_encrypt (gcry_sexp_t *result,
878 gcry_sexp_t data, gcry_sexp_t pkey);
879
880/* Decrypt the DATA using the private key SKEY and store the result as
881 a newly created S-expression at RESULT. */
882gcry_error_t gcry_pk_decrypt (gcry_sexp_t *result,
883 gcry_sexp_t data, gcry_sexp_t skey);
884
885/* Sign the DATA using the private key SKEY and store the result as
886 a newly created S-expression at RESULT. */
887gcry_error_t gcry_pk_sign (gcry_sexp_t *result,
888 gcry_sexp_t data, gcry_sexp_t skey);
889
890/* Check the signature SIGVAL on DATA using the public key PKEY. */
891gcry_error_t gcry_pk_verify (gcry_sexp_t sigval,
892 gcry_sexp_t data, gcry_sexp_t pkey);
893
894/* Check that private KEY is sane. */
895gcry_error_t gcry_pk_testkey (gcry_sexp_t key);
896
897/* Generate a new key pair according to the parameters given in
898 S_PARMS. The new key pair is returned in as an S-expression in
899 R_KEY. */
900gcry_error_t gcry_pk_genkey (gcry_sexp_t *r_key, gcry_sexp_t s_parms);
901
902/* Catch all function for miscellaneous operations. */
903gcry_error_t gcry_pk_ctl (int cmd, void *buffer, size_t buflen);
904
905/* Retrieve information about the public key algorithm ALGO. */
906gcry_error_t gcry_pk_algo_info (int algo, int what,
907 void *buffer, size_t *nbytes);
908
909/* Map the public key algorithm id ALGO to a string representation of the
910 algorithm name. For unknown algorithms this functions returns an
911 empty string. */
912const char *gcry_pk_algo_name (int algo) _GCRY_GCC_ATTR_PURE;
913
914/* Map the algorithm NAME to a public key algorithm Id. Return 0 if
915 the algorithm name is not known. */
916int gcry_pk_map_name (const char* name) _GCRY_GCC_ATTR_PURE;
917
918/* Return what is commonly referred as the key length for the given
919 public or private KEY. */
920unsigned int gcry_pk_get_nbits (gcry_sexp_t key) _GCRY_GCC_ATTR_PURE;
921
922/* Please note that keygrip is still experimental and should not be
923 used without contacting the author. */
924unsigned char *gcry_pk_get_keygrip (gcry_sexp_t key, unsigned char *array);
925
926/* Return 0 if the public key algorithm A is available for use. */
927#define gcry_pk_test_algo(a) \
928 gcry_pk_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL )
929
930/* Get a list consisting of the IDs of the loaded pubkey modules. If
931 LIST is zero, write the number of loaded pubkey modules to
932 LIST_LENGTH and return. If LIST is non-zero, the first
933 *LIST_LENGTH algorithm IDs are stored in LIST, which must be of
934 according size. In case there are less pubkey modules than
935 *LIST_LENGTH, *LIST_LENGTH is updated to the correct number. */
936gcry_error_t gcry_pk_list (int *list, int *list_length);
937
938/* Alternative interface for asymetric cryptography. */
939
940/* The algorithm IDs. */
941typedef enum gcry_ac_id
942 {
943 GCRY_AC_RSA = 1,
944 GCRY_AC_DSA = 17,
945 GCRY_AC_ELG = 20,
946 GCRY_AC_ELG_E = 16,
947 }
948gcry_ac_id_t;
949
950/* Key types. */
951typedef enum gcry_ac_key_type
952 {
953 GCRY_AC_KEY_SECRET,
954 GCRY_AC_KEY_PUBLIC,
955 }
956gcry_ac_key_type_t;
957
958/* AC data. */
959#define GCRY_AC_FLAG_DEALLOC (1 << 0)
960#define GCRY_AC_FLAG_COPY (1 << 1)
961#define GCRY_AC_FLAG_NO_BLINDING (1 << 2)
962
963/* This type represents a `data set'. */
964typedef struct gcry_ac_data *gcry_ac_data_t;
965
966/* This type represents a single `key', either a secret one or a
967 public one. */
968typedef struct gcry_ac_key *gcry_ac_key_t;
969
970/* This type represents a `key pair' containing a secret and a public
971 key. */
972typedef struct gcry_ac_key_pair *gcry_ac_key_pair_t;
973
974/* This type represents a `handle' that is needed by functions
975 performing cryptographic operations. */
976typedef struct gcry_ac_handle *gcry_ac_handle_t;
977
978/* The caller of gcry_ac_key_pair_generate can provide one of these
979 structures in order to influence the key generation process in an
980 algorithm-specific way. */
981typedef struct gcry_ac_key_spec_rsa
982{
983 gcry_mpi_t e; /* E to use. */
984} gcry_ac_key_spec_rsa_t;
985
986/* Returns a new, empty data set in DATA. */
987gcry_error_t gcry_ac_data_new (gcry_ac_data_t *data);
988
989/* Destroy the data set DATA. */
990void gcry_ac_data_destroy (gcry_ac_data_t data);
991
992/* Create a copy of the data set DATA and store it in DATA_CP. */
993gcry_error_t gcry_ac_data_copy (gcry_ac_data_t *data_cp,
994 gcry_ac_data_t data);
995
996/* Return the number of named MPI values inside of the data set
997 DATA. */
998unsigned int gcry_ac_data_length (gcry_ac_data_t data);
999
1000/* Destroy any values contained in the data set DATA. */
1001void gcry_ac_data_clear (gcry_ac_data_t data);
1002
1003/* Add the value MPI to DATA with the label NAME. If FLAGS contains
1004 GCRY_AC_FLAG_DATA_COPY, the data set will contain copies of NAME
1005 and MPI. If FLAGS contains GCRY_AC_FLAG_DATA_DEALLOC or
1006 GCRY_AC_FLAG_DATA_COPY, the values contained in the data set will
1007 be deallocated when they are to be removed from the data set. */
1008gcry_error_t gcry_ac_data_set (gcry_ac_data_t data, unsigned int flags,
1009 const char *name, gcry_mpi_t mpi);
1010
1011/* Store the value labelled with NAME found in DATA in MPI. If FLAGS
1012 contains GCRY_AC_FLAG_COPY, store a copy of the MPI value contained
1013 in the data set. MPI may be NULL. */
1014gcry_error_t gcry_ac_data_get_name (gcry_ac_data_t data, unsigned int flags,
1015 const char *name, gcry_mpi_t *mpi);
1016
1017/* Stores in NAME and MPI the named MPI value contained in the data
1018 set DATA with the index IDX. If FLAGS contains GCRY_AC_FLAG_COPY,
1019 store copies of the values contained in the data set. NAME or MPI
1020 may be NULL. */
1021gcry_error_t gcry_ac_data_get_index (gcry_ac_data_t data, unsigned int flags,
1022 unsigned int idx,
1023 const char **name, gcry_mpi_t *mpi);
1024
1025/* Create a new ac handle. */
1026gcry_error_t gcry_ac_open (gcry_ac_handle_t *handle,
1027 gcry_ac_id_t algorithm, unsigned int flags);
1028
1029/* Destroy an ac handle. */
1030void gcry_ac_close (gcry_ac_handle_t handle);
1031
1032/* Initialize a key from a given data set. */
1033gcry_error_t gcry_ac_key_init (gcry_ac_key_t *key, gcry_ac_handle_t handle,
1034 gcry_ac_key_type_t type, gcry_ac_data_t data);
1035
1036/* Generates a new key pair via the handle HANDLE of NBITS bits and
1037 stores it in KEY_PAIR. In case non-standard settings are wanted, a
1038 pointer to a structure of type gcry_ac_key_spec_<algorithm>_t,
1039 matching the selected algorithm, can be given as KEY_SPEC.
1040 MISC_DATA is not used yet. */
1041gcry_error_t gcry_ac_key_pair_generate (gcry_ac_handle_t handle,
1042 unsigned int nbits, void *spec,
1043 gcry_ac_key_pair_t *key_pair,
1044 gcry_mpi_t **misc_data);
1045
1046/* Returns the key of type WHICH out of the key pair KEY_PAIR. */
1047gcry_ac_key_t gcry_ac_key_pair_extract (gcry_ac_key_pair_t key_pair,
1048 gcry_ac_key_type_t which);
1049
1050/* Returns the data set contained in the key KEY. */
1051gcry_ac_data_t gcry_ac_key_data_get (gcry_ac_key_t key);
1052
1053/* Verifies that the key KEY is sane via HANDLE. */
1054gcry_error_t gcry_ac_key_test (gcry_ac_handle_t handle, gcry_ac_key_t key);
1055
1056/* Stores the number of bits of the key KEY in NBITS via HANDLE. */
1057gcry_error_t gcry_ac_key_get_nbits (gcry_ac_handle_t handle,
1058 gcry_ac_key_t key, unsigned int *nbits);
1059
1060/* Writes the 20 byte long key grip of the key KEY to KEY_GRIP via
1061 HANDLE. */
1062gcry_error_t gcry_ac_key_get_grip (gcry_ac_handle_t handle, gcry_ac_key_t key,
1063 unsigned char *key_grip);
1064
1065/* Destroy a key. */
1066void gcry_ac_key_destroy (gcry_ac_key_t key);
1067
1068/* Destroy a key pair. */
1069void gcry_ac_key_pair_destroy (gcry_ac_key_pair_t key_pair);
1070
1071/* Encrypt the plain text MPI value DATA_PLAIN with the key KEY under
1072 the control of the flags FLAGS and store the resulting data set
1073 into DATA_ENCRYPTED. */
1074gcry_error_t gcry_ac_data_encrypt (gcry_ac_handle_t handle,
1075 unsigned int flags,
1076 gcry_ac_key_t key,
1077 gcry_mpi_t data_plain,
1078 gcry_ac_data_t *data_encrypted);
1079
1080/* Decrypt the decrypted data contained in the data set DATA_ENCRYPTED
1081 with the key KEY under the control of the flags FLAGS and store the
1082 resulting plain text MPI value in DATA_PLAIN. */
1083gcry_error_t gcry_ac_data_decrypt (gcry_ac_handle_t handle,
1084 unsigned int flags,
1085 gcry_ac_key_t key,
1086 gcry_mpi_t *data_plain,
1087 gcry_ac_data_t data_encrypted);
1088
1089/* Sign the data contained in DATA with the key KEY and store the
1090 resulting signature in the data set DATA_SIGNATURE. */
1091gcry_error_t gcry_ac_data_sign (gcry_ac_handle_t handle,
1092 gcry_ac_key_t key,
1093 gcry_mpi_t data,
1094 gcry_ac_data_t *data_signature);
1095
1096/* Verify that the signature contained in the data set DATA_SIGNATURE
1097 is indeed the result of signing the data contained in DATA with the
1098 secret key belonging to the public key KEY. */
1099gcry_error_t gcry_ac_data_verify (gcry_ac_handle_t handle,
1100 gcry_ac_key_t key,
1101 gcry_mpi_t data,
1102 gcry_ac_data_t data_signature);
1103
1104/* Store the textual representation of the algorithm whose id is given
1105 in ALGORITHM in NAME. */
1106gcry_error_t gcry_ac_id_to_name (gcry_ac_id_t algorithm,
1107 const char **name);
1108
1109/* Store the numeric ID of the algorithm whose textual representation
1110 is contained in NAME in ALGORITHM. */
1111gcry_error_t gcry_ac_name_to_id (const char *name,
1112 gcry_ac_id_t *algorithm);
1113
1114
1115
1116/************************************
1117 * *
1118 * cryptograhic hash functions *
1119 * *
1120 ************************************/
1121
1122/* Algorithm IDs for the hash functions we know about. Not all of them
1123 are implemnted. */
1124enum gcry_md_algos
1125 {
1126 GCRY_MD_NONE = 0,
1127 GCRY_MD_MD5 = 1,
1128 GCRY_MD_SHA1 = 2,
1129 GCRY_MD_RMD160 = 3,
1130 GCRY_MD_MD2 = 5,
1131 GCRY_MD_TIGER = 6, /* TIGER/192. */
1132 GCRY_MD_HAVAL = 7, /* HAVAL, 5 pass, 160 bit. */
1133 GCRY_MD_SHA256 = 8,
1134 GCRY_MD_SHA384 = 9,
1135 GCRY_MD_SHA512 = 10,
1136 GCRY_MD_MD4 = 301,
1137 GCRY_MD_CRC32 = 302,
1138 GCRY_MD_CRC32_RFC1510 = 303,
1139 GCRY_MD_CRC24_RFC2440 = 304
1140 };
1141
1142/* Flags used with the open function. */
1143enum gcry_md_flags
1144 {
1145 GCRY_MD_FLAG_SECURE = 1, /* Allocate all buffers in "secure"
1146 memory. */
1147 GCRY_MD_FLAG_HMAC = 2 /* Make an HMAC out of this
1148 algorithm. */
1149 };
1150
1151/* Forward declaration. */
1152struct gcry_md_context;
1153
1154/* This object is used to hold a handle to a message digest object.
1155 This structure is private - only to be used by the public gcry_md_*
1156 macros. */
1157typedef struct gcry_md_handle
1158{
1159 /* Actual context. */
1160 struct gcry_md_context *ctx;
1161
1162 /* Buffer management. */
1163 int bufpos;
1164 int bufsize;
1165 unsigned char buf[1];
1166} *gcry_md_hd_t;
1167
1168/* Compatibility types, do not use them. */
1169typedef struct gcry_md_handle *GCRY_MD_HD _GCRY_GCC_ATTR_DEPRECATED;
1170typedef struct gcry_md_handle *GcryMDHd _GCRY_GCC_ATTR_DEPRECATED;
1171
1172/* Create a message digest object for algorithm ALGO. FLAGS may be
1173 given as an bitwise OR of the gcry_md_flags values. ALGO may be
1174 given as 0 if the algorithms to be used are later set using
1175 gcry_md_enable. */
1176gcry_error_t gcry_md_open (gcry_md_hd_t *h, int algo, unsigned int flags);
1177
1178/* Release the message digest object HD. */
1179void gcry_md_close (gcry_md_hd_t hd);
1180
1181/* Add the message digest algorithm ALGO to the digest object HD. */
1182gcry_error_t gcry_md_enable (gcry_md_hd_t hd, int algo);
1183
1184/* Create a new digest object as an exact copy of the object HD. */
1185gcry_error_t gcry_md_copy (gcry_md_hd_t *bhd, gcry_md_hd_t ahd);
1186
1187/* Reset the digest object HD to its initial state. */
1188void gcry_md_reset (gcry_md_hd_t hd);
1189
1190/* Perform various operations on the digets object HD. */
1191gcry_error_t gcry_md_ctl (gcry_md_hd_t hd, int cmd, unsigned char *buffer,
1192 size_t buflen);
1193
1194/* Pass LENGTH bytes of data in BUFFER to the digest object HD so that
1195 it can update the digest values. This is the actual hash
1196 function. */
1197void gcry_md_write (gcry_md_hd_t hd, const void *buffer, size_t length);
1198
1199/* Read out the final digest from HD return the digest value for
1200 algorithm ALGO. */
1201unsigned char *gcry_md_read (gcry_md_hd_t hd, int algo);
1202
1203/* Convenience function to calculate the hash from the data in BUFFER
1204 of size LENGTH using the algorithm ALGO avoiding the creating of a
1205 hash object. The hash is returned in the caller provided buffer
1206 DIGEST which must be large enough to hold the digest of the given
1207 algorithm. */
1208void gcry_md_hash_buffer (int algo, void *digest,
1209 const void *buffer, size_t length);
1210
1211/* Retrieve the algorithm used with HD. This does not work reliable
1212 if more than one algorithm is enabled in HD. */
1213int gcry_md_get_algo (gcry_md_hd_t hd);
1214
1215/* Retrieve the length in bytes of the digest yielded by algorithm
1216 ALGO. */
1217unsigned int gcry_md_get_algo_dlen (int algo);
1218
1219/* Return true if the the algorithm ALGO is enabled in the digest
1220 object A. */
1221int gcry_md_is_enabled (gcry_md_hd_t a, int algo);
1222
1223/* Return true if the digest object A is allocated in "secure" memory. */
1224int gcry_md_is_secure (gcry_md_hd_t a);
1225
1226/* Retrieve various information about the object H. */
1227gcry_error_t gcry_md_info (gcry_md_hd_t h, int what, void *buffer,
1228 size_t *nbytes);
1229
1230/* Retrieve various information about the algorithm ALGO. */
1231gcry_error_t gcry_md_algo_info (int algo, int what, void *buffer,
1232 size_t *nbytes);
1233
1234/* Map the digest algorithm id ALGO to a string representation of the
1235 algorithm name. For unknown algorithms this functions returns an
1236 empty string. */
1237const char *gcry_md_algo_name (int algo) _GCRY_GCC_ATTR_PURE;
1238
1239/* Map the algorithm NAME to a digest algorithm Id. Return 0 if
1240 the algorithm name is not known. */
1241int gcry_md_map_name (const char* name) _GCRY_GCC_ATTR_PURE;
1242
1243/* For use with the HMAC feature, the set MAC key to the KEY of
1244 KEYLEN. */
1245gcry_error_t gcry_md_setkey (gcry_md_hd_t hd, const void *key, size_t keylen);
1246
1247/* Update the hash(s) of H with the character C. This is a buffered
1248 version of the gcry_md_write function. */
1249#define gcry_md_putc(h,c) \
1250 do { \
1251 gcry_md_hd_t h__ = (h); \
1252 if( (h__)->bufpos == (h__)->bufsize ) \
1253 gcry_md_write( (h__), NULL, 0 ); \
1254 (h__)->buf[(h__)->bufpos++] = (c) & 0xff; \
1255 } while(0)
1256
1257/* Finalize the digest calculation. This is not really needed because
1258 gcry_md_read() does this implicitly. */
1259#define gcry_md_final(a) \
1260 gcry_md_ctl ((a), GCRYCTL_FINALIZE, NULL, 0)
1261
1262/* Return 0 if the algorithm A is available for use. */
1263#define gcry_md_test_algo(a) \
1264 gcry_md_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL )
1265
1266/* Return an DER encoded ASN.1 OID for the algorithm A in buffer B. N
1267 must point to size_t variable with the available size of buffer B.
1268 After return it will receive the actual size of the returned
1269 OID. */
1270#define gcry_md_get_asnoid(a,b,n) \
1271 gcry_md_algo_info((a), GCRYCTL_GET_ASNOID, (b), (n))
1272
1273/* Enable debugging for digets object A; i.e. create files named
1274 dbgmd-<n>.<string> while hashing. B is a string used as the suffix
1275 for the filename. */
1276#define gcry_md_start_debug(a,b) \
1277 gcry_md_ctl( (a), GCRYCTL_START_DUMP, (b), 0 )
1278
1279/* Disable the debugging of A. */
1280#define gcry_md_stop_debug(a,b) \
1281 gcry_md_ctl( (a), GCRYCTL_STOP_DUMP, (b), 0 )
1282
1283/* Get a list consisting of the IDs of the loaded message digest
1284 modules. If LIST is zero, write the number of loaded message
1285 digest modules to LIST_LENGTH and return. If LIST is non-zero, the
1286 first *LIST_LENGTH algorithm IDs are stored in LIST, which must be
1287 of according size. In case there are less message digest modules
1288 than *LIST_LENGTH, *LIST_LENGTH is updated to the correct
1289 number. */
1290gcry_error_t gcry_md_list (int *list, int *list_length);
1291
1292
1293/************************************
1294 * *
1295 * random generating functions *
1296 * *
1297 ************************************/
1298
1299/* The possible values for the random quality. The rule of thumb is
1300 to use STRONG for session keys and VERY_STRONG for key material.
1301 WEAK is currently an alias for STRONG and should not be used
1302 anymore - use gcry_create_nonce instead. */
1303typedef enum gcry_random_level
1304 {
1305 GCRY_WEAK_RANDOM = 0,
1306 GCRY_STRONG_RANDOM = 1,
1307 GCRY_VERY_STRONG_RANDOM = 2
1308 }
1309gcry_random_level_t;
1310
1311/* Fill BUFFER with LENGTH bytes of random, using random numbers of
1312 quality LEVEL. */
1313void gcry_randomize (unsigned char *buffer, size_t length,
1314 enum gcry_random_level level);
1315
1316/* Add the external random from BUFFER with LENGTH bytes into the
1317 pool. QUALITY should either be -1 for unknown or in the range of 0
1318 to 100 */
1319gcry_error_t gcry_random_add_bytes (const void *buffer, size_t length,
1320 int quality);
1321
1322/* Return NBYTES of allocated random using a random numbers of quality
1323 LEVEL. */
1324void *gcry_random_bytes (size_t nbytes, enum gcry_random_level level)
1325 _GCRY_GCC_ATTR_MALLOC;
1326
1327/* Return NBYTES of allocated random using a random numbers of quality
1328 LEVEL. The random numbers are created returned in "secure"
1329 memory. */
1330void *gcry_random_bytes_secure (size_t nbytes, enum gcry_random_level level)
1331 _GCRY_GCC_ATTR_MALLOC;
1332
1333
1334/* Set the big integer W to a random value of NBITS using a random
1335 generator with quality LEVEL. */
1336void gcry_mpi_randomize (gcry_mpi_t w,
1337 unsigned int nbits, enum gcry_random_level level);
1338
1339
1340/* Create an unpredicable nonce of LENGTH bytes in BUFFER. */
1341void gcry_create_nonce (unsigned char *buffer, size_t length);
1342
1343
1344
1345/* Prime interface. */
1346
1347/* Mode values passed to a gcry_prime_check_func_t. */
1348#define GCRY_PRIME_CHECK_AT_FINISH 0
1349#define GCRY_PRIME_CHECK_AT_GOT_PRIME 1
1350#define GCRY_PRIME_CHECK_AT_MAYBE_PRIME 2
1351
1352/* The function should return 1 if the operation shall continue, 0 to
1353 reject the prime candidate. */
1354typedef int (*gcry_prime_check_func_t) (void *arg, int mode,
1355 gcry_mpi_t candidate);
1356
1357/* Flags for gcry_prime_generate(): */
1358
1359/* Allocate prime numbers and factors in secure memory. */
1360#define GCRY_PRIME_FLAG_SECRET (1 << 0)
1361
1362/* Make sure that at least one prime factor is of size
1363 `FACTOR_BITS'. */
1364#define GCRY_PRIME_FLAG_SPECIAL_FACTOR (1 << 1)
1365
1366/* Generate a new prime number of PRIME_BITS bits and store it in
1367 PRIME. If FACTOR_BITS is non-zero, one of the prime factors of
1368 (prime - 1) / 2 must be FACTOR_BITS bits long. If FACTORS is
1369 non-zero, allocate a new, NULL-terminated array holding the prime
1370 factors and store it in FACTORS. FLAGS might be used to influence
1371 the prime number generation process. */
1372gcry_error_t gcry_prime_generate (gcry_mpi_t *prime,
1373 unsigned int prime_bits,
1374 unsigned int factor_bits,
1375 gcry_mpi_t **factors,
1376 gcry_prime_check_func_t cb_func,
1377 void *cb_arg,
1378 gcry_random_level_t random_level,
1379 unsigned int flags);
1380
1381/* Find a generator for PRIME where the factorization of (prime-1) is
1382 in the NULL terminated array FACTORS. Return the generator as a
1383 newly allocated MPI in R_G. If START_G is not NULL, use this as
1384 teh start for the search. */
1385gcry_error_t gcry_prime_group_generator (gcry_mpi_t *r_g,
1386 gcry_mpi_t prime, gcry_mpi_t *factors,
1387 gcry_mpi_t start_g);
1388
1389
1390/* Convenience function to release the FACTORS array. */
1391void gcry_prime_release_factors (gcry_mpi_t *factors);
1392
1393
1394/* Check wether the number X is prime. */
1395gcry_error_t gcry_prime_check (gcry_mpi_t x, unsigned int flags);
1396
1397
1398
1399/************************************
1400 * *
1401 * miscellaneous stuff *
1402 * *
1403 ************************************/
1404
1405/* Log levels used by the internal logging facility. */
1406enum gcry_log_levels
1407 {
1408 GCRY_LOG_CONT = 0, /* continue the last log line */
1409 GCRY_LOG_INFO = 10,
1410 GCRY_LOG_WARN = 20,
1411 GCRY_LOG_ERROR = 30,
1412 GCRY_LOG_FATAL = 40,
1413 GCRY_LOG_BUG = 50,
1414 GCRY_LOG_DEBUG = 100
1415 };
1416
1417/* Type for progress handlers. */
1418typedef void (*gcry_handler_progress_t) (void *, const char *, int, int, int);
1419
1420/* Type for memory allocation handlers. */
1421typedef void *(*gcry_handler_alloc_t) (size_t n);
1422
1423/* Type for secure memory check handlers. */
1424typedef int (*gcry_handler_secure_check_t) (const void *);
1425
1426/* Type for memory reallocation handlers. */
1427typedef void *(*gcry_handler_realloc_t) (void *p, size_t n);
1428
1429/* Type for memory free handlers. */
1430typedef void (*gcry_handler_free_t) (void *);
1431
1432/* Type for out-of-memory handlers. */
1433typedef int (*gcry_handler_no_mem_t) (void *, size_t, unsigned int);
1434
1435/* Type for fatal error handlers. */
1436typedef void (*gcry_handler_error_t) (void *, int, const char *);
1437
1438/* Type for logging handlers. */
1439typedef void (*gcry_handler_log_t) (void *, int, const char *, va_list);
1440
1441/* Certain operations can provide progress information. This function
1442 is used to register a handler for retrieving these information. */
1443void gcry_set_progress_handler (gcry_handler_progress_t cb, void *cb_data);
1444
1445
1446/* Register a custom memory allocation functions. */
1447void gcry_set_allocation_handler (
1448 gcry_handler_alloc_t func_alloc,
1449 gcry_handler_alloc_t func_alloc_secure,
1450 gcry_handler_secure_check_t func_secure_check,
1451 gcry_handler_realloc_t func_realloc,
1452 gcry_handler_free_t func_free);
1453
1454/* Register a function used instead of the internal out of memory
1455 handler. */
1456void gcry_set_outofcore_handler (gcry_handler_no_mem_t h, void *opaque);
1457
1458/* Register a function used instead of the internal fatal error
1459 handler. */
1460void gcry_set_fatalerror_handler (gcry_handler_error_t fnc, void *opaque);
1461
1462/* Register a function used instead of the internal logging
1463 facility. */
1464void gcry_set_log_handler (gcry_handler_log_t f, void *opaque);
1465
1466/* Reserved for future use. */
1467void gcry_set_gettext_handler (const char *(*f)(const char*));
1468
1469/* Libgcrypt uses its own memory allocation. It is important to use
1470 gcry_free () to release memory allocated by libgcrypt. */
1471void *gcry_malloc (size_t n) _GCRY_GCC_ATTR_MALLOC;
1472void *gcry_calloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
1473void *gcry_malloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC;
1474void *gcry_calloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
1475void *gcry_realloc (void *a, size_t n);
1476char *gcry_strdup (const char *string) _GCRY_GCC_ATTR_MALLOC;
1477void *gcry_xmalloc (size_t n) _GCRY_GCC_ATTR_MALLOC;
1478void *gcry_xcalloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
1479void *gcry_xmalloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC;
1480void *gcry_xcalloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
1481void *gcry_xrealloc (void *a, size_t n);
1482char *gcry_xstrdup (const char * a) _GCRY_GCC_ATTR_MALLOC;
1483void gcry_free (void *a);
1484
1485/* Return true if A is allocated in "secure" memory. */
1486int gcry_is_secure (const void *a) _GCRY_GCC_ATTR_PURE;
1487
1488/* Include support for Libgcrypt modules. */
1489#include <gcrypt-module.h>
1490
1491#if 0 /* keep Emacsens' auto-indent happy */
1492{
1493#endif
1494#ifdef __cplusplus
1495}
1496#endif
1497#endif /* _GCRYPT_H */
diff --git a/pwmanager/libcrypt/crypt/global.c b/pwmanager/libcrypt/crypt/global.c
new file mode 100644
index 0000000..9e7165b
--- a/dev/null
+++ b/pwmanager/libcrypt/crypt/global.c
@@ -0,0 +1,672 @@
1 /* global.c -global control functions
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
3 * 2004 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser general Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 */
21
22#include <config.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <stdarg.h>
27#include <ctype.h>
28#include <assert.h>
29#include <limits.h>
30#include <errno.h>
31
32#include "g10lib.h"
33#include "cipher.h"
34#include "stdmem.h" /* our own memory allocator */
35#include "secmem.h" /* our own secmem allocator */
36#include "ath.h"
37
38/****************
39 * flag bits: 0 : general cipher debug
40 * 1 : general MPI debug
41 */
42static unsigned int debug_flags;
43
44static gcry_handler_alloc_t alloc_func;
45static gcry_handler_alloc_t alloc_secure_func;
46static gcry_handler_secure_check_t is_secure_func;
47static gcry_handler_realloc_t realloc_func;
48static gcry_handler_free_t free_func;
49static gcry_handler_no_mem_t outofcore_handler;
50
51static void *outofcore_handler_value = NULL;
52static int no_secure_memory = 0;
53static int any_init_done;
54
55/* This is our handmade constructor. It gets called by any function
56 likely to be called at startup. The suggested way for an
57 application to make sure that this has been called is by using
58 gcry_check_version. */
59static void
60global_init (void)
61{
62 gcry_err_code_t err = GPG_ERR_NO_ERROR;
63
64 if (any_init_done)
65 return;
66 any_init_done = 1;
67
68 err = ath_init ();
69 if (! err)
70 _gcry_cipher_init ();
71 if (! err)
72 _gcry_md_init ();
73 if (! err)
74 _gcry_pk_init ();
75
76 if (err)
77 /* FIXME? */
78 BUG ();
79}
80
81
82static const char*
83parse_version_number( const char *s, int *number )
84{
85 int val = 0;
86
87 if( *s == '0' && isdigit(s[1]) )
88 return NULL; /* leading zeros are not allowed */
89 for ( ; isdigit(*s); s++ ) {
90 val *= 10;
91 val += *s - '0';
92 }
93 *number = val;
94 return val < 0? NULL : s;
95}
96
97
98static const char *
99parse_version_string( const char *s, int *major, int *minor, int *micro )
100{
101 s = parse_version_number( s, major );
102 if( !s || *s != '.' )
103 return NULL;
104 s++;
105 s = parse_version_number( s, minor );
106 if( !s || *s != '.' )
107 return NULL;
108 s++;
109 s = parse_version_number( s, micro );
110 if( !s )
111 return NULL;
112 return s; /* patchlevel */
113}
114
115/****************
116 * Check that the the version of the library is at minimum the requested one
117 * and return the version string; return NULL if the condition is not
118 * satisfied. If a NULL is passed to this function, no check is done,
119 * but the version string is simply returned.
120 */
121const char *
122gcry_check_version( const char *req_version )
123{
124 const char *ver = VERSION;
125 int my_major, my_minor, my_micro;
126 int rq_major, rq_minor, rq_micro;
127 const char *my_plvl, *rq_plvl;
128
129 global_init ();
130 if ( !req_version )
131 return ver;
132
133 my_plvl = parse_version_string( ver, &my_major, &my_minor, &my_micro );
134 if ( !my_plvl )
135 return NULL; /* very strange our own version is bogus */
136 rq_plvl = parse_version_string( req_version, &rq_major, &rq_minor,
137 &rq_micro );
138 if ( !rq_plvl )
139 return NULL; /* req version string is invalid */
140
141 if ( my_major > rq_major
142 || (my_major == rq_major && my_minor > rq_minor)
143 || (my_major == rq_major && my_minor == rq_minor
144 && my_micro > rq_micro)
145 || (my_major == rq_major && my_minor == rq_minor
146 && my_micro == rq_micro
147 && strcmp( my_plvl, rq_plvl ) >= 0) ) {
148 return ver;
149 }
150 return NULL;
151}
152
153gcry_error_t
154gcry_control (enum gcry_ctl_cmds cmd, ...)
155{
156 gcry_err_code_t err = GPG_ERR_NO_ERROR;
157 static int init_finished = 0;
158 va_list arg_ptr;
159
160 va_start (arg_ptr, cmd);
161 switch (cmd)
162 {
163 case GCRYCTL_ENABLE_M_GUARD:
164 _gcry_private_enable_m_guard ();
165 break;
166
167 case GCRYCTL_ENABLE_QUICK_RANDOM:
168 _gcry_quick_random_gen (1);
169 break;
170
171 case GCRYCTL_DUMP_RANDOM_STATS:
172 _gcry_random_dump_stats ();
173 break;
174
175 case GCRYCTL_DUMP_MEMORY_STATS:
176 /*m_print_stats("[fixme: prefix]");*/
177 break;
178
179 case GCRYCTL_DUMP_SECMEM_STATS:
180 _gcry_secmem_dump_stats ();
181 break;
182
183 case GCRYCTL_DROP_PRIVS:
184 global_init ();
185 _gcry_secmem_init (0);
186 break;
187
188 case GCRYCTL_DISABLE_SECMEM:
189 global_init ();
190 no_secure_memory = 1;
191 break;
192
193 case GCRYCTL_INIT_SECMEM:
194 global_init ();
195 _gcry_secmem_init (va_arg (arg_ptr, unsigned int));
196 break;
197
198 case GCRYCTL_TERM_SECMEM:
199 global_init ();
200 _gcry_secmem_term ();
201 break;
202
203 case GCRYCTL_DISABLE_SECMEM_WARN:
204 _gcry_secmem_set_flags ((_gcry_secmem_get_flags ()
205 | GCRY_SECMEM_FLAG_NO_WARNING));
206 break;
207
208 case GCRYCTL_SUSPEND_SECMEM_WARN:
209 _gcry_secmem_set_flags ((_gcry_secmem_get_flags ()
210 | GCRY_SECMEM_FLAG_SUSPEND_WARNING));
211 break;
212
213 case GCRYCTL_RESUME_SECMEM_WARN:
214 _gcry_secmem_set_flags ((_gcry_secmem_get_flags ()
215 & ~GCRY_SECMEM_FLAG_SUSPEND_WARNING));
216 break;
217
218 case GCRYCTL_USE_SECURE_RNDPOOL:
219 global_init ();
220 _gcry_secure_random_alloc (); /* put random number into secure memory */
221 break;
222
223 case GCRYCTL_SET_RANDOM_SEED_FILE:
224 _gcry_set_random_seed_file (va_arg (arg_ptr, const char *));
225 break;
226
227 case GCRYCTL_UPDATE_RANDOM_SEED_FILE:
228 _gcry_update_random_seed_file ();
229 break;
230
231 case GCRYCTL_SET_VERBOSITY:
232 _gcry_set_log_verbosity (va_arg (arg_ptr, int));
233 break;
234
235 case GCRYCTL_SET_DEBUG_FLAGS:
236 debug_flags |= va_arg (arg_ptr, unsigned int);
237 break;
238
239 case GCRYCTL_CLEAR_DEBUG_FLAGS:
240 debug_flags &= ~va_arg (arg_ptr, unsigned int);
241 break;
242
243 case GCRYCTL_DISABLE_INTERNAL_LOCKING:
244 global_init ();
245 break;
246
247 case GCRYCTL_ANY_INITIALIZATION_P:
248 if (any_init_done)
249 err = GPG_ERR_GENERAL;
250 break;
251
252 case GCRYCTL_INITIALIZATION_FINISHED_P:
253 if (init_finished)
254 err = GPG_ERR_GENERAL;
255 break;
256
257 case GCRYCTL_INITIALIZATION_FINISHED:
258 /* This is a hook which should be used by an application after
259 all initialization has been done and right before any threads
260 are started. It is not really needed but the only way to be
261 really sure that all initialization for thread-safety has
262 been done. */
263 if (! init_finished)
264 {
265 global_init ();
266 _gcry_random_initialize ();
267 init_finished = 1;
268 }
269 break;
270
271 case GCRYCTL_SET_THREAD_CBS:
272 err = ath_install (va_arg (arg_ptr, void *), any_init_done);
273 break;
274
275 default:
276 err = GPG_ERR_INV_OP;
277 }
278
279 va_end(arg_ptr);
280 return gcry_error (err);
281}
282
283/* Return a pointer to a string containing a description of the error
284 code in the error value ERR. */
285const char *
286gcry_strerror (gcry_error_t err)
287{
288 return gpg_strerror (err);
289}
290
291/* Return a pointer to a string containing a description of the error
292 source in the error value ERR. */
293const char *
294gcry_strsource (gcry_error_t err)
295{
296 return gpg_strsource (err);
297}
298
299/* Retrieve the error code for the system error ERR. This returns
300 GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report
301 this). */
302gcry_err_code_t
303gcry_err_code_from_errno (int err)
304{
305 return gpg_err_code_from_errno (err);
306}
307
308
309/* Retrieve the system error for the error code CODE. This returns 0
310 if CODE is not a system error code. */
311int
312gcry_err_code_to_errno (gcry_err_code_t code)
313{
314 return gpg_err_code_from_errno (code);
315}
316
317
318/* Return an error value with the error source SOURCE and the system
319 error ERR. */
320gcry_error_t
321gcry_err_make_from_errno (gpg_err_source_t source, int err)
322{
323 return gpg_err_make_from_errno (source, err);
324}
325
326
327/* Return an error value with the system error ERR. */
328gcry_err_code_t
329gcry_error_from_errno (int err)
330{
331 return gcry_error (gpg_err_code_from_errno (err));
332}
333
334/****************
335 * NOTE: All 5 functions should be set. */
336void
337gcry_set_allocation_handler (gcry_handler_alloc_t new_alloc_func,
338 gcry_handler_alloc_t new_alloc_secure_func,
339 gcry_handler_secure_check_t new_is_secure_func,
340 gcry_handler_realloc_t new_realloc_func,
341 gcry_handler_free_t new_free_func)
342{
343 global_init ();
344
345 alloc_func = new_alloc_func;
346 alloc_secure_func = new_alloc_secure_func;
347 is_secure_func = new_is_secure_func;
348 realloc_func = new_realloc_func;
349 free_func = new_free_func;
350}
351
352
353
354/****************
355 * Set an optional handler which is called in case the xmalloc functions
356 * ran out of memory. This handler may do one of these things:
357 * o free some memory and return true, so that the xmalloc function
358 * tries again.
359 * o Do whatever it like and return false, so that the xmalloc functions
360 * use the default fatal error handler.
361 * o Terminate the program and don't return.
362 *
363 * The handler function is called with 3 arguments: The opaque value set with
364 * this function, the requested memory size, and a flag with these bits
365 * currently defined:
366 *bit 0 set = secure memory has been requested.
367 */
368void
369gcry_set_outofcore_handler( int (*f)( void*, size_t, unsigned int ),
370 void *value )
371{
372 global_init ();
373
374 outofcore_handler = f;
375 outofcore_handler_value = value;
376}
377
378gcry_err_code_t
379_gcry_malloc (size_t n, unsigned int flags, void **mem)
380{
381 gcry_err_code_t err = GPG_ERR_NO_ERROR;
382 void *m = NULL;
383
384 if ((flags & GCRY_ALLOC_FLAG_SECURE) && !no_secure_memory)
385 {
386 if (alloc_secure_func)
387 m = (*alloc_secure_func) (n);
388 else
389 m = _gcry_private_malloc_secure (n);
390 }
391 else
392 {
393 if (alloc_func)
394 m = (*alloc_func) (n);
395 else
396 m = _gcry_private_malloc (n);
397 }
398
399 if (! m)
400 err = gpg_err_code_from_errno (ENOMEM);
401 else
402 *mem = m;
403
404 return err;
405}
406
407void *
408gcry_malloc (size_t n)
409{
410 void *mem = NULL;
411
412 _gcry_malloc (n, 0, &mem);
413
414 return mem;
415}
416
417void *
418gcry_malloc_secure (size_t n)
419{
420 void *mem = NULL;
421
422 _gcry_malloc (n, GCRY_ALLOC_FLAG_SECURE, &mem);
423
424 return mem;
425}
426
427int
428gcry_is_secure (const void *a)
429{
430 if (no_secure_memory)
431 return 0;
432 if (is_secure_func)
433 return is_secure_func (a) ;
434 return _gcry_private_is_secure (a);
435}
436
437void
438_gcry_check_heap( const void *a )
439{
440 /* FIXME: implement this*/
441#if 0
442 if( some_handler )
443 some_handler(a)
444 else
445 _gcry_private_check_heap(a)
446#endif
447}
448
449void *
450gcry_realloc (void *a, size_t n)
451{
452 if (realloc_func)
453 return realloc_func (a, n);
454 return _gcry_private_realloc (a, n);
455}
456
457void
458gcry_free( void *p )
459{
460 if( !p )
461 return;
462
463 if (free_func)
464 free_func (p);
465 else
466 _gcry_private_free (p);
467}
468
469void *
470gcry_calloc (size_t n, size_t m)
471{
472 size_t bytes;
473 void *p;
474
475 bytes = n * m; /* size_t is unsigned so the behavior on overflow is
476 defined. */
477 if (m && bytes / m != n)
478 {
479 errno = ENOMEM;
480 return NULL;
481 }
482
483 p = gcry_malloc (bytes);
484 if (p)
485 memset (p, 0, bytes);
486 return p;
487}
488
489void *
490gcry_calloc_secure (size_t n, size_t m)
491{
492 size_t bytes;
493 void *p;
494
495 bytes = n * m; /* size_t is unsigned so the behavior on overflow is
496 defined. */
497 if (m && bytes / m != n)
498 {
499 errno = ENOMEM;
500 return NULL;
501 }
502
503 p = gcry_malloc_secure (bytes);
504 if (p)
505 memset (p, 0, bytes);
506 return p;
507}
508
509
510/* Create and return a copy of the null-terminated string STRING. If
511 it is contained in secure memory, the copy will be contained in
512 secure memory as well. In an out-of-memory condition, NULL is
513 returned. */
514char *
515gcry_strdup (const char *string)
516{
517 char *string_cp = NULL;
518 size_t string_n = 0;
519
520 string_n = strlen (string);
521
522 if (gcry_is_secure (string))
523 string_cp = gcry_malloc_secure (string_n + 1);
524 else
525 string_cp = gcry_malloc (string_n + 1);
526
527 if (string_cp)
528 strcpy (string_cp, string);
529
530 return string_cp;
531}
532
533
534void *
535gcry_xmalloc( size_t n )
536{
537 void *p;
538
539 while ( !(p = gcry_malloc( n )) ) {
540 if( !outofcore_handler
541 || !outofcore_handler( outofcore_handler_value, n, 0 ) ) {
542 _gcry_fatal_error(gpg_err_code_from_errno (errno), NULL );
543 }
544 }
545 return p;
546}
547
548void *
549gcry_xrealloc( void *a, size_t n )
550{
551 void *p;
552
553 while ( !(p = gcry_realloc( a, n )) ) {
554 if( !outofcore_handler
555 || !outofcore_handler( outofcore_handler_value, n, 2 ) ) {
556 _gcry_fatal_error(gpg_err_code_from_errno (errno), NULL );
557 }
558 }
559 return p;
560}
561
562void *
563gcry_xmalloc_secure( size_t n )
564{
565 void *p;
566
567 while ( !(p = gcry_malloc_secure( n )) ) {
568 if( !outofcore_handler
569 || !outofcore_handler( outofcore_handler_value, n, 1 ) ) {
570 _gcry_fatal_error(gpg_err_code_from_errno (errno),
571 _("out of core in secure memory"));
572 }
573 }
574 return p;
575}
576
577void *
578gcry_xcalloc( size_t n, size_t m )
579{
580 void *p = gcry_xmalloc( n*m );
581 memset( p, 0, n*m );
582 return p;
583}
584
585void *
586gcry_xcalloc_secure( size_t n, size_t m )
587{
588 void *p = gcry_xmalloc_secure( n* m );
589 memset( p, 0, n*m );
590 return p;
591}
592
593char *
594gcry_xstrdup (const char *string)
595{
596 char *p;
597
598 while ( !(p = gcry_strdup (string)) )
599 {
600 size_t n = strlen (string);
601 int is_sec = !!gcry_is_secure (string);
602
603 if (!outofcore_handler
604 || !outofcore_handler (outofcore_handler_value, n, is_sec) )
605 {
606 _gcry_fatal_error (gpg_err_code_from_errno (errno),
607 is_sec? _("out of core in secure memory"):NULL);
608 }
609 }
610
611 strcpy( p, string );
612 return p;
613}
614
615
616int
617_gcry_get_debug_flag( unsigned int mask )
618{
619 return debug_flags & mask;
620}
621
622
623
624/* It is often useful to get some feedback of long running operations.
625 This function may be used to register a handler for this.
626 The callback function CB is used as:
627
628 void cb (void *opaque, const char *what, int printchar,
629 int current, int total);
630
631 Where WHAT is a string identifying the the type of the progress
632 output, PRINTCHAR the character usually printed, CURRENT the amount
633 of progress currently done and TOTAL the expected amount of
634 progress. A value of 0 for TOTAL indicates that there is no
635 estimation available.
636
637 Defined values for WHAT:
638
639 "need_entropy" X 0 number-of-bytes-required
640 When running low on entropy
641 "primegen" '\n' 0 0
642 Prime generated
643 '!'
644 Need to refresh the prime pool
645 '<','>'
646 Number of bits adjusted
647 '^'
648 Looking for a generator
649 '.'
650 Fermat tests on 10 candidates failed
651 ':'
652 Restart with a new random value
653 '+'
654 Rabin Miller test passed
655 "pk_elg" '+','-','.','\n' 0 0
656 Only used in debugging mode.
657 "pk_dsa"
658 Only used in debugging mode.
659*/
660void
661gcry_set_progress_handler (void (*cb)(void *,const char*,int, int, int),
662 void *cb_data)
663{
664#if USE_DSA
665 _gcry_register_pk_dsa_progress (cb, cb_data);
666#endif
667#if USE_ELGAMAL
668 _gcry_register_pk_elg_progress (cb, cb_data);
669#endif
670 _gcry_register_primegen_progress (cb, cb_data);
671 _gcry_register_random_progress (cb, cb_data);
672}
diff --git a/pwmanager/libcrypt/crypt/misc.c b/pwmanager/libcrypt/crypt/misc.c
new file mode 100644
index 0000000..6b630e6
--- a/dev/null
+++ b/pwmanager/libcrypt/crypt/misc.c
@@ -0,0 +1,241 @@
1/* misc.c
2 *Copyright (C) 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <config.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <stdarg.h>
26#include <assert.h>
27#include <unistd.h>
28
29#include "g10lib.h"
30
31static int verbosity_level = 0;
32
33static void (*fatal_error_handler)(void*,int, const char*) = NULL;
34static void *fatal_error_handler_value = 0;
35static void (*log_handler)(void*,int, const char*, va_list) = NULL;
36static void *log_handler_value = 0;
37
38static const char *(*user_gettext_handler)( const char * ) = NULL;
39
40void
41gcry_set_gettext_handler( const char *(*f)(const char*) )
42{
43 user_gettext_handler = f;
44}
45
46
47const char *
48_gcry_gettext( const char *key )
49{
50 if( user_gettext_handler )
51 return user_gettext_handler( key );
52 /* FIXME: switch the domain to gnupg and restore later */
53 return key;
54}
55
56void
57gcry_set_fatalerror_handler( void (*fnc)(void*,int, const char*), void *value)
58{
59 fatal_error_handler_value = value;
60 fatal_error_handler = fnc;
61}
62
63static void
64write2stderr( const char *s )
65{
66 write( 2, s, strlen(s) );
67}
68
69/*
70 * This function is called for fatal errors. A caller might want to
71 * set his own handler because this function simply calls abort().
72 */
73void
74_gcry_fatal_error (int rc, const char *text)
75{
76 if ( !text ) /* get a default text */
77 text = gpg_strerror (rc);
78
79 if (fatal_error_handler)
80 fatal_error_handler (fatal_error_handler_value, rc, text);
81
82 write2stderr("\nFatal error: ");
83 write2stderr(text);
84 write2stderr("\n");
85 abort ();
86}
87
88void
89gcry_set_log_handler( void (*f)(void*,int, const char*, va_list ),
90 void *opaque )
91{
92 log_handler = f;
93 log_handler_value = opaque;
94}
95
96void
97_gcry_set_log_verbosity( int level )
98{
99 verbosity_level = level;
100}
101
102int
103_gcry_log_verbosity( int level )
104{
105 return verbosity_level >= level;
106}
107
108/****************
109 * This is our log function which prints all log messages to stderr or
110 * using the function defined with gcry_set_log_handler().
111 */
112static void
113_gcry_logv( int level, const char *fmt, va_list arg_ptr )
114{
115 if( log_handler )
116 log_handler( log_handler_value, level, fmt, arg_ptr );
117 else {
118 switch ( level ) {
119 case GCRY_LOG_CONT: break;
120 case GCRY_LOG_INFO: break;
121 case GCRY_LOG_WARN: break;
122 case GCRY_LOG_ERROR: break;
123 case GCRY_LOG_FATAL: fputs("Fatal: ",stderr ); break;
124 case GCRY_LOG_BUG: fputs("Ohhhh jeeee: ", stderr); break;
125 case GCRY_LOG_DEBUG: fputs("DBG: ", stderr ); break;
126 default: fprintf(stderr,"[Unknown log level %d]: ", level ); break;
127 }
128 vfprintf(stderr,fmt,arg_ptr) ;
129 }
130
131 if( level == GCRY_LOG_FATAL )
132 exit(2);
133 else if( level == GCRY_LOG_BUG )
134 abort();
135}
136
137void
138_gcry_log( int level, const char *fmt, ... )
139{
140 va_list arg_ptr ;
141
142 va_start( arg_ptr, fmt ) ;
143 _gcry_logv( level, fmt, arg_ptr );
144 va_end(arg_ptr);
145}
146
147
148#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 )
149void
150_gcry_bug( const char *file, int line, const char *func )
151{
152 _gcry_log( GCRY_LOG_BUG,
153 ("... this is a bug (%s:%d:%s)\n"), file, line, func );
154 abort(); /* never called, but it makes the compiler happy */
155}
156#else
157void
158_gcry_bug( const char *file, int line )
159{
160 _gcry_log( GCRY_LOG_BUG,
161 _("you found a bug ... (%s:%d)\n"), file, line);
162 abort(); /* never called, but it makes the compiler happy */
163}
164#endif
165
166void
167_gcry_log_info( const char *fmt, ... )
168{
169 va_list arg_ptr ;
170
171 va_start( arg_ptr, fmt ) ;
172 _gcry_logv( GCRY_LOG_INFO, fmt, arg_ptr );
173 va_end(arg_ptr);
174}
175
176void
177_gcry_log_error( const char *fmt, ... )
178{
179 va_list arg_ptr ;
180
181 va_start( arg_ptr, fmt ) ;
182 _gcry_logv( GCRY_LOG_ERROR, fmt, arg_ptr );
183 va_end(arg_ptr);
184}
185
186
187void
188_gcry_log_fatal( const char *fmt, ... )
189{
190 va_list arg_ptr ;
191
192 va_start( arg_ptr, fmt ) ;
193 _gcry_logv( GCRY_LOG_FATAL, fmt, arg_ptr );
194 va_end(arg_ptr);
195 abort(); /* never called, but it makes the compiler happy */
196}
197
198void
199_gcry_log_bug( const char *fmt, ... )
200{
201 va_list arg_ptr ;
202
203 va_start( arg_ptr, fmt ) ;
204 _gcry_logv( GCRY_LOG_BUG, fmt, arg_ptr );
205 va_end(arg_ptr);
206 abort(); /* never called, but it makes the compiler happy */
207}
208
209void
210_gcry_log_debug( const char *fmt, ... )
211{
212 va_list arg_ptr ;
213
214 va_start( arg_ptr, fmt ) ;
215 _gcry_logv( GCRY_LOG_DEBUG, fmt, arg_ptr );
216 va_end(arg_ptr);
217}
218
219void
220_gcry_log_printf (const char *fmt, ...)
221{
222 va_list arg_ptr;
223
224 if (fmt)
225 {
226 va_start( arg_ptr, fmt ) ;
227 _gcry_logv (GCRY_LOG_CONT, fmt, arg_ptr);
228 va_end(arg_ptr);
229 }
230}
231
232void
233_gcry_burn_stack (int bytes)
234{
235 char buf[64];
236
237 wipememory (buf, sizeof buf);
238 bytes -= sizeof buf;
239 if (bytes > 0)
240 _gcry_burn_stack (bytes);
241}
diff --git a/pwmanager/libcrypt/crypt/missing-string.c b/pwmanager/libcrypt/crypt/missing-string.c
new file mode 100644
index 0000000..80ff052
--- a/dev/null
+++ b/pwmanager/libcrypt/crypt/missing-string.c
@@ -0,0 +1,151 @@
1/* missing-string.c - missing string utilities
2 * Copyright (C) 1994, 1998, 1999, 2000, 2001,
3 * 2003 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 */
21
22#include <config.h>
23#include <stdlib.h>
24#include <string.h>
25#include <ctype.h>
26
27#include "g10lib.h"
28
29
30#ifndef HAVE_STPCPY
31char *
32stpcpy(char *a,const char *b)
33{
34 while( *b )
35 *a++ = *b++;
36 *a = 0;
37
38 return (char*)a;
39}
40#endif
41
42
43#ifndef HAVE_STRCASECMP
44int
45strcasecmp( const char *a, const char *b )
46{
47 for( ; *a && *b; a++, b++ ) {
48 if( *a != *b && toupper(*a) != toupper(*b) )
49 break;
50 }
51 return *(const byte*)a - *(const byte*)b;
52}
53#endif
54
55
56#ifdef __MINGW32__
57/*
58 * Like vsprintf but provides a pointer to malloc'd storage, which
59 * must be freed by the caller (gcry_free). Taken from libiberty as
60 * found in gcc-2.95.2 and a little bit modernized.
61 * FIXME: Write a new CRT for W32.
62 */
63int
64vasprintf ( char **result, const char *format, va_list args)
65{
66 const char *p = format;
67 /* Add one to make sure that it is never zero, which might cause malloc
68 to return NULL. */
69 int total_width = strlen (format) + 1;
70 va_list ap;
71
72 /* this is not really portable but works under Windows */
73 memcpy ( &ap, &args, sizeof (va_list));
74
75 while (*p != '\0')
76 {
77 if (*p++ == '%')
78 {
79 while (strchr ("-+ #0", *p))
80 ++p;
81 if (*p == '*')
82 {
83 ++p;
84 total_width += abs (va_arg (ap, int));
85 }
86 else
87 {
88 char *endp;
89 total_width += strtoul (p, &endp, 10);
90 p = endp;
91 }
92 if (*p == '.')
93 {
94 ++p;
95 if (*p == '*')
96 {
97 ++p;
98 total_width += abs (va_arg (ap, int));
99 }
100 else
101 {
102 char *endp;
103 total_width += strtoul (p, &endp, 10);
104 p = endp;
105 }
106 }
107 while (strchr ("hlL", *p))
108 ++p;
109 /* Should be big enough for any format specifier except %s
110 and floats. */
111 total_width += 30;
112 switch (*p)
113 {
114 case 'd':
115 case 'i':
116 case 'o':
117 case 'u':
118 case 'x':
119 case 'X':
120 case 'c':
121 (void) va_arg (ap, int);
122 break;
123 case 'f':
124 case 'e':
125 case 'E':
126 case 'g':
127 case 'G':
128 (void) va_arg (ap, double);
129 /* Since an ieee double can have an exponent of 307, we'll
130 make the buffer wide enough to cover the gross case. */
131 total_width += 307;
132
133 case 's':
134 total_width += strlen (va_arg (ap, char *));
135 break;
136 case 'p':
137 case 'n':
138 (void) va_arg (ap, char *);
139 break;
140 }
141 }
142 }
143 *result = gcry_malloc (total_width);
144 if (*result != NULL)
145 return vsprintf (*result, format, args);
146 else
147 return 0;
148}
149
150#endif /*__MINGW32__*/
151
diff --git a/pwmanager/libcrypt/crypt/module.c b/pwmanager/libcrypt/crypt/module.c
new file mode 100644
index 0000000..e98c36e
--- a/dev/null
+++ b/pwmanager/libcrypt/crypt/module.c
@@ -0,0 +1,200 @@
1/* module.c - Module management for libgcrypt.
2 * Copyright (C) 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <assert.h>
22#include <config.h>
23#include <errno.h>
24#include "g10lib.h"
25
26#define MODULE_ID_MIN 600
27
28/* Internal function. Generate a new, unique module ID for a module
29 that should be inserted into the module chain starting at
30 MODULES. */
31static gcry_err_code_t
32_gcry_module_id_new (gcry_module_t modules, unsigned int *id_new)
33{
34 /* FIXME, what should be the ID of the first module registered by
35 the user? */
36 unsigned int id_min = MODULE_ID_MIN, id_max = (unsigned int) -1, mod_id;
37 gcry_err_code_t err = GPG_ERR_NO_ERROR;
38 gcry_module_t module;
39
40 /* Search for unused ID. */
41 for (mod_id = id_min; mod_id < id_max; mod_id++)
42 {
43 /* Search for a module with the current ID. */
44 for (module = modules; module; module = module->next)
45 if (mod_id == module->mod_id)
46 break;
47
48 if (! module)
49 /* None found -> the ID is available for use. */
50 break;
51 }
52
53 if (mod_id < id_max)
54 /* Done. */
55 *id_new = mod_id;
56 else
57 /* No free ID found. */
58 err = GPG_ERR_INTERNAL;
59
60 return err;
61}
62
63/* Add a module specification to the list ENTRIES. The new module has
64 it's use-counter set to one. */
65gcry_err_code_t
66_gcry_module_add (gcry_module_t *entries, unsigned int mod_id,
67 void *spec, gcry_module_t *module)
68{
69 gcry_err_code_t err = 0;
70 gcry_module_t entry;
71
72 if (! mod_id)
73 err = _gcry_module_id_new (*entries, &mod_id);
74
75 if (! err)
76 {
77 entry = gcry_malloc (sizeof (struct gcry_module));
78 if (! entry)
79 err = gpg_err_code_from_errno (errno);
80 }
81
82 if (! err)
83 {
84 /* Fill new module entry. */
85 entry->flags = 0;
86 entry->counter = 1;
87 entry->spec = spec;
88 entry->mod_id = mod_id;
89
90 /* Link it into the list. */
91 entry->next = *entries;
92 entry->prevp = entries;
93 if (*entries)
94 (*entries)->prevp = &entry->next;
95 *entries = entry;
96
97 /* And give it to the caller. */
98 if (module)
99 *module = entry;
100 }
101 return err;
102}
103
104/* Internal function. Unlink CIPHER_ENTRY from the list of registered
105 ciphers and destroy it. */
106static void
107_gcry_module_drop (gcry_module_t entry)
108{
109 *entry->prevp = entry->next;
110 if (entry->next)
111 entry->next->prevp = entry->prevp;
112
113 gcry_free (entry);
114}
115
116/* Lookup a module specification by it's ID. After a successfull
117 lookup, the module has it's resource counter incremented. */
118gcry_module_t
119_gcry_module_lookup_id (gcry_module_t entries, unsigned int mod_id)
120{
121 gcry_module_t entry;
122
123 for (entry = entries; entry; entry = entry->next)
124 if (entry->mod_id == mod_id)
125 {
126 entry->counter++;
127 break;
128 }
129
130 return entry;
131}
132
133/* Lookup a module specification. After a successfull lookup, the
134 module has it's resource counter incremented. FUNC is a function
135 provided by the caller, which is responsible for identifying the
136 wanted module. */
137gcry_module_t
138_gcry_module_lookup (gcry_module_t entries, void *data,
139 gcry_module_lookup_t func)
140{
141 gcry_module_t entry;
142
143 for (entry = entries; entry; entry = entry->next)
144 if ((*func) (entry->spec, data))
145 {
146 entry->counter++;
147 break;
148 }
149
150 return entry;
151}
152
153/* Release a module. In case the use-counter reaches zero, destroy
154 the module. Passing MODULE as NULL is a dummy operation (similar
155 to free()). */
156void
157_gcry_module_release (gcry_module_t module)
158{
159 if (module && ! --module->counter)
160 _gcry_module_drop (module);
161}
162
163/* Add a reference to a module. */
164void
165_gcry_module_use (gcry_module_t module)
166{
167 ++module->counter;
168}
169
170/* If LIST is zero, write the number of modules identified by MODULES
171 to LIST_LENGTH and return. If LIST is non-zero, the first
172 *LIST_LENGTH algorithm IDs are stored in LIST, which must be of
173 according size. In case there are less cipher modules than
174 *LIST_LENGTH, *LIST_LENGTH is updated to the correct number. */
175gcry_err_code_t
176_gcry_module_list (gcry_module_t modules,
177 int *list, int *list_length)
178{
179 gcry_err_code_t err = GPG_ERR_NO_ERROR;
180 gcry_module_t module;
181 int length, i;
182
183 for (module = modules, length = 0; module; module = module->next, length++);
184
185 if (list)
186 {
187 if (length > *list_length)
188 length = *list_length;
189
190 for (module = modules, i = 0; i < length; module = module->next, i++)
191 list[i] = module->mod_id;
192
193 if (length < *list_length)
194 *list_length = length;
195 }
196 else
197 *list_length = length;
198
199 return err;
200}
diff --git a/pwmanager/libcrypt/crypt/mpi.h b/pwmanager/libcrypt/crypt/mpi.h
new file mode 100644
index 0000000..9e0037e
--- a/dev/null
+++ b/pwmanager/libcrypt/crypt/mpi.h
@@ -0,0 +1,199 @@
1/* mpi.h - Multi Precision Integers
2 * Copyright (C) 1994, 1996, 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 *
20 * Note: This code is heavily based on the GNU MP Library.
21 * Actually it's the same code with only minor changes in the
22 * way the data is stored; this is to support the abstraction
23 * of an optional secure memory allocation which may be used
24 * to avoid revealing of sensitive data due to paging etc.
25 */
26
27#ifndef G10_MPI_H
28#define G10_MPI_H
29
30#include <config.h>
31#include <stdio.h>
32#include "types.h"
33#include "memory.h"
34#include "../mpi/mpi-asm-defs.h"
35
36#include "g10lib.h"
37
38#ifndef _GCRYPT_IN_LIBGCRYPT
39#error this file should only be used inside libgcrypt
40#endif
41
42#ifndef BITS_PER_MPI_LIMB
43#if BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_INT
44 typedef unsigned int mpi_limb_t;
45 typedef signed int mpi_limb_signed_t;
46#elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG
47 typedef unsigned long int mpi_limb_t;
48 typedef signed long int mpi_limb_signed_t;
49#elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG_LONG
50 typedef unsigned long long int mpi_limb_t;
51 typedef signed long long int mpi_limb_signed_t;
52#elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_SHORT
53 typedef unsigned short int mpi_limb_t;
54 typedef signed short int mpi_limb_signed_t;
55#else
56#error BYTES_PER_MPI_LIMB does not match any C type
57#endif
58#define BITS_PER_MPI_LIMB (8*BYTES_PER_MPI_LIMB)
59#endif /*BITS_PER_MPI_LIMB*/
60
61#define DBG_MPI _gcry_get_debug_flag( 2 );
62
63struct gcry_mpi {
64 int alloced; /* array size (# of allocated limbs) */
65 int nlimbs; /* number of valid limbs */
66 int sign; /* indicates a negative number and is used for opaque
67 * MPIs to store the length */
68 unsigned flags; /* bit 0: array must be allocated in secure memory space */
69 /* bit 2: the limb is a pointer to some m_alloced data */
70 mpi_limb_t *d; /* array with the limbs */
71};
72
73#define MPI_NULL NULL
74
75#define mpi_get_nlimbs(a) ((a)->nlimbs)
76 #define mpi_is_neg(a) ((a)->sign)
77
78/*-- mpiutil.c --*/
79
80#ifdef M_DEBUG
81 #define mpi_alloc(n)_gcry_mpi_debug_alloc((n), M_DBGINFO( __LINE__ ) )
82#define mpi_alloc_secure(n) _gcry_mpi_debug_alloc_secure((n), M_DBGINFO( __LINE__ ) )
83 #define mpi_free(a)_gcry_mpi_debug_free((a), M_DBGINFO(__LINE__) )
84#define mpi_resize(a,b) _gcry_mpi_debug_resize((a),(b), M_DBGINFO(__LINE__) )
85 #define mpi_copy(a) _gcry_mpi_debug_copy((a), M_DBGINFO(__LINE__) )
86 gcry_mpi_t _gcry_mpi_debug_alloc( unsigned nlimbs, const char *info );
87 gcry_mpi_t _gcry_mpi_debug_alloc_secure( unsigned nlimbs, const char *info );
88 void _gcry_mpi_debug_free( gcry_mpi_t a, const char *info );
89 void _gcry_mpi_debug_resize( gcry_mpi_t a, unsigned nlimbs, const char *info );
90 gcry_mpi_t _gcry_mpi_debug_copy( gcry_mpi_t a, const char *info);
91#else
92 #define mpi_alloc(n) _gcry_mpi_alloc((n) )
93#define mpi_alloc_secure(n) _gcry_mpi_alloc_secure((n) )
94 #define mpi_free(a) _gcry_mpi_free((a) )
95#define mpi_resize(a,b) _gcry_mpi_resize((a),(b))
96 #define mpi_copy(a) _gcry_mpi_copy((a))
97 gcry_mpi_t _gcry_mpi_alloc( unsigned nlimbs );
98 gcry_mpi_t _gcry_mpi_alloc_secure( unsigned nlimbs );
99 void _gcry_mpi_free( gcry_mpi_t a );
100 void _gcry_mpi_resize( gcry_mpi_t a, unsigned nlimbs );
101 gcry_mpi_t _gcry_mpi_copy( gcry_mpi_t a );
102#endif
103#define mpi_is_opaque(a) ((a) && ((a)->flags&4))
104#define mpi_is_secure(a) ((a) && ((a)->flags&1))
105#define mpi_clear(a) _gcry_mpi_clear ((a))
106#define mpi_alloc_like(a) _gcry_mpi_alloc_like((a))
107#define mpi_set(a,b) _gcry_mpi_set ((a),(b))
108#define mpi_set_ui(a,b) _gcry_mpi_set_ui ((a),(b))
109#define mpi_alloc_set_ui(a) _gcry_mpi_alloc_set_ui ((a))
110#define mpi_m_check(a) _gcry_mpi_m_check ((a))
111#define mpi_swap(a,b) _gcry_mpi_swap ((a),(b))
112
113void _gcry_mpi_clear( gcry_mpi_t a );
114gcry_mpi_t _gcry_mpi_alloc_like( gcry_mpi_t a );
115void _gcry_mpi_set( gcry_mpi_t w, gcry_mpi_t u);
116void _gcry_mpi_set_ui( gcry_mpi_t w, ulong u);
117gcry_mpi_t _gcry_mpi_alloc_set_ui( unsigned long u);
118void _gcry_mpi_m_check( gcry_mpi_t a );
119void _gcry_mpi_swap( gcry_mpi_t a, gcry_mpi_t b);
120
121/*-- mpicoder.c --*/
122void _gcry_log_mpidump( const char *text, gcry_mpi_t a );
123u32 _gcry_mpi_get_keyid( gcry_mpi_t a, u32 *keyid );
124byte *_gcry_mpi_get_buffer( gcry_mpi_t a, unsigned *nbytes, int *sign );
125byte *_gcry_mpi_get_secure_buffer( gcry_mpi_t a, unsigned *nbytes, int *sign );
126void _gcry_mpi_set_buffer( gcry_mpi_t a, const byte *buffer, unsigned nbytes, int sign );
127
128#define log_mpidump _gcry_log_mpidump
129
130/*-- mpi-add.c --*/
131#define mpi_add_ui(w,u,v) gcry_mpi_add_ui((w),(u),(v))
132#define mpi_add(w,u,v) gcry_mpi_add ((w),(u),(v))
133#define mpi_addm(w,u,v,m) gcry_mpi_addm ((w),(u),(v),(m))
134#define mpi_sub_ui(w,u,v) gcry_mpi_sub_ui ((w),(u),(v))
135#define mpi_sub(w,u,v) gcry_mpi_sub ((w),(u),(v))
136#define mpi_subm(w,u,v,m) gcry_mpi_subm ((w),(u),(v),(m))
137
138
139/*-- mpi-mul.c --*/
140#define mpi_mul_ui(w,u,v) gcry_mpi_mul_ui ((w),(u),(v))
141#define mpi_mul_2exp(w,u,v) gcry_mpi_mul_2exp ((w),(u),(v))
142#define mpi_mul(w,u,v) gcry_mpi_mul ((w),(u),(v))
143#define mpi_mulm(w,u,v,m) gcry_mpi_mulm ((w),(u),(v),(m))
144
145
146/*-- mpi-div.c --*/
147#define mpi_fdiv_r_ui(a,b,c) _gcry_mpi_fdiv_r_ui((a),(b),(c))
148#define mpi_fdiv_r(a,b,c) _gcry_mpi_fdiv_r((a),(b),(c))
149#define mpi_fdiv_q(a,b,c) _gcry_mpi_fdiv_q((a),(b),(c))
150#define mpi_fdiv_qr(a,b,c,d) _gcry_mpi_fdiv_qr((a),(b),(c),(d))
151#define mpi_tdiv_r(a,b,c) _gcry_mpi_tdiv_r((a),(b),(c))
152#define mpi_tdiv_qr(a,b,c,d) _gcry_mpi_tdiv_qr((a),(b),(c),(d))
153#define mpi_tdiv_q_2exp(a,b,c) _gcry_mpi_tdiv_q_2exp((a),(b),(c))
154#define mpi_divisible_ui(a,b) _gcry_mpi_divisible_ui((a),(b))
155ulong _gcry_mpi_fdiv_r_ui( gcry_mpi_t rem, gcry_mpi_t dividend, ulong divisor );
156void _gcry_mpi_fdiv_r( gcry_mpi_t rem, gcry_mpi_t dividend, gcry_mpi_t divisor );
157void _gcry_mpi_fdiv_q( gcry_mpi_t quot, gcry_mpi_t dividend, gcry_mpi_t divisor );
158void _gcry_mpi_fdiv_qr( gcry_mpi_t quot, gcry_mpi_t rem, gcry_mpi_t dividend, gcry_mpi_t divisor );
159void _gcry_mpi_tdiv_r( gcry_mpi_t rem, gcry_mpi_t num, gcry_mpi_t den);
160void _gcry_mpi_tdiv_qr( gcry_mpi_t quot, gcry_mpi_t rem, gcry_mpi_t num, gcry_mpi_t den);
161void _gcry_mpi_tdiv_q_2exp( gcry_mpi_t w, gcry_mpi_t u, unsigned count );
162int _gcry_mpi_divisible_ui(gcry_mpi_t dividend, ulong divisor );
163
164/*-- mpi-gcd.c --*/
165
166/*-- mpi-mpow.c --*/
167#define mpi_mulpowm(a,b,c,d) _gcry_mpi_mulpowm ((a),(b),(c),(d))
168void _gcry_mpi_mulpowm( gcry_mpi_t res, gcry_mpi_t *basearray, gcry_mpi_t *exparray, gcry_mpi_t mod);
169
170/*-- mpi-cmp.c --*/
171#define mpi_cmp_ui(a,b) gcry_mpi_cmp_ui ((a),(b))
172#define mpi_cmp(a,b) gcry_mpi_cmp ((a),(b))
173int gcry_mpi_cmp_ui( gcry_mpi_t u, ulong v );
174int gcry_mpi_cmp( gcry_mpi_t u, gcry_mpi_t v );
175
176/*-- mpi-scan.c --*/
177#define mpi_trailing_zeros(a) _gcry_mpi_trailing_zeros ((a))
178int _gcry_mpi_getbyte( gcry_mpi_t a, unsigned idx );
179void _gcry_mpi_putbyte( gcry_mpi_t a, unsigned idx, int value );
180unsigned _gcry_mpi_trailing_zeros( gcry_mpi_t a );
181
182/*-- mpi-bit.c --*/
183#define mpi_normalize(a) _gcry_mpi_normalize ((a))
184#define mpi_get_nbits(a) gcry_mpi_get_nbits ((a))
185#define mpi_test_bit(a,b) gcry_mpi_test_bit ((a),(b))
186#define mpi_set_bit(a,b) gcry_mpi_set_bit ((a),(b))
187#define mpi_set_highbit(a,b) gcry_mpi_set_highbit ((a),(b))
188#define mpi_clear_bit(a,b) gcry_mpi_clear_bit ((a),(b))
189#define mpi_clear_highbit(a,b) gcry_mpi_clear_highbit ((a),(b))
190#define mpi_rshift(a,b,c) gcry_mpi_rshift ((a),(b),(c))
191
192void _gcry_mpi_normalize( gcry_mpi_t a );
193
194/*-- mpi-inv.c --*/
195#define mpi_invm(a,b,c) _gcry_mpi_invm ((a),(b),(c))
196void _gcry_mpi_invm( gcry_mpi_t x, gcry_mpi_t u, gcry_mpi_t v );
197
198
199#endif /*G10_MPI_H*/
diff --git a/pwmanager/libcrypt/crypt/secmem.c b/pwmanager/libcrypt/crypt/secmem.c
new file mode 100644
index 0000000..163bf20
--- a/dev/null
+++ b/pwmanager/libcrypt/crypt/secmem.c
@@ -0,0 +1,653 @@
1 /* secmem.c -memory allocation from a secure heap
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002,
3 * 2003 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser general Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 */
21
22#include <config.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <errno.h>
27#include <stdarg.h>
28#include <unistd.h>
29#include <stddef.h>
30
31#if defined(HAVE_MLOCK) || defined(HAVE_MMAP)
32#include <sys/mman.h>
33#include <sys/types.h>
34#include <fcntl.h>
35#ifdef USE_CAPABILITIES
36#include <sys/capability.h>
37#endif
38#endif
39
40#include "ath.h"
41#include "g10lib.h"
42#include "secmem.h"
43
44#if defined (MAP_ANON) && ! defined (MAP_ANONYMOUS)
45#define MAP_ANONYMOUS MAP_ANON
46#endif
47
48#define DEFAULT_POOL_SIZE 16384
49#define DEFAULT_PAGE_SIZE 4096
50
51typedef struct memblock
52{
53 unsigned size; /* Size of the memory available to the
54 user. */
55 int flags; /* See below. */
56 PROPERLY_ALIGNED_TYPE aligned;
57} memblock_t;
58
59/* This flag specifies that the memory block is in use. */
60#define MB_FLAG_ACTIVE 1 << 0
61
62/* The pool of secure memory. */
63static void *pool;
64
65/* Size of POOL in bytes. */
66static size_t pool_size;
67
68/* True, if the memory pool is ready for use. May be checked in an
69 atexit function. */
70static volatile int pool_okay;
71
72/* True, if the memory pool is mmapped. */
73static volatile int pool_is_mmapped;
74
75/* FIXME? */
76static int disable_secmem;
77static int show_warning;
78static int no_warning;
79static int suspend_warning;
80
81/* Stats. */
82static unsigned int cur_alloced, cur_blocks;
83
84/* Lock protecting accesses to the memory pool. */
85static ath_mutex_t secmem_lock;
86
87/* Convenient macros. */
88#define SECMEM_LOCK ath_mutex_lock (&secmem_lock)
89#define SECMEM_UNLOCK ath_mutex_unlock (&secmem_lock)
90
91/* The size of the memblock structure; this does not include the
92 memory that is available to the user. */
93#define BLOCK_HEAD_SIZE \
94 offsetof (memblock_t, aligned)
95
96/* Convert an address into the according memory block structure. */
97#define ADDR_TO_BLOCK(addr) \
98 (memblock_t *) ((char *) addr - BLOCK_HEAD_SIZE)
99
100/* Check wether MB is a valid block. */
101#define BLOCK_VALID(mb) \
102 (((char *) mb - (char *) pool) < pool_size)
103
104/* Update the stats. */
105static void
106stats_update (size_t add, size_t sub)
107{
108 if (add)
109 {
110 cur_alloced += add;
111 cur_blocks++;
112 }
113 if (sub)
114 {
115 cur_alloced -= sub;
116 cur_blocks--;
117 }
118}
119
120/* Return the block following MB or NULL, if MB is the last block. */
121static memblock_t *
122mb_get_next (memblock_t *mb)
123{
124 memblock_t *mb_next;
125
126 mb_next = (memblock_t *) ((char *) mb + BLOCK_HEAD_SIZE + mb->size);
127
128 if (! BLOCK_VALID (mb_next))
129 mb_next = NULL;
130
131 return mb_next;
132}
133
134/* Return the block preceeding MB or NULL, if MB is the first
135 block. */
136static memblock_t *
137mb_get_prev (memblock_t *mb)
138{
139 memblock_t *mb_prev, *mb_next;
140
141 if (mb == pool)
142 mb_prev = NULL;
143 else
144 {
145 mb_prev = (memblock_t *) pool;
146 while (1)
147 {
148 mb_next = mb_get_next (mb_prev);
149 if (mb_next == mb)
150 break;
151 else
152 mb_prev = mb_next;
153 }
154 }
155
156 return mb_prev;
157}
158
159/* If the preceeding block of MB and/or the following block of MB
160 exist and are not active, merge them to form a bigger block. */
161static void
162mb_merge (memblock_t *mb)
163{
164 memblock_t *mb_prev, *mb_next;
165
166 mb_prev = mb_get_prev (mb);
167 mb_next = mb_get_next (mb);
168
169 if (mb_prev && (! (mb_prev->flags & MB_FLAG_ACTIVE)))
170 {
171 mb_prev->size += BLOCK_HEAD_SIZE + mb->size;
172 mb = mb_prev;
173 }
174 if (mb_next && (! (mb_next->flags & MB_FLAG_ACTIVE)))
175 mb->size += BLOCK_HEAD_SIZE + mb_next->size;
176}
177
178/* Return a new block, which can hold SIZE bytes. */
179static memblock_t *
180mb_get_new (memblock_t *block, size_t size)
181{
182 memblock_t *mb, *mb_split;
183
184 for (mb = block; BLOCK_VALID (mb); mb = mb_get_next (mb))
185 if (! (mb->flags & MB_FLAG_ACTIVE) && mb->size >= size)
186 {
187 /* Found a free block. */
188 mb->flags |= MB_FLAG_ACTIVE;
189
190 if (mb->size - size > BLOCK_HEAD_SIZE)
191 {
192 /* Split block. */
193
194 mb_split = (memblock_t *) (((char *) mb) + BLOCK_HEAD_SIZE + size);
195 mb_split->size = mb->size - size - BLOCK_HEAD_SIZE;
196 mb_split->flags = 0;
197
198 mb->size = size;
199
200 mb_merge (mb_split);
201
202 }
203
204 break;
205 }
206
207 if (! BLOCK_VALID (mb))
208 mb = NULL;
209
210 return mb;
211}
212
213/* Print a warning message. */
214static void
215print_warn (void)
216{
217 if (!no_warning)
218 log_info (_("Warning: using insecure memory!\n"));
219}
220
221/* Lock the memory pages into core and drop privileges. */
222static void
223lock_pool (void *p, size_t n)
224{
225#if defined(USE_CAPABILITIES) && defined(HAVE_MLOCK)
226 int err;
227
228 cap_set_proc (cap_from_text ("cap_ipc_lock+ep"));
229 err = mlock (p, n);
230 if (err && errno)
231 err = errno;
232 cap_set_proc (cap_from_text ("cap_ipc_lock+p"));
233
234 if (err)
235 {
236 if (errno != EPERM
237 #ifdef EAGAIN/* OpenBSD returns this */
238 && errno != EAGAIN
239#endif
240 #ifdef ENOSYS/* Some SCOs return this (function not implemented) */
241 && errno != ENOSYS
242#endif
243#ifdef ENOMEM /* Linux might return this. */
244 && errno != ENOMEM
245#endif
246 )
247 log_error ("can't lock memory: %s\n", strerror (err));
248 show_warning = 1;
249 }
250
251#elif defined(HAVE_MLOCK)
252 uid_t uid;
253 int err;
254
255 uid = getuid ();
256
257#ifdef HAVE_BROKEN_MLOCK
258 /* Under HP/UX mlock segfaults if called by non-root. Note, we have
259 noch checked whether mlock does really work under AIX where we
260 also detected a broken nlock. Note further, that using plock ()
261 is not a good idea under AIX. */
262 if (uid)
263 {
264 errno = EPERM;
265 err = errno;
266 }
267 else
268 {
269 err = mlock (p, n);
270 if (err && errno)
271 err = errno;
272 }
273#else /* !HAVE_BROKEN_MLOCK */
274 err = mlock (p, n);
275 if (err && errno)
276 err = errno;
277#endif /* !HAVE_BROKEN_MLOCK */
278
279 if (uid && ! geteuid ())
280 {
281 /* check that we really dropped the privs.
282 * Note: setuid(0) should always fail */
283 if (setuid (uid) || getuid () != geteuid () || !setuid (0))
284 log_fatal ("failed to reset uid: %s\n", strerror (errno));
285 }
286
287 if (err)
288 {
289 if (errno != EPERM
290 #ifdef EAGAIN/* OpenBSD returns this. */
291 && errno != EAGAIN
292#endif
293 #ifdef ENOSYS/* Some SCOs return this (function not implemented). */
294 && errno != ENOSYS
295#endif
296#ifdef ENOMEM /* Linux might return this. */
297 && errno != ENOMEM
298#endif
299 )
300 log_error ("can't lock memory: %s\n", strerror (err));
301 show_warning = 1;
302 }
303
304#elif defined ( __QNX__ )
305 /* QNX does not page at all, so the whole secure memory stuff does
306 * not make much sense. However it is still of use because it
307 * wipes out the memory on a free().
308 * Therefore it is sufficient to suppress the warning
309 */
310#elif defined (HAVE_DOSISH_SYSTEM) || defined (__CYGWIN__)
311 /* It does not make sense to print such a warning, given the fact that
312 * this whole Windows !@#$% and their user base are inherently insecure
313 */
314#elif defined (__riscos__)
315 /* no virtual memory on RISC OS, so no pages are swapped to disc,
316 * besides we don't have mmap, so we don't use it! ;-)
317 * But don't complain, as explained above.
318 */
319#else
320 log_info ("Please note that you don't have secure memory on this system\n");
321#endif
322}
323
324/* Initialize POOL. */
325static void
326init_pool (size_t n)
327{
328 size_t pgsize;
329 memblock_t *mb;
330
331 pool_size = n;
332
333 if (disable_secmem)
334 log_bug ("secure memory is disabled");
335
336#ifdef HAVE_GETPAGESIZE
337 pgsize = getpagesize ();
338#else
339 pgsize = DEFAULT_PAGE_SIZE;
340#endif
341
342#if HAVE_MMAP
343 pool_size = (pool_size + pgsize - 1) & ~(pgsize - 1);
344#ifdef MAP_ANONYMOUS
345 pool = mmap (0, pool_size, PROT_READ | PROT_WRITE,
346 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
347#else /* map /dev/zero instead */
348 {
349 int fd;
350
351 fd = open ("/dev/zero", O_RDWR);
352 if (fd == -1)
353 {
354 log_error ("can't open /dev/zero: %s\n", strerror (errno));
355 pool = (void *) -1;
356 }
357 else
358 {
359 pool = mmap (0, pool_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
360 }
361 }
362#endif
363 if (pool == (void *) -1)
364 log_info ("can't mmap pool of %u bytes: %s - using malloc\n",
365 (unsigned) pool_size, strerror (errno));
366 else
367 {
368 pool_is_mmapped = 1;
369 pool_okay = 1;
370 }
371
372#endif
373 if (!pool_okay)
374 {
375 pool = malloc (pool_size);
376 if (!pool)
377 log_fatal ("can't allocate memory pool of %u bytes\n",
378 (unsigned) pool_size);
379 else
380 pool_okay = 1;
381 }
382
383 /* Initialize first memory block. */
384 mb = (memblock_t *) pool;
385 mb->size = pool_size;
386 mb->flags = 0;
387}
388
389void
390_gcry_secmem_set_flags (unsigned flags)
391{
392 int was_susp;
393
394 SECMEM_LOCK;
395
396 was_susp = suspend_warning;
397 no_warning = flags & GCRY_SECMEM_FLAG_NO_WARNING;
398 suspend_warning = flags & GCRY_SECMEM_FLAG_SUSPEND_WARNING;
399
400 /* and now issue the warning if it is not longer suspended */
401 if (was_susp && !suspend_warning && show_warning)
402 {
403 show_warning = 0;
404 print_warn ();
405 }
406
407 SECMEM_UNLOCK;
408}
409
410unsigned
411_gcry_secmem_get_flags (void)
412{
413 unsigned flags;
414
415 SECMEM_LOCK;
416
417 flags = no_warning ? GCRY_SECMEM_FLAG_NO_WARNING : 0;
418 flags |= suspend_warning ? GCRY_SECMEM_FLAG_SUSPEND_WARNING : 0;
419
420 SECMEM_UNLOCK;
421
422 return flags;
423}
424
425/* Initialize the secure memory system. If running with the necessary
426 privileges, the secure memory pool will be locked into the core in
427 order to prevent page-outs of the data. Furthermore allocated
428 secure memory will be wiped out when released. */
429void
430_gcry_secmem_init (size_t n)
431{
432 SECMEM_LOCK;
433
434 if (!n)
435 {
436#ifdef USE_CAPABILITIES
437 /* drop all capabilities */
438 cap_set_proc (cap_from_text ("all-eip"));
439
440#elif !defined(HAVE_DOSISH_SYSTEM)
441 uid_t uid;
442
443 disable_secmem = 1;
444 uid = getuid ();
445 if (uid != geteuid ())
446 {
447 if (setuid (uid) || getuid () != geteuid () || !setuid (0))
448 log_fatal ("failed to drop setuid\n");
449 }
450#endif
451 }
452 else
453 {
454 if (n < DEFAULT_POOL_SIZE)
455 n = DEFAULT_POOL_SIZE;
456 if (! pool_okay)
457 {
458 init_pool (n);
459 if (! geteuid ())
460 lock_pool (pool, n);
461 else if (!no_warning)
462 log_info ("Secure memory is not locked into core\n");
463 }
464 else
465 log_error ("Oops, secure memory pool already initialized\n");
466 }
467
468 SECMEM_UNLOCK;
469}
470
471
472static void *
473_gcry_secmem_malloc_internal (size_t size)
474{
475 memblock_t *mb;
476
477 if (!pool_okay)
478 {
479 log_info (_
480 ("operation is not possible without initialized secure memory\n"));
481 exit (2);
482 }
483 if (show_warning && !suspend_warning)
484 {
485 show_warning = 0;
486 print_warn ();
487 }
488
489 /* Blocks are always a multiple of 32. */
490 size = ((size + 31) / 32) * 32;
491
492 mb = mb_get_new ((memblock_t *) pool, size);
493 if (mb)
494 stats_update (size, 0);
495
496 return mb ? &mb->aligned.c : NULL;
497}
498
499void *
500_gcry_secmem_malloc (size_t size)
501{
502 void *p;
503
504 SECMEM_LOCK;
505 p = _gcry_secmem_malloc_internal (size);
506 SECMEM_UNLOCK;
507
508 return p;
509}
510
511static void
512_gcry_secmem_free_internal (void *a)
513{
514 memblock_t *mb;
515 int size;
516
517 if (!a)
518 return;
519
520 mb = ADDR_TO_BLOCK (a);
521 size = mb->size;
522
523 /* This does not make much sense: probably this memory is held in the
524 * cache. We do it anyway: */
525#define MB_WIPE_OUT(byte) \
526 memset ((memblock_t *) ((char *) mb + BLOCK_HEAD_SIZE), (byte), size);
527
528 MB_WIPE_OUT (0xff);
529 MB_WIPE_OUT (0xaa);
530 MB_WIPE_OUT (0x55);
531 MB_WIPE_OUT (0x00);
532
533 stats_update (0, size);
534
535 mb->flags &= ~MB_FLAG_ACTIVE;
536
537 /* Update stats. */
538
539 mb_merge (mb);
540}
541
542/* Wipe out and release memory. */
543void
544_gcry_secmem_free (void *a)
545{
546 SECMEM_LOCK;
547 _gcry_secmem_free_internal (a);
548 SECMEM_UNLOCK;
549}
550
551/* Realloc memory. */
552void *
553_gcry_secmem_realloc (void *p, size_t newsize)
554{
555 memblock_t *mb;
556 size_t size;
557 void *a;
558
559 SECMEM_LOCK;
560
561 mb = (memblock_t *) ((char *) p - ((size_t) &((memblock_t *) 0)->aligned.c));
562 size = mb->size;
563 if (newsize < size)
564 {
565 /* It is easier to not shrink the memory. */
566 a = p;
567 }
568 else
569 {
570 a = _gcry_secmem_malloc_internal (newsize);
571 if (a)
572 {
573 memcpy (a, p, size);
574 memset ((char *) a + size, 0, newsize - size);
575 _gcry_secmem_free_internal (p);
576 }
577 }
578
579 SECMEM_UNLOCK;
580
581 return a;
582}
583
584int
585_gcry_private_is_secure (const void *p)
586{
587 int ret = 0;
588
589 SECMEM_LOCK;
590
591 if (pool_okay && BLOCK_VALID (ADDR_TO_BLOCK (p)))
592 ret = 1;
593
594 SECMEM_UNLOCK;
595
596 return ret;
597}
598
599
600/****************
601 * Warning: This code might be called by an interrupt handler
602 * and frankly, there should really be such a handler,
603 * to make sure that the memory is wiped out.
604 * We hope that the OS wipes out mlocked memory after
605 * receiving a SIGKILL - it really should do so, otherwise
606 * there is no chance to get the secure memory cleaned.
607 */
608void
609_gcry_secmem_term ()
610{
611 if (!pool_okay)
612 return;
613
614 wipememory2 (pool, 0xff, pool_size);
615 wipememory2 (pool, 0xaa, pool_size);
616 wipememory2 (pool, 0x55, pool_size);
617 wipememory2 (pool, 0x00, pool_size);
618#if HAVE_MMAP
619 if (pool_is_mmapped)
620 munmap (pool, pool_size);
621#endif
622 pool = NULL;
623 pool_okay = 0;
624 pool_size = 0;
625}
626
627
628void
629_gcry_secmem_dump_stats ()
630{
631#if 1
632 SECMEM_LOCK;
633
634 if (pool_okay)
635 log_info ("secmem usage: %u/%lu bytes in %u blocks\n",
636 cur_alloced, (unsigned long)pool_size, cur_blocks);
637 SECMEM_UNLOCK;
638#else
639 memblock_t *mb;
640 int i;
641
642 SECMEM_LOCK;
643
644 for (i = 0, mb = (memblock_t *) pool;
645 BLOCK_VALID (mb);
646 mb = mb_get_next (mb), i++)
647 log_info ("SECMEM: [%s] block: %i; size: %i\n",
648 (mb->flags & MB_FLAG_ACTIVE) ? "used" : "free",
649 i,
650 mb->size);
651 SECMEM_UNLOCK;
652#endif
653}
diff --git a/pwmanager/libcrypt/crypt/secmem.h b/pwmanager/libcrypt/crypt/secmem.h
new file mode 100644
index 0000000..54e16ba
--- a/dev/null
+++ b/pwmanager/libcrypt/crypt/secmem.h
@@ -0,0 +1,38 @@
1/* secmem.h - internal definitions for secmem
2 *Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#ifndef G10_SECMEM_H
22#define G10_SECMEM_H 1
23
24void _gcry_secmem_init (size_t npool);
25void _gcry_secmem_term (void);
26void *_gcry_secmem_malloc (size_t size) GCC_ATTR_MALLOC;
27void *_gcry_secmem_realloc (void *a, size_t newsize);
28void _gcry_secmem_free (void *a);
29void _gcry_secmem_dump_stats (void);
30void _gcry_secmem_set_flags (unsigned flags);
31unsigned _gcry_secmem_get_flags(void) GCC_ATTR_PURE;
32int _gcry_private_is_secure (const void *p) GCC_ATTR_PURE;
33
34/* Flags for _gcry_secmem_{set,get}_flags. */
35#define GCRY_SECMEM_FLAG_NO_WARNING 1 << 0
36#define GCRY_SECMEM_FLAG_SUSPEND_WARNING 1 << 1
37
38#endif /* G10_SECMEM_H */
diff --git a/pwmanager/libcrypt/crypt/sexp.c b/pwmanager/libcrypt/crypt/sexp.c
new file mode 100644
index 0000000..c19dc33
--- a/dev/null
+++ b/pwmanager/libcrypt/crypt/sexp.c
@@ -0,0 +1,1804 @@
1/* sexp.c - S-Expression handling
2 * Copyright (C) 1999, 2000, 2001, 2002, 2003,
3 * 2004 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser general Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 */
21
22
23#include <config.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <stdarg.h>
28#include <ctype.h>
29#include <assert.h>
30
31#define GCRYPT_NO_MPI_MACROS 1
32#include "g10lib.h"
33#include "memory.h"
34
35typedef struct gcry_sexp *NODE;
36typedef unsigned short DATALEN;
37
38struct gcry_sexp
39{
40 byte d[1];
41};
42
43#define ST_STOP 0
44#define ST_DATA 1 /* datalen follows */
45#define ST_HINT 2 /* datalen follows */
46#define ST_OPEN 3
47#define ST_CLOSE 4
48
49/* the atoi macros assume that the buffer has only valid digits */
50#define atoi_1(p) (*(p) - '0' )
51#define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
52 *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
53#define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
54
55#define TOKEN_SPECIALS "-./_:*+="
56
57static gcry_error_t
58sexp_sscan (gcry_sexp_t *retsexp, size_t *erroff,
59 const char *buffer, size_t length, int argflag,
60 va_list arg_ptr, void **arg_list);
61
62#if 0
63static void
64dump_mpi( gcry_mpi_t a )
65{
66 char buffer[1000];
67 size_t n = 1000;
68
69 if( !a )
70 fputs("[no MPI]", stderr );
71 else if( gcry_mpi_print( GCRYMPI_FMT_HEX, buffer, &n, a ) )
72 fputs("[MPI too large to print]", stderr );
73 else
74 fputs( buffer, stderr );
75}
76#endif
77
78static void
79dump_string (const byte *p, size_t n, int delim )
80{
81 for (; n; n--, p++ )
82 {
83 if ((*p & 0x80) || iscntrl( *p ) || *p == delim )
84 {
85 if( *p == '\n' )
86 log_printf ("\\n");
87 else if( *p == '\r' )
88 log_printf ("\\r");
89 else if( *p == '\f' )
90 log_printf ("\\f");
91 else if( *p == '\v' )
92 log_printf ("\\v");
93 else if( *p == '\b' )
94 log_printf ("\\b");
95 else if( !*p )
96 log_printf ("\\0");
97 else
98 log_printf ("\\x%02x", *p );
99 }
100 else
101 log_printf ("%c", *p);
102 }
103}
104
105
106void
107gcry_sexp_dump (const gcry_sexp_t a)
108{
109 const byte *p;
110 int indent = 0;
111 int type;
112
113 if (!a)
114 {
115 log_printf ( "[nil]\n");
116 return;
117 }
118
119 p = a->d;
120 while ( (type = *p) != ST_STOP )
121 {
122 p++;
123 switch ( type )
124 {
125 case ST_OPEN:
126 log_printf ("%*s[open]\n", 2*indent, "");
127 indent++;
128 break;
129 case ST_CLOSE:
130 if( indent )
131 indent--;
132 log_printf ("%*s[close]\n", 2*indent, "");
133 break;
134 case ST_DATA: {
135 DATALEN n;
136 memcpy ( &n, p, sizeof n );
137 p += sizeof n;
138 log_printf ("%*s[data=\"", 2*indent, "" );
139 dump_string (p, n, '\"' );
140 log_printf ("\"]\n");
141 p += n;
142 }
143 break;
144 default:
145 log_printf ("%*s[unknown tag %d]\n", 2*indent, "", type);
146 break;
147 }
148 }
149}
150
151/****************
152 * Pass list through except when it is an empty list - in that case
153 * return NULL and release the passed list.
154 */
155static gcry_sexp_t
156normalize ( gcry_sexp_t list )
157{
158 char *p;
159 if ( !list )
160 return NULL;
161 p = list->d;
162 if ( *p == ST_STOP ) {
163 /* this is "" */
164 gcry_sexp_release ( list );
165 return NULL;
166 }
167 if( *p == ST_OPEN && p[1] == ST_CLOSE ) {
168 /* this is "()" */
169 gcry_sexp_release ( list );
170 return NULL;
171 }
172
173 return list;
174}
175
176/* Create a new S-expression object by reading LENGTH bytes from
177 BUFFER, assuming it is canonilized encoded or autodetected encoding
178 when AUTODETECT is set to 1. With FREEFNC not NULL, ownership of
179 the buffer is transferred to tyhe newle created object. FREEFNC
180 should be the freefnc used to release BUFFER; there is no guarantee
181 at which point this function is called; most likey you want to use
182 free() or gcry_free().
183
184 Passing LENGTH and AUTODETECT as 0 is allowed to indicate that
185 BUFFER points to a valid canonical encoded S-expression. A LENGTH
186 of 0 and AUTODETECT 1 indicates that buffer points to a
187 null-terminated string.
188
189 This function returns 0 and and the pointer to the new object in
190 RETSEXP or an error code in which case RETSEXP is set to NULL. */
191gcry_error_t
192gcry_sexp_create (gcry_sexp_t *retsexp, void *buffer, size_t length,
193 int autodetect, void (*freefnc)(void*) )
194{
195 gcry_error_t errcode;
196 gcry_sexp_t se;
197 volatile va_list dummy_arg_ptr;
198
199 if (!retsexp)
200 return gcry_error (GPG_ERR_INV_ARG);
201 *retsexp = NULL;
202 if (autodetect < 0 || autodetect > 1 || !buffer)
203 return gcry_error (GPG_ERR_INV_ARG);
204
205 if (!length && !autodetect)
206 { /* What a brave caller to assume that there is really a canonical
207 encoded S-expression in buffer */
208 length = gcry_sexp_canon_len (buffer, 0, NULL, &errcode);
209 if (!length)
210 return errcode;
211 }
212 else if (!length && autodetect)
213 { /* buffer is a string */
214 length = strlen ((char *)buffer);
215 }
216
217 errcode = sexp_sscan (&se, NULL, buffer, length, 0, dummy_arg_ptr, NULL);
218 if (errcode)
219 return errcode;
220
221 *retsexp = se;
222 if (freefnc)
223 {
224 /* For now we release the buffer immediately. As soon as we
225 have changed the internal represenation of S-expression to
226 the canoncial format - which has the advantage of faster
227 parsing - we will use this function as a closure in our
228 GCRYSEXP object and use the BUFFER directly */
229 freefnc (buffer);
230 }
231 return gcry_error (GPG_ERR_NO_ERROR);
232}
233
234/* Same as gcry_sexp_create but don't transfer ownership */
235gcry_error_t
236gcry_sexp_new (gcry_sexp_t *retsexp, const void *buffer, size_t length,
237 int autodetect)
238{
239 return gcry_sexp_create (retsexp, (void *)buffer, length, autodetect, NULL);
240}
241
242
243/****************
244 * Release resource of the given SEXP object.
245 */
246void
247gcry_sexp_release( gcry_sexp_t sexp )
248{
249 if (sexp)
250 {
251 if (gcry_is_secure (sexp))
252 {
253 /* Extra paranoid wiping. */
254 const byte *p = sexp->d;
255 int type;
256
257 while ( (type = *p) != ST_STOP )
258 {
259 p++;
260 switch ( type )
261 {
262 case ST_OPEN:
263 break;
264 case ST_CLOSE:
265 break;
266 case ST_DATA:
267 {
268 DATALEN n;
269 memcpy ( &n, p, sizeof n );
270 p += sizeof n;
271 p += n;
272 }
273 break;
274 default:
275 break;
276 }
277 }
278 wipememory (sexp->d, p - sexp->d);
279 }
280 gcry_free ( sexp );
281 }
282}
283
284
285/****************
286 * Make a pair from lists a and b, don't use a or b later on.
287 * Special behaviour: If one is a single element list we put the
288 * element straight into the new pair.
289 */
290gcry_sexp_t
291gcry_sexp_cons( const gcry_sexp_t a, const gcry_sexp_t b )
292{
293 /* NYI: Implementation should be quite easy with our new data
294 representation */
295 BUG ();
296 return NULL;
297}
298
299
300/****************
301 * Make a list from all items in the array the end of the array is marked
302 * with a NULL.
303 */
304gcry_sexp_t
305gcry_sexp_alist( const gcry_sexp_t *array )
306{
307 /* NYI: Implementation should be quite easy with our new data
308 representation. */
309 BUG ();
310 return NULL;
311}
312
313/****************
314 * Make a list from all items, the end of list is indicated by a NULL
315 */
316gcry_sexp_t
317gcry_sexp_vlist( const gcry_sexp_t a, ... )
318{
319 /* NYI: Implementation should be quite easy with our new data
320 representation. */
321 BUG ();
322 return NULL;
323}
324
325
326/****************
327 * Append n to the list a
328 * Returns: a new ist (which maybe a)
329 */
330gcry_sexp_t
331gcry_sexp_append( const gcry_sexp_t a, const gcry_sexp_t n )
332{
333 /* NYI: Implementation should be quite easy with our new data
334 representation. */
335 BUG ();
336 return NULL;
337}
338
339gcry_sexp_t
340gcry_sexp_prepend( const gcry_sexp_t a, const gcry_sexp_t n )
341{
342 /* NYI: Implementation should be quite easy with our new data
343 representation. */
344 BUG ();
345 return NULL;
346}
347
348
349
350/****************
351 * Locate token in a list. The token must be the car of a sublist.
352 * Returns: A new list with this sublist or NULL if not found.
353 */
354gcry_sexp_t
355gcry_sexp_find_token( const gcry_sexp_t list, const char *tok, size_t toklen )
356{
357 const byte *p;
358 DATALEN n;
359
360 if ( !list )
361 return NULL;
362
363 if( !toklen )
364 toklen = strlen(tok);
365 p = list->d;
366 while ( *p != ST_STOP ) {
367 if ( *p == ST_OPEN && p[1] == ST_DATA ) {
368 const byte *head = p;
369
370 p += 2;
371 memcpy ( &n, p, sizeof n ); p += sizeof n;
372 if ( n == toklen && !memcmp( p, tok, toklen ) ) { /* found it */
373 gcry_sexp_t newlist;
374 byte *d;
375 int level = 1;
376
377 /* look for the end of the list */
378 for ( p += n; level; p++ ) {
379 if ( *p == ST_DATA ) {
380 memcpy ( &n, ++p, sizeof n );
381 p += sizeof n + n;
382 p--; /* compensate for later increment */
383 }
384 else if ( *p == ST_OPEN ) {
385 level++;
386 }
387 else if ( *p == ST_CLOSE ) {
388 level--;
389 }
390 else if ( *p == ST_STOP ) {
391 BUG ();
392 }
393 } while ( level );
394 n = p - head;
395
396 newlist = gcry_xmalloc ( sizeof *newlist + n );
397 d = newlist->d;
398 memcpy ( d, head, n ); d += n;
399 *d++ = ST_STOP;
400 return normalize ( newlist );
401 }
402 p += n;
403 }
404 else if ( *p == ST_DATA ) {
405 memcpy ( &n, ++p, sizeof n ); p += sizeof n;
406 p += n;
407 }
408 else
409 p++;
410 }
411 return NULL;
412}
413
414/****************
415 * Return the length of the given list
416 */
417int
418gcry_sexp_length( const gcry_sexp_t list )
419{
420 const byte *p;
421 DATALEN n;
422 int type;
423 int length = 0;
424 int level = 0;
425
426 if ( !list )
427 return 0;
428
429 p = list->d;
430 while ( (type=*p) != ST_STOP ) {
431 p++;
432 if ( type == ST_DATA ) {
433 memcpy ( &n, p, sizeof n );
434 p += sizeof n + n;
435 if ( level == 1 )
436 length++;
437 }
438 else if ( type == ST_OPEN ) {
439 if ( level == 1 )
440 length++;
441 level++;
442 }
443 else if ( type == ST_CLOSE ) {
444 level--;
445 }
446 }
447 return length;
448}
449
450
451
452/****************
453 * Extract the CAR of the given list
454 */
455gcry_sexp_t
456gcry_sexp_nth( const gcry_sexp_t list, int number )
457{
458 const byte *p;
459 DATALEN n;
460 gcry_sexp_t newlist;
461 byte *d;
462 int level = 0;
463
464 if ( !list || list->d[0] != ST_OPEN )
465 return NULL;
466 p = list->d;
467
468 while ( number > 0 ) {
469 p++;
470 if ( *p == ST_DATA ) {
471 memcpy ( &n, ++p, sizeof n );
472 p += sizeof n + n;
473 p--;
474 if ( !level )
475 number--;
476 }
477 else if ( *p == ST_OPEN ) {
478 level++;
479 }
480 else if ( *p == ST_CLOSE ) {
481 level--;
482 if ( !level )
483 number--;
484 }
485 else if ( *p == ST_STOP ) {
486 return NULL;
487 }
488 }
489 p++;
490
491 if ( *p == ST_DATA ) {
492 memcpy ( &n, p, sizeof n ); p += sizeof n;
493 newlist = gcry_xmalloc ( sizeof *newlist + n + 1 );
494 d = newlist->d;
495 memcpy ( d, p, n ); d += n;
496 *d++ = ST_STOP;
497 }
498 else if ( *p == ST_OPEN ) {
499 const byte *head = p;
500
501 level = 1;
502 do {
503 p++;
504 if ( *p == ST_DATA ) {
505 memcpy ( &n, ++p, sizeof n );
506 p += sizeof n + n;
507 p--;
508 }
509 else if ( *p == ST_OPEN ) {
510 level++;
511 }
512 else if ( *p == ST_CLOSE ) {
513 level--;
514 }
515 else if ( *p == ST_STOP ) {
516 BUG ();
517 }
518 } while ( level );
519 n = p + 1 - head;
520
521 newlist = gcry_xmalloc ( sizeof *newlist + n );
522 d = newlist->d;
523 memcpy ( d, head, n ); d += n;
524 *d++ = ST_STOP;
525 }
526 else
527 newlist = NULL;
528
529 return normalize (newlist);
530}
531
532gcry_sexp_t
533gcry_sexp_car( const gcry_sexp_t list )
534{
535 return gcry_sexp_nth ( list, 0 );
536}
537
538/****************
539 * Get data from the car. The returned value is valid as long as the list
540 * is not modified.
541 */
542const char *
543gcry_sexp_nth_data( const gcry_sexp_t list, int number, size_t *datalen )
544{
545 const byte *p;
546 DATALEN n;
547 int level = 0;
548
549 *datalen = 0;
550 if ( !list ) {
551 return NULL;
552 }
553 p = list->d;
554 if ( *p == ST_OPEN )
555 p++; /* yep, a list */
556 else if (number )
557 return NULL; /* not a list but an n > 0 element requested */
558
559 /* skip n elements */
560 while ( number > 0 ) {
561 if ( *p == ST_DATA ) {
562 memcpy ( &n, ++p, sizeof n );
563 p += sizeof n + n;
564 p--;
565 if ( !level )
566 number--;
567 }
568 else if ( *p == ST_OPEN ) {
569 level++;
570 }
571 else if ( *p == ST_CLOSE ) {
572 level--;
573 if ( !level )
574 number--;
575 }
576 else if ( *p == ST_STOP ) {
577 return NULL;
578 }
579 p++;
580 }
581
582
583 if ( *p == ST_DATA ) {
584 memcpy ( &n, ++p, sizeof n );
585 *datalen = n;
586 return p + sizeof n;
587 }
588
589 return NULL;
590}
591
592/****************
593 * Get a MPI from the car
594 */
595gcry_mpi_t
596gcry_sexp_nth_mpi( gcry_sexp_t list, int number, int mpifmt )
597{
598 const byte *p;
599 DATALEN n;
600 int level = 0;
601
602 if ( !list )
603 return NULL;
604 if ( !mpifmt )
605 mpifmt = GCRYMPI_FMT_STD;
606
607 p = list->d;
608 if ( *p == ST_OPEN )
609 p++; /* yep, a list */
610 else if (number )
611 return NULL; /* not a list but an n > 0 element requested */
612
613 /* skip n elements */
614 while ( number > 0 ) {
615 if ( *p == ST_DATA ) {
616 memcpy ( &n, ++p, sizeof n );
617 p += sizeof n + n;
618 p--;
619 if ( !level )
620 number--;
621 }
622 else if ( *p == ST_OPEN ) {
623 level++;
624 }
625 else if ( *p == ST_CLOSE ) {
626 level--;
627 if ( !level )
628 number--;
629 }
630 else if ( *p == ST_STOP ) {
631 return NULL;
632 }
633 p++;
634 }
635
636 if ( *p == ST_DATA ) {
637 gcry_mpi_t a;
638 size_t nbytes;
639
640 memcpy ( &n, ++p, sizeof n );
641 p += sizeof n;
642 nbytes = n;
643 if( !gcry_mpi_scan( &a, mpifmt, p, n, &nbytes ) )
644 return a;
645 }
646
647 return NULL;
648}
649
650
651/****************
652 * Get the CDR
653 */
654gcry_sexp_t
655gcry_sexp_cdr( const gcry_sexp_t list )
656{
657 const byte *p;
658 const byte *head;
659 DATALEN n;
660 gcry_sexp_t newlist;
661 byte *d;
662 int level = 0;
663 int skip = 1;
664
665 if ( !list || list->d[0] != ST_OPEN )
666 return NULL;
667 p = list->d;
668
669 while ( skip > 0 ) {
670 p++;
671 if ( *p == ST_DATA ) {
672 memcpy ( &n, ++p, sizeof n );
673 p += sizeof n + n;
674 p--;
675 if ( !level )
676 skip--;
677 }
678 else if ( *p == ST_OPEN ) {
679 level++;
680 }
681 else if ( *p == ST_CLOSE ) {
682 level--;
683 if ( !level )
684 skip--;
685 }
686 else if ( *p == ST_STOP ) {
687 return NULL;
688 }
689 }
690 p++;
691
692 head = p;
693 level = 0;
694 do {
695 if ( *p == ST_DATA ) {
696 memcpy ( &n, ++p, sizeof n );
697 p += sizeof n + n;
698 p--;
699 }
700 else if ( *p == ST_OPEN ) {
701 level++;
702 }
703 else if ( *p == ST_CLOSE ) {
704 level--;
705 }
706 else if ( *p == ST_STOP ) {
707 return NULL;
708 }
709 p++;
710 } while ( level );
711 n = p - head;
712
713 newlist = gcry_xmalloc ( sizeof *newlist + n + 2 );
714 d = newlist->d;
715 *d++ = ST_OPEN;
716 memcpy ( d, head, n ); d += n;
717 *d++ = ST_CLOSE;
718 *d++ = ST_STOP;
719
720 return normalize (newlist);
721}
722
723gcry_sexp_t
724gcry_sexp_cadr ( const gcry_sexp_t list )
725{
726 gcry_sexp_t a, b;
727
728 a = gcry_sexp_cdr ( list );
729 b = gcry_sexp_car ( a );
730 gcry_sexp_release ( a );
731 return b;
732}
733
734
735
736static int
737hextobyte( const byte *s )
738{
739 int c=0;
740
741 if( *s >= '0' && *s <= '9' )
742 c = 16 * (*s - '0');
743 else if( *s >= 'A' && *s <= 'F' )
744 c = 16 * (10 + *s - 'A');
745 else if( *s >= 'a' && *s <= 'f' ) {
746 c = 16 * (10 + *s - 'a');
747 }
748 s++;
749 if( *s >= '0' && *s <= '9' )
750 c += *s - '0';
751 else if( *s >= 'A' && *s <= 'F' )
752 c += 10 + *s - 'A';
753 else if( *s >= 'a' && *s <= 'f' ) {
754 c += 10 + *s - 'a';
755 }
756 return c;
757}
758
759struct make_space_ctx {
760 gcry_sexp_t sexp;
761 size_t allocated;
762 byte *pos;
763};
764
765static void
766make_space ( struct make_space_ctx *c, size_t n )
767{
768 size_t used = c->pos - c->sexp->d;
769
770 if ( used + n + sizeof(DATALEN) + 1 >= c->allocated ) {
771 gcry_sexp_t newsexp;
772 byte *newhead;
773
774 c->allocated += 2*(n+sizeof(DATALEN)+1);
775 newsexp = gcry_xrealloc ( c->sexp, sizeof *newsexp + c->allocated - 1 );
776 newhead = newsexp->d;
777 c->pos = newhead + used;
778 c->sexp = newsexp;
779 }
780}
781
782
783/* Unquote STRING of LENGTH and store it into BUF. The surrounding
784 quotes are must already be removed from STRING. We assume that the
785 quoted string is syntacillay correct. */
786static size_t
787unquote_string (const unsigned char *string, size_t length, unsigned char *buf)
788{
789 int esc = 0;
790 const unsigned char *s = string;
791 unsigned char *d = buf;
792 size_t n = length;
793
794 for (; n; n--, s++)
795 {
796 if (esc)
797 {
798 switch (*s)
799 {
800 case 'b': *d++ = '\b'; break;
801 case 't': *d++ = '\t'; break;
802 case 'v': *d++ = '\v'; break;
803 case 'n': *d++ = '\n'; break;
804 case 'f': *d++ = '\f'; break;
805 case 'r': *d++ = '\r'; break;
806 case '"': *d++ = '\"'; break;
807 case '\'': *d++ = '\''; break;
808 case '\\': *d++ = '\\'; break;
809
810 case '\r': /* ignore CR[,LF] */
811 if (n>1 && s[1] == '\n')
812 {
813 s++; n--;
814 }
815 esc = 0;
816 break;
817
818 case '\n': /* ignore LF[,CR] */
819 if (n>1 && s[1] == '\r')
820 {
821 s++; n--;
822 }
823 break;
824
825 case 'x': /* hex value */
826 if (n>2 && hexdigitp (s+1) && hexdigitp (s+2))
827 {
828 s++; n--;
829 *d++ = xtoi_2 (s);
830 s++; n--;
831 }
832 break;
833
834 default:
835 if (n>2 && octdigitp (s) && octdigitp (s+1) && octdigitp (s+2))
836 {
837 *d++ = (atoi_1 (s)*64) + (atoi_1 (s+1)*8) + atoi_1 (s+2);
838 s += 2;
839 n -= 2;
840 }
841 break;
842 }
843 esc = 0;
844 }
845 else if( *s == '\\' )
846 esc = 1;
847 else
848 *d++ = *s;
849 }
850
851 return d - buf;
852}
853
854/****************
855 * Scan the provided buffer and return the S expression in our internal
856 * format. Returns a newly allocated expression. If erroff is not NULL and
857 * a parsing error has occured, the offset into buffer will be returned.
858 * If ARGFLAG is true, the function supports some printf like
859 * expressions.
860 * These are:
861 *%m - MPI
862 *%s - string (no autoswitch to secure allocation)
863 *%d - integer stored as string (no autoswitch to secure allocation)
864 * %b - memory buffer; this takes _two_ arguments: an integer with the
865 * length of the buffer and a pointer to the buffer.
866 * all other format elements are currently not defined and return an error.
867 * this includes the "%%" sequence becauce the percent sign is not an
868 * allowed character.
869 * FIXME: We should find a way to store the secure-MPIs not in the string
870 * but as reference to somewhere - this can help us to save huge amounts
871 * of secure memory. The problem is, that if only one element is secure, all
872 * other elements are automagicaly copied to secure memory too, so the most
873 * common operation gcry_sexp_cdr_mpi() will always return a secure MPI
874 * regardless whether it is needed or not.
875 */
876static gcry_error_t
877sexp_sscan (gcry_sexp_t *retsexp, size_t *erroff,
878 const char *buffer, size_t length, int argflag,
879 va_list arg_ptr, void **arg_list)
880{
881 gcry_err_code_t err = GPG_ERR_NO_ERROR;
882 static const char tokenchars[] =
883 "abcdefghijklmnopqrstuvwxyz"
884 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
885 "0123456789-./_:*+=";
886 const char *p;
887 size_t n;
888 const char *digptr = NULL;
889 const char *quoted = NULL;
890 const char *tokenp = NULL;
891 const char *hexfmt = NULL;
892 const char *base64 = NULL;
893 const char *disphint = NULL;
894 const char *percent = NULL;
895 int hexcount = 0;
896 int quoted_esc = 0;
897 int datalen = 0;
898 size_t dummy_erroff;
899 struct make_space_ctx c;
900 int arg_counter = 0;
901 int level = 0;
902
903 /* FIXME: invent better error codes (?). */
904
905 if (! erroff)
906 erroff = &dummy_erroff;
907
908 /* Depending on wether ARG_LIST is non-zero or not, this macro gives
909 us the next argument, either from the variable argument list as
910 specified by ARG_PTR or from the argument array ARG_LIST. */
911#define ARG_NEXT(storage, type) \
912 do \
913 { \
914 if (! arg_list) \
915 storage = va_arg (arg_ptr, type); \
916 else \
917 storage = *((type *) (arg_list[arg_counter++])); \
918 } \
919 while (0)
920
921#define MAKE_SPACE(n) do { make_space ( &c, (n) ); } while (0)
922 #define STORE_LEN(p,n) do { \
923 DATALEN ashort = (n); \
924 memcpy ( (p), &ashort, sizeof(ashort) ); \
925 (p) += sizeof (ashort); \
926 } while (0)
927
928 /* We assume that the internal representation takes less memory
929 * than the provided one. However, we add space for one extra datalen
930 * so that the code which does the ST_CLOSE can use MAKE_SPACE */
931 c.allocated = length + sizeof(DATALEN);
932 if (buffer && length && gcry_is_secure (buffer))
933 c.sexp = gcry_xmalloc_secure (sizeof *c.sexp + c.allocated - 1);
934 else
935 c.sexp = gcry_xmalloc (sizeof *c.sexp + c.allocated - 1);
936 c.pos = c.sexp->d;
937
938 for (p = buffer, n = length; n; p++, n--)
939 {
940 if (tokenp && (! hexfmt))
941 {
942 if (strchr (tokenchars, *p))
943 continue;
944 else
945 {
946 datalen = p - tokenp;
947 MAKE_SPACE (datalen);
948 *c.pos++ = ST_DATA;
949 STORE_LEN (c.pos, datalen);
950 memcpy (c.pos, tokenp, datalen);
951 c.pos += datalen;
952 tokenp = NULL;
953 }
954 }
955
956 if (quoted)
957 {
958 if (quoted_esc)
959 {
960 switch (*p)
961 {
962 case 'b': case 't': case 'v': case 'n': case 'f':
963 case 'r': case '"': case '\'': case '\\':
964 quoted_esc = 0;
965 break;
966
967 case '0': case '1': case '2': case '3': case '4':
968 case '5': case '6': case '7':
969 if (! ((n > 2)
970 && (p[1] >= '0') && (p[1] <= '7')
971 && (p[2] >= '0') && (p[2] <= '7')))
972 {
973 *erroff = p - buffer;
974 /* Invalid octal value. */
975 err = GPG_ERR_SEXP_BAD_QUOTATION;
976 //return gcry_error (GPG_ERR_SEXP_BAD_QUOTATION);
977 }
978 p += 2;
979 n -= 2;
980 quoted_esc = 0;
981 break;
982
983 case 'x':
984 if (! ((n > 2) && isxdigit(p[1]) && isxdigit(p[2])))
985 {
986 *erroff = p - buffer;
987 /* Invalid hex value. */
988 err = GPG_ERR_SEXP_BAD_QUOTATION;
989 //return gcry_error (GPG_ERR_SEXP_BAD_QUOTATION);
990 }
991 p += 2;
992 n -= 2;
993 quoted_esc = 0;
994 break;
995
996 case '\r':
997 /* ignore CR[,LF] */
998 if (n && (p[1] == '\n'))
999 {
1000 p++;
1001 n--;
1002 }
1003 quoted_esc = 0;
1004 break;
1005
1006 case '\n':
1007 /* ignore LF[,CR] */
1008 if (n && (p[1] == '\r'))
1009 {
1010 p++;
1011 n--;
1012 }
1013 quoted_esc = 0;
1014 break;
1015
1016 default:
1017 *erroff = p - buffer;
1018 /* Invalid quoted string escape. */
1019 err = GPG_ERR_SEXP_BAD_QUOTATION;
1020 }
1021 }
1022 else if (*p == '\\')
1023 quoted_esc = 1;
1024 else if (*p == '\"')
1025 {
1026 /* Keep it easy - we know that the unquoted string will
1027 never be larger. */
1028 char *save;
1029 size_t len;
1030
1031 quoted++; /* Skip leading quote. */
1032 MAKE_SPACE (p - quoted);
1033 *c.pos++ = ST_DATA;
1034 save = c.pos;
1035 STORE_LEN (c.pos, 0); /* Will be fixed up later. */
1036 len = unquote_string (quoted, p - quoted, c.pos);
1037 c.pos += len;
1038 STORE_LEN (save, len);
1039 quoted = NULL;
1040 }
1041 }
1042 else if (hexfmt)
1043 {
1044 if (isxdigit (*p))
1045 hexcount++;
1046 else if (*p == '#')
1047 {
1048 if ((hexcount & 1))
1049 {
1050 *erroff = p - buffer;
1051 err = GPG_ERR_SEXP_ODD_HEX_NUMBERS;
1052 }
1053
1054 datalen = hexcount / 2;
1055 MAKE_SPACE (datalen);
1056 *c.pos++ = ST_DATA;
1057 STORE_LEN (c.pos, datalen);
1058 for (hexfmt++; hexfmt < p; hexfmt++)
1059 {
1060 if (isspace (*hexfmt))
1061 continue;
1062 *c.pos++ = hextobyte (hexfmt);
1063 hexfmt++;
1064 }
1065 hexfmt = NULL;
1066 }
1067 else if (! isspace (*p))
1068 {
1069 *erroff = p - buffer;
1070 err = GPG_ERR_SEXP_BAD_HEX_CHAR;
1071 }
1072 }
1073 else if (base64)
1074 {
1075 if (*p == '|')
1076 base64 = NULL;
1077 }
1078 else if (digptr)
1079 {
1080 if (isdigit (*p))
1081 ;
1082 else if (*p == ':')
1083 {
1084 datalen = atoi (digptr); /* FIXME: check for overflow. */
1085 digptr = NULL;
1086 if (datalen > n - 1)
1087 {
1088 *erroff = p - buffer;
1089 /* Buffer too short. */
1090 err = GPG_ERR_SEXP_STRING_TOO_LONG;
1091 }
1092 /* Make a new list entry. */
1093 MAKE_SPACE (datalen);
1094 *c.pos++ = ST_DATA;
1095 STORE_LEN (c.pos, datalen);
1096 memcpy (c.pos, p + 1, datalen);
1097 c.pos += datalen;
1098 n -= datalen;
1099 p += datalen;
1100 }
1101 else if (*p == '\"')
1102 {
1103 digptr = NULL; /* We ignore the optional length. */
1104 quoted = p;
1105 quoted_esc = 0;
1106 }
1107 else if (*p == '#')
1108 {
1109 digptr = NULL; /* We ignore the optional length. */
1110 hexfmt = p;
1111 hexcount = 0;
1112 }
1113 else if (*p == '|')
1114 {
1115 digptr = NULL; /* We ignore the optional length. */
1116 base64 = p;
1117 }
1118 else
1119 {
1120 *erroff = p - buffer;
1121 err = GPG_ERR_SEXP_INV_LEN_SPEC;
1122 }
1123 }
1124 else if (percent)
1125 {
1126 if (*p == 'm')
1127 {
1128 /* Insert an MPI. */
1129 gcry_mpi_t m;
1130 size_t nm = 0;
1131
1132 ARG_NEXT (m, gcry_mpi_t);
1133
1134 if (gcry_mpi_print (GCRYMPI_FMT_STD, NULL, 0, &nm, m))
1135 BUG ();
1136
1137 MAKE_SPACE (nm);
1138 if ((! gcry_is_secure (c.sexp->d))
1139 && gcry_mpi_get_flag ( m, GCRYMPI_FLAG_SECURE))
1140 {
1141 /* We have to switch to secure allocation. */
1142 gcry_sexp_t newsexp;
1143 byte *newhead;
1144
1145 newsexp = gcry_xmalloc_secure (sizeof *newsexp
1146 + c.allocated - 1);
1147 newhead = newsexp->d;
1148 memcpy (newhead, c.sexp->d, (c.pos - c.sexp->d));
1149 c.pos = newhead + (c.pos - c.sexp->d);
1150 gcry_free (c.sexp);
1151 c.sexp = newsexp;
1152 }
1153
1154 *c.pos++ = ST_DATA;
1155 STORE_LEN (c.pos, nm);
1156 if (gcry_mpi_print (GCRYMPI_FMT_STD, c.pos, nm, &nm, m))
1157 BUG ();
1158 c.pos += nm;
1159 }
1160 else if (*p == 's')
1161 {
1162 /* Insert an string. */
1163 const char *astr;
1164 size_t alen;
1165
1166 ARG_NEXT (astr, const char *);
1167 alen = strlen (astr);
1168
1169 MAKE_SPACE (alen);
1170 *c.pos++ = ST_DATA;
1171 STORE_LEN (c.pos, alen);
1172 memcpy (c.pos, astr, alen);
1173 c.pos += alen;
1174 }
1175 else if (*p == 'b')
1176 {
1177 /* Insert a memory buffer. */
1178 const char *astr;
1179 int alen;
1180
1181 ARG_NEXT (alen, int);
1182 ARG_NEXT (astr, const char *);
1183
1184 MAKE_SPACE (alen);
1185 if (alen
1186 && !gcry_is_secure (c.sexp->d)
1187 && gcry_is_secure (astr))
1188 {
1189 /* We have to switch to secure allocation. */
1190 gcry_sexp_t newsexp;
1191 byte *newhead;
1192
1193 newsexp = gcry_xmalloc_secure (sizeof *newsexp
1194 + c.allocated - 1);
1195 newhead = newsexp->d;
1196 memcpy (newhead, c.sexp->d, (c.pos - c.sexp->d));
1197 c.pos = newhead + (c.pos - c.sexp->d);
1198 gcry_free (c.sexp);
1199 c.sexp = newsexp;
1200 }
1201
1202 *c.pos++ = ST_DATA;
1203 STORE_LEN (c.pos, alen);
1204 memcpy (c.pos, astr, alen);
1205 c.pos += alen;
1206 }
1207 else if (*p == 'd')
1208 {
1209 /* Insert an integer as string. */
1210 int aint;
1211 size_t alen;
1212 char buf[20];
1213
1214 ARG_NEXT (aint, int);
1215 sprintf (buf, "%d", aint);
1216 alen = strlen (buf);
1217 MAKE_SPACE (alen);
1218 *c.pos++ = ST_DATA;
1219 STORE_LEN (c.pos, alen);
1220 memcpy (c.pos, buf, alen);
1221 c.pos += alen;
1222 }
1223 else
1224 {
1225 *erroff = p - buffer;
1226 /* Invalid format specifier. */
1227 err = GPG_ERR_SEXP_INV_LEN_SPEC;
1228 }
1229 percent = NULL;
1230 }
1231 else if (*p == '(')
1232 {
1233 if (disphint)
1234 {
1235 *erroff = p - buffer;
1236 /* Open display hint. */
1237 err = GPG_ERR_SEXP_UNMATCHED_DH;
1238 }
1239 MAKE_SPACE (0);
1240 *c.pos++ = ST_OPEN;
1241 level++;
1242 }
1243 else if (*p == ')')
1244 {
1245 /* Walk up. */
1246 if (disphint)
1247 {
1248 *erroff = p - buffer;
1249 /* Open display hint. */
1250 err = GPG_ERR_SEXP_UNMATCHED_DH;
1251 }
1252 MAKE_SPACE (0);
1253 *c.pos++ = ST_CLOSE;
1254 level--;
1255 }
1256 else if (*p == '\"')
1257 {
1258 quoted = p;
1259 quoted_esc = 0;
1260 }
1261 else if (*p == '#')
1262 {
1263 hexfmt = p;
1264 hexcount = 0;
1265 }
1266 else if (*p == '|')
1267 base64 = p;
1268 else if (*p == '[')
1269 {
1270 if (disphint)
1271 {
1272 *erroff = p - buffer;
1273 /* Open display hint. */
1274 err = GPG_ERR_SEXP_NESTED_DH;
1275 }
1276 disphint = p;
1277 }
1278 else if (*p == ']')
1279 {
1280 if (! disphint)
1281 {
1282 *erroff = p - buffer;
1283 /* Open display hint. */
1284 err = GPG_ERR_SEXP_UNMATCHED_DH;
1285 }
1286 disphint = NULL;
1287 }
1288 else if (isdigit (*p))
1289 {
1290 if (*p == '0')
1291 {
1292 /* A length may not begin with zero. */
1293 *erroff = p - buffer;
1294 err = GPG_ERR_SEXP_ZERO_PREFIX;
1295 }
1296 digptr = p;
1297 }
1298 else if (strchr (tokenchars, *p))
1299 tokenp = p;
1300 else if (isspace (*p))
1301 ;
1302 else if (*p == '{')
1303 {
1304 /* fixme: handle rescanning: we can do this by saving our
1305 current state and start over at p+1 -- Hmmm. At this
1306 point here we are in a well defined state, so we don't
1307 need to save it. Great. */
1308 *erroff = p - buffer;
1309 err = GPG_ERR_SEXP_UNEXPECTED_PUNC;
1310 }
1311 else if (strchr ("&\\", *p))
1312 {
1313 /* Reserved punctuation. */
1314 *erroff = p - buffer;
1315 err = GPG_ERR_SEXP_UNEXPECTED_PUNC;
1316 }
1317 else if (argflag && (*p == '%'))
1318 percent = p;
1319 else
1320 {
1321 /* Bad or unavailable. */
1322 *erroff = p - buffer;
1323 err = GPG_ERR_SEXP_BAD_CHARACTER;
1324 }
1325 }
1326 MAKE_SPACE (0);
1327 *c.pos++ = ST_STOP;
1328
1329 if (level)
1330 err = GPG_ERR_SEXP_UNMATCHED_PAREN;
1331
1332 if (err)
1333 {
1334 /* Error -> deallocate. */
1335 if (c.sexp)
1336 {
1337 /* Extra paranoid wipe on error. */
1338 if (gcry_is_secure (c.sexp))
1339 wipememory (c.sexp, sizeof (struct gcry_sexp) + c.allocated - 1);
1340 gcry_free (c.sexp);
1341 }
1342 /* This might be expected by existing code... */
1343 *retsexp = NULL;
1344 }
1345 else
1346 *retsexp = normalize (c.sexp);
1347
1348 return gcry_error (err);
1349#undef MAKE_SPACE
1350#undef STORE_LEN
1351}
1352
1353gcry_error_t
1354gcry_sexp_build (gcry_sexp_t *retsexp, size_t *erroff, const char *format, ...)
1355{
1356 gcry_error_t rc;
1357 va_list arg_ptr;
1358
1359 va_start (arg_ptr, format);
1360 rc = sexp_sscan (retsexp, erroff, format, strlen(format), 1,
1361 arg_ptr, NULL);
1362 va_end (arg_ptr);
1363
1364 return rc;
1365}
1366
1367/* Like gcry_sexp_build, but uses an array instead of variable
1368 function arguments. */
1369gcry_error_t
1370gcry_sexp_build_array (gcry_sexp_t *retsexp, size_t *erroff,
1371 const char *format, void **arg_list)
1372{
1373 /* We don't need the va_list because it is controlled by the
1374 following flag, however we have to pass it but can't initialize
1375 it as there is no portable way to do so. volatile is needed to
1376 suppress the compiler warning */
1377 volatile va_list dummy_arg_ptr;
1378
1379 gcry_error_t rc;
1380
1381 rc = sexp_sscan (retsexp, erroff, format, strlen(format), 1,
1382 dummy_arg_ptr, arg_list);
1383
1384 return rc;
1385}
1386
1387gcry_error_t
1388gcry_sexp_sscan (gcry_sexp_t *retsexp, size_t *erroff,
1389 const char *buffer, size_t length)
1390{
1391 /* We don't need the va_list because it is controlled by the
1392 following flag, however we have to pass it but can't initialize
1393 it as there is no portable way to do so. volatile is needed to
1394 suppress the compiler warning */
1395 volatile va_list dummy_arg_ptr;
1396
1397 return sexp_sscan (retsexp, erroff, buffer, length, 0,
1398 dummy_arg_ptr, NULL);
1399}
1400
1401
1402/* Figure out a suitable encoding for BUFFER of LENGTH.
1403 Returns: 0 = Binary
1404 1 = String possible
1405 2 = Token possible
1406*/
1407static int
1408suitable_encoding (const unsigned char *buffer, size_t length)
1409{
1410 const unsigned char *s;
1411 int maybe_token = 1;
1412
1413 if (!length)
1414 return 1;
1415
1416 for (s=buffer; length; s++, length--)
1417 {
1418 if ( (*s < 0x20 || (*s >= 0x7f && *s <= 0xa0))
1419 && !strchr ("\b\t\v\n\f\r\"\'\\", *s))
1420 return 0; /*binary*/
1421 if ( maybe_token
1422 && !alphap (s) && !digitp (s) && !strchr (TOKEN_SPECIALS, *s))
1423 maybe_token = 0;
1424 }
1425 s = buffer;
1426 if ( maybe_token && !digitp (s) )
1427 return 2;
1428 return 1;
1429}
1430
1431
1432static int
1433convert_to_hex (const unsigned char *src, size_t len, unsigned char *dest)
1434{
1435 int i;
1436
1437 if (dest)
1438 {
1439 *dest++ = '#';
1440 for (i=0; i < len; i++, dest += 2 )
1441 sprintf (dest, "%02X", src[i]);
1442 *dest++ = '#';
1443 }
1444 return len*2+2;
1445}
1446
1447static int
1448convert_to_string (const unsigned char *s, size_t len, unsigned char *dest)
1449{
1450 if (dest)
1451 {
1452 unsigned char *p = dest;
1453 *p++ = '\"';
1454 for (; len; len--, s++ )
1455 {
1456 switch (*s)
1457 {
1458 case '\b': *p++ = '\\'; *p++ = 'b'; break;
1459 case '\t': *p++ = '\\'; *p++ = 't'; break;
1460 case '\v': *p++ = '\\'; *p++ = 'v'; break;
1461 case '\n': *p++ = '\\'; *p++ = 'n'; break;
1462 case '\f': *p++ = '\\'; *p++ = 'f'; break;
1463 case '\r': *p++ = '\\'; *p++ = 'r'; break;
1464 case '\"': *p++ = '\\'; *p++ = '\"'; break;
1465 case '\'': *p++ = '\\'; *p++ = '\''; break;
1466 case '\\': *p++ = '\\'; *p++ = '\\'; break;
1467 default:
1468 if ( (*s < 0x20 || (*s >= 0x7f && *s <= 0xa0)))
1469 {
1470 sprintf (p, "\\x%02x", *s);
1471 p += 4;
1472 }
1473 else
1474 *p++ = *s;
1475 }
1476 }
1477 *p++ = '\"';
1478 return p - dest;
1479 }
1480 else
1481 {
1482 int count = 2;
1483 for (; len; len--, s++ )
1484 {
1485 switch (*s)
1486 {
1487 case '\b':
1488 case '\t':
1489 case '\v':
1490 case '\n':
1491 case '\f':
1492 case '\r':
1493 case '\"':
1494 case '\'':
1495 case '\\': count += 2; break;
1496 default:
1497 if ( (*s < 0x20 || (*s >= 0x7f && *s <= 0xa0)))
1498 count += 4;
1499 else
1500 count++;
1501 }
1502 }
1503 return count;
1504 }
1505}
1506
1507
1508
1509static int
1510convert_to_token (const unsigned char *src, size_t len, unsigned char *dest)
1511{
1512 if (dest)
1513 memcpy (dest, src, len);
1514 return len;
1515}
1516
1517
1518/****************
1519 * Print SEXP to buffer using the MODE. Returns the length of the
1520 * SEXP in buffer or 0 if the buffer is too short (We have at least an
1521 * empty list consisting of 2 bytes). If a buffer of NULL is provided,
1522 * the required length is returned.
1523 */
1524size_t
1525gcry_sexp_sprint( const gcry_sexp_t list, int mode,
1526 char *buffer, size_t maxlength )
1527{
1528 static byte empty[3] = { ST_OPEN, ST_CLOSE, ST_STOP };
1529 const byte *s;
1530 char *d;
1531 DATALEN n;
1532 char numbuf[20];
1533 size_t len = 0;
1534 int i, indent = 0;
1535
1536 s = list? list->d : empty;
1537 d = buffer;
1538 while ( *s != ST_STOP )
1539 {
1540 switch ( *s )
1541 {
1542 case ST_OPEN:
1543 s++;
1544 if ( mode != GCRYSEXP_FMT_CANON )
1545 {
1546 if (indent)
1547 len++;
1548 len += indent;
1549 }
1550 len++;
1551 if ( buffer )
1552 {
1553 if ( len >= maxlength )
1554 return 0;
1555 if ( mode != GCRYSEXP_FMT_CANON )
1556 {
1557 if (indent)
1558 *d++ = '\n';
1559 for (i=0; i < indent; i++)
1560 *d++ = ' ';
1561 }
1562 *d++ = '(';
1563 }
1564 indent++;
1565 break;
1566 case ST_CLOSE:
1567 s++;
1568 len++;
1569 if ( buffer )
1570 {
1571 if ( len >= maxlength )
1572 return 0;
1573 *d++ = ')';
1574 }
1575 indent--;
1576 if (*s != ST_OPEN && *s != ST_STOP && mode != GCRYSEXP_FMT_CANON)
1577 {
1578 len++;
1579 len += indent;
1580 if (buffer)
1581 {
1582 if (len >= maxlength)
1583 return 0;
1584 *d++ = '\n';
1585 for (i=0; i < indent; i++)
1586 *d++ = ' ';
1587 }
1588 }
1589 break;
1590 case ST_DATA:
1591 s++;
1592 memcpy ( &n, s, sizeof n ); s += sizeof n;
1593 if (mode == GCRYSEXP_FMT_ADVANCED)
1594 {
1595 int type;
1596 size_t nn;
1597
1598 switch ( (type=suitable_encoding (s, n)))
1599 {
1600 case 1: nn = convert_to_string (s, n, NULL); break;
1601 case 2: nn = convert_to_token (s, n, NULL); break;
1602 default: nn = convert_to_hex (s, n, NULL); break;
1603 }
1604 len += nn;
1605 if (buffer)
1606 {
1607 if (len >= maxlength)
1608 return 0;
1609 switch (type)
1610 {
1611 case 1: convert_to_string (s, n, d); break;
1612 case 2: convert_to_token (s, n, d); break;
1613 default: convert_to_hex (s, n, d); break;
1614 }
1615 d += nn;
1616 }
1617 if (s[n] != ST_CLOSE)
1618 {
1619 len++;
1620 if (buffer)
1621 {
1622 if (len >= maxlength)
1623 return 0;
1624 *d++ = ' ';
1625 }
1626 }
1627 }
1628 else
1629 {
1630 sprintf (numbuf, "%u:", (unsigned int)n );
1631 len += strlen (numbuf) + n;
1632 if ( buffer )
1633 {
1634 if ( len >= maxlength )
1635 return 0;
1636 d = stpcpy ( d, numbuf );
1637 memcpy ( d, s, n ); d += n;
1638 }
1639 }
1640 s += n;
1641 break;
1642 default:
1643 BUG ();
1644 }
1645 }
1646 if ( mode != GCRYSEXP_FMT_CANON )
1647 {
1648 len++;
1649 if (buffer)
1650 {
1651 if ( len >= maxlength )
1652 return 0;
1653 *d++ = '\n';
1654 }
1655 }
1656 if (buffer)
1657 {
1658 if ( len >= maxlength )
1659 return 0;
1660 *d++ = 0; /* for convenience we make a C string */
1661 }
1662 else
1663 len++; /* we need one byte more for this */
1664
1665 return len;
1666}
1667
1668
1669/* Scan a cannocial encoded buffer with implicit length values and
1670 return the actual length this S-expression uses. For a valid S-Exp
1671 it should never return 0. If LENGTH is not zero, the maximum
1672 length to scan is given - this can be used for syntax checks of
1673 data passed from outside. errorcode and erroff may both be passed as
1674 NULL. */
1675size_t
1676gcry_sexp_canon_len (const unsigned char *buffer, size_t length,
1677 size_t *erroff, gcry_error_t *errcode)
1678{
1679 const unsigned char *p;
1680 const char *disphint=NULL;
1681 unsigned int datalen = 0;
1682 size_t dummy_erroff;
1683 gcry_error_t dummy_errcode;
1684 size_t count = 0;
1685 int level = 0;
1686
1687 if (!erroff)
1688 erroff = &dummy_erroff;
1689 if (!errcode)
1690 errcode = &dummy_errcode;
1691
1692 *errcode = gcry_error (GPG_ERR_NO_ERROR);
1693 *erroff = 0;
1694 if (!buffer)
1695 return 0;
1696 if (*buffer != '(')
1697 {
1698 *errcode = gcry_error (GPG_ERR_SEXP_NOT_CANONICAL);
1699 return 0;
1700 }
1701
1702 for (p=buffer; ; p++, count++ )
1703 {
1704 if (length && count >= length)
1705 {
1706 *erroff = count;
1707 *errcode = gcry_error (GPG_ERR_SEXP_STRING_TOO_LONG);
1708 return 0;
1709 }
1710
1711 if (datalen)
1712 {
1713 if (*p == ':')
1714 {
1715 if (length && (count+datalen) >= length)
1716 {
1717 *erroff = count;
1718 *errcode = gcry_error (GPG_ERR_SEXP_STRING_TOO_LONG);
1719 return 0;
1720 }
1721 count += datalen;
1722 p += datalen;
1723 datalen = 0;
1724 }
1725 else if (digitp(p))
1726 datalen = datalen*10 + atoi_1(p);
1727 else
1728 {
1729 *erroff = count;
1730 *errcode = gcry_error (GPG_ERR_SEXP_INV_LEN_SPEC);
1731 return 0;
1732 }
1733 }
1734 else if (*p == '(')
1735 {
1736 if (disphint)
1737 {
1738 *erroff = count;
1739 *errcode = gcry_error (GPG_ERR_SEXP_UNMATCHED_DH);
1740 return 0;
1741 }
1742 level++;
1743 }
1744 else if (*p == ')')
1745 { /* walk up */
1746 if (!level)
1747 {
1748 *erroff = count;
1749 *errcode = gcry_error (GPG_ERR_SEXP_UNMATCHED_PAREN);
1750 return 0;
1751 }
1752 if (disphint)
1753 {
1754 *erroff = count;
1755 *errcode = gcry_error (GPG_ERR_SEXP_UNMATCHED_DH);
1756 return 0;
1757 }
1758 if (!--level)
1759 return ++count; /* ready */
1760 }
1761 else if (*p == '[')
1762 {
1763 if (disphint)
1764 {
1765 *erroff = count;
1766 *errcode = gcry_error (GPG_ERR_SEXP_NESTED_DH);
1767 return 0;
1768 }
1769 disphint = p;
1770 }
1771 else if (*p == ']')
1772 {
1773 if( !disphint )
1774 {
1775 *erroff = count;
1776 *errcode = gcry_error (GPG_ERR_SEXP_UNMATCHED_DH);
1777 return 0;
1778 }
1779 disphint = NULL;
1780 }
1781 else if (digitp (p) )
1782 {
1783 if (*p == '0')
1784 {
1785 *erroff = count;
1786 *errcode = gcry_error (GPG_ERR_SEXP_ZERO_PREFIX);
1787 return 0;
1788 }
1789 datalen = atoi_1 (p);
1790 }
1791 else if (*p == '&' || *p == '\\')
1792 {
1793 *erroff = count;
1794 *errcode = gcry_error (GPG_ERR_SEXP_UNEXPECTED_PUNC);
1795 return 0;
1796 }
1797 else
1798 {
1799 *erroff = count;
1800 *errcode = gcry_error (GPG_ERR_SEXP_BAD_CHARACTER);
1801 return 0;
1802 }
1803 }
1804}
diff --git a/pwmanager/libcrypt/crypt/stdmem.c b/pwmanager/libcrypt/crypt/stdmem.c
new file mode 100644
index 0000000..659bc4e
--- a/dev/null
+++ b/pwmanager/libcrypt/crypt/stdmem.c
@@ -0,0 +1,195 @@
1 /* stdmem.c -private memory allocator
2 *Copyright (C) 1998, 2000, 2002 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <config.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <stdarg.h>
26
27#include "g10lib.h"
28#include "stdmem.h"
29#include "secmem.h"
30
31
32#define MAGIC_NOR_BYTE 0x55
33#define MAGIC_SEC_BYTE 0xcc
34#define MAGIC_END_BYTE 0xaa
35
36#if SIZEOF_UNSIGNED_LONG == 8
37#define EXTRA_ALIGN 4
38#else
39#define EXTRA_ALIGN 0
40#endif
41
42
43static int use_m_guard = 0;
44
45/****************
46 * Warning: Never use this function after any of the functions
47 * here have been used.
48 */
49void
50_gcry_private_enable_m_guard(void)
51{
52 use_m_guard = 1;
53}
54
55/****************
56 * Allocate memory of size n.
57 * Return NULL if we are out of memory.
58 */
59void *
60_gcry_private_malloc( size_t n)
61{
62 if(!n)
63 return NULL; /* allocating 0 bytes is undefined - better return
64 an error */
65 if( use_m_guard ) {
66 char *p;
67
68 if( !(p = malloc( n + EXTRA_ALIGN+5 )) )
69 return NULL;
70 ((byte*)p)[EXTRA_ALIGN+0] = n;
71 ((byte*)p)[EXTRA_ALIGN+1] = n >> 8 ;
72 ((byte*)p)[EXTRA_ALIGN+2] = n >> 16 ;
73 ((byte*)p)[EXTRA_ALIGN+3] = MAGIC_NOR_BYTE;
74 p[4+EXTRA_ALIGN+n] = MAGIC_END_BYTE;
75 return p+EXTRA_ALIGN+4;
76 }
77 else {
78 return malloc( n );
79 }
80}
81
82/****************
83 * Allocate memory of size n from the secure memory pool.
84 * Return NULL if we are out of memory.
85 */
86void *
87_gcry_private_malloc_secure( size_t n)
88{
89 if(!n)
90 return NULL; /* allocating 0 bytes is undefined - better return
91 an error */
92 if( use_m_guard ) {
93 char *p;
94
95 if( !(p = _gcry_secmem_malloc( n +EXTRA_ALIGN+ 5 )) )
96 return NULL;
97 ((byte*)p)[EXTRA_ALIGN+0] = n;
98 ((byte*)p)[EXTRA_ALIGN+1] = n >> 8 ;
99 ((byte*)p)[EXTRA_ALIGN+2] = n >> 16 ;
100 ((byte*)p)[EXTRA_ALIGN+3] = MAGIC_SEC_BYTE;
101 p[4+EXTRA_ALIGN+n] = MAGIC_END_BYTE;
102 return p+EXTRA_ALIGN+4;
103 }
104 else {
105 return _gcry_secmem_malloc( n );
106 }
107}
108
109
110/****************
111 * realloc and clear the old space
112 * Return NULL if there is not enoug memory.
113 */
114void *
115_gcry_private_realloc( void *a, size_t n )
116{
117 if( use_m_guard ) {
118 unsigned char *p = a;
119 char *b;
120 size_t len;
121
122 if (!a)
123 return _gcry_private_malloc(n);
124
125 _gcry_private_check_heap(p);
126 len = p[-4];
127 len |= p[-3] << 8;
128 len |= p[-2] << 16;
129 if( len >= n ) /* we don't shrink for now */
130 return a;
131 if( p[-1] == MAGIC_SEC_BYTE )
132 b = _gcry_private_malloc_secure(n);
133 else
134 b = _gcry_private_malloc(n);
135 if( !b )
136 return NULL;
137 memcpy(b, a, len );
138 memset(b+len, 0, n-len );
139 _gcry_private_free( p );
140 return b;
141 }
142 else if( _gcry_private_is_secure(a) ) {
143 return _gcry_secmem_realloc( a, n );
144 }
145 else {
146 return realloc( a, n );
147 }
148}
149
150
151void
152_gcry_private_check_heap( const void *a )
153{
154 if( use_m_guard ) {
155 const byte *p = a;
156 size_t len;
157
158 if( !p )
159 return;
160
161 if( !(p[-1] == MAGIC_NOR_BYTE || p[-1] == MAGIC_SEC_BYTE) )
162 _gcry_log_fatal("memory at %p corrupted (underflow=%02x)\n", p, p[-1] );
163 len = p[-4];
164 len |= p[-3] << 8;
165 len |= p[-2] << 16;
166 if( p[len] != MAGIC_END_BYTE )
167 _gcry_log_fatal("memory at %p corrupted (overflow=%02x)\n", p, p[-1] );
168 }
169}
170
171/****************
172 * Free a memory block allocated by this opr the secmem module
173 */
174void
175_gcry_private_free( void *a )
176{
177 byte *p = a;
178
179 if( !p )
180 return;
181 if( use_m_guard ) {
182 _gcry_private_check_heap(p);
183 if( _gcry_private_is_secure(a) )
184 _gcry_secmem_free(p-EXTRA_ALIGN-4);
185 else {
186 free(p-EXTRA_ALIGN-4);
187 }
188 }
189 else if( _gcry_private_is_secure(a) )
190 _gcry_secmem_free(p);
191 else
192 free(p);
193}
194
195
diff --git a/pwmanager/libcrypt/crypt/stdmem.h b/pwmanager/libcrypt/crypt/stdmem.h
new file mode 100644
index 0000000..cf83f68
--- a/dev/null
+++ b/pwmanager/libcrypt/crypt/stdmem.h
@@ -0,0 +1,32 @@
1/* stdmem.h - internal definitions for stdmem
2 *Copyright (C) 2000, 2002 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#ifndef G10_STDMEM_H
22#define G10_STDMEM_H 1
23
24void _gcry_private_enable_m_guard(void);
25
26void *_gcry_private_malloc (size_t n) GCC_ATTR_MALLOC;
27void *_gcry_private_malloc_secure (size_t n) GCC_ATTR_MALLOC;
28void *_gcry_private_realloc (void *a, size_t n);
29void _gcry_private_check_heap (const void *a);
30void _gcry_private_free (void *a);
31
32#endif /* G10_STDMEM_H */
diff --git a/pwmanager/libcrypt/crypt/types.h b/pwmanager/libcrypt/crypt/types.h
new file mode 100644
index 0000000..c3a9d93
--- a/dev/null
+++ b/pwmanager/libcrypt/crypt/types.h
@@ -0,0 +1,124 @@
1/* types.h - some common typedefs
2 *Copyright (C) 1998, 2000, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#ifndef GCRYPT_TYPES_H
22#define GCRYPT_TYPES_H
23
24
25/* The AC_CHECK_SIZEOF() in configure fails for some machines.
26 * we provide some fallback values here */
27#if !SIZEOF_UNSIGNED_SHORT
28#undef SIZEOF_UNSIGNED_SHORT
29#define SIZEOF_UNSIGNED_SHORT 2
30#endif
31#if !SIZEOF_UNSIGNED_INT
32#undef SIZEOF_UNSIGNED_INT
33#define SIZEOF_UNSIGNED_INT 4
34#endif
35#if !SIZEOF_UNSIGNED_LONG
36#undef SIZEOF_UNSIGNED_LONG
37#define SIZEOF_UNSIGNED_LONG 4
38#endif
39
40
41#include <sys/types.h>
42
43
44#ifndef HAVE_BYTE_TYPEDEF
45 #undef byte /* maybe there is a macro with this name */
46 typedef unsigned char byte;
47#define HAVE_BYTE_TYPEDEF
48#endif
49
50#ifndef HAVE_USHORT_TYPEDEF
51#undef ushort /* maybe there is a macro with this name */
52 typedef unsigned short ushort;
53#define HAVE_USHORT_TYPEDEF
54#endif
55
56#ifndef HAVE_ULONG_TYPEDEF
57 #undef ulong /* maybe there is a macro with this name */
58 typedef unsigned long ulong;
59#define HAVE_ULONG_TYPEDEF
60#endif
61
62#ifndef HAVE_U16_TYPEDEF
63 #undef u16 /* maybe there is a macro with this name */
64#if SIZEOF_UNSIGNED_INT == 2
65 typedef unsigned int u16;
66#elif SIZEOF_UNSIGNED_SHORT == 2
67 typedef unsigned short u16;
68#else
69#error no typedef for u16
70#endif
71#define HAVE_U16_TYPEDEF
72#endif
73
74#ifndef HAVE_U32_TYPEDEF
75 #undef u32 /* maybe there is a macro with this name */
76#if SIZEOF_UNSIGNED_INT == 4
77 typedef unsigned int u32;
78#elif SIZEOF_UNSIGNED_LONG == 4
79 typedef unsigned long u32;
80#else
81#error no typedef for u32
82#endif
83#define HAVE_U32_TYPEDEF
84#endif
85
86/****************
87 * Warning: Some systems segfault when this u64 typedef and
88 * the dummy code in cipher/md.c is not available. Examples are
89 * Solaris and IRIX.
90 */
91#ifndef HAVE_U64_TYPEDEF
92 #undef u64 /* maybe there is a macro with this name */
93#if SIZEOF_UNSIGNED_INT == 8
94 typedef unsigned int u64;
95#define U64_C(c) (c ## U)
96#define HAVE_U64_TYPEDEF
97#elif SIZEOF_UNSIGNED_LONG == 8
98 typedef unsigned long u64;
99#define U64_C(c) (c ## UL)
100#define HAVE_U64_TYPEDEF
101#elif SIZEOF_UNSIGNED_LONG_LONG == 8
102 typedef unsigned long long u64;
103#define U64_C(c) (c ## ULL)
104#define HAVE_U64_TYPEDEF
105#elif SIZEOF_UINT64_T == 8
106 typedef uint64_t u64;
107#define U64_C(c) (UINT64_C(c))
108#define HAVE_U64_TYPEDEF
109#endif
110#endif
111
112typedef union {
113 int a;
114 short b;
115 char c[1];
116 long d;
117#ifdef HAVE_U64_TYPEDEF
118 u64 e;
119#endif
120 float f;
121 double g;
122} PROPERLY_ALIGNED_TYPE;
123
124#endif /*GCRYPT_TYPES_H*/
diff --git a/pwmanager/libcrypt/error/code-from-errno.c b/pwmanager/libcrypt/error/code-from-errno.c
new file mode 100644
index 0000000..d431ef1
--- a/dev/null
+++ b/pwmanager/libcrypt/error/code-from-errno.c
@@ -0,0 +1,44 @@
1/* code-from-errno.c - Mapping errnos to error codes.
2 Copyright (C) 2003 g10 Code GmbH
3
4 This file is part of libgpg-error.
5
6 libgpg-error is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License
8 as published by the Free Software Foundation; either version 2.1 of
9 the License, or (at your option) any later version.
10
11 libgpg-error is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with libgpg-error; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21#if HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25#include <gpg-error.h>
26
27#include "code-from-errno.h"
28
29/* Retrieve the error code for the system error ERR. This returns
30 GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report
31 this). */
32gpg_err_code_t
33gpg_err_code_from_errno (int err)
34{
35 int idx = errno_to_idx (err);
36
37 if (!err)
38 return GPG_ERR_NO_ERROR;
39
40 if (idx < 0)
41 return GPG_ERR_UNKNOWN_ERRNO;
42
43 return GPG_ERR_SYSTEM_ERROR | err_code_from_index[idx];
44}
diff --git a/pwmanager/libcrypt/error/code-from-errno.h b/pwmanager/libcrypt/error/code-from-errno.h
new file mode 100644
index 0000000..8730ff0
--- a/dev/null
+++ b/pwmanager/libcrypt/error/code-from-errno.h
@@ -0,0 +1,158 @@
1/* Output of mkerrcodes2.awk. DO NOT EDIT. */
2
3/* errnos.h - List of system error values.
4 Copyright (C) 2004 g10 Code GmbH
5 This file is part of libgpg-error.
6
7 libgpg-error is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public License
9 as published by the Free Software Foundation; either version 2.1 of
10 the License, or (at your option) any later version.
11
12 libgpg-error is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgpg-error; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22static const int err_code_from_index[] = {
23 GPG_ERR_EPERM,
24 GPG_ERR_ENOENT,
25 GPG_ERR_ESRCH,
26 GPG_ERR_EINTR,
27 GPG_ERR_EIO,
28 GPG_ERR_ENXIO,
29 GPG_ERR_E2BIG,
30 GPG_ERR_ENOEXEC,
31 GPG_ERR_EBADF,
32 GPG_ERR_ECHILD,
33 GPG_ERR_EAGAIN,
34 GPG_ERR_EWOULDBLOCK,
35 GPG_ERR_ENOMEM,
36 GPG_ERR_EACCES,
37 GPG_ERR_EFAULT,
38 GPG_ERR_ENOTBLK,
39 GPG_ERR_EBUSY,
40 GPG_ERR_EEXIST,
41 GPG_ERR_EXDEV,
42 GPG_ERR_ENODEV,
43 GPG_ERR_ENOTDIR,
44 GPG_ERR_EISDIR,
45 GPG_ERR_EINVAL,
46 GPG_ERR_ENFILE,
47 GPG_ERR_EMFILE,
48 GPG_ERR_ENOTTY,
49 GPG_ERR_ETXTBSY,
50 GPG_ERR_EFBIG,
51 GPG_ERR_ENOSPC,
52 GPG_ERR_ESPIPE,
53 GPG_ERR_EROFS,
54 GPG_ERR_EMLINK,
55 GPG_ERR_EPIPE,
56 GPG_ERR_EDOM,
57 GPG_ERR_ERANGE,
58 GPG_ERR_EDEADLK,
59 GPG_ERR_EDEADLOCK,
60 GPG_ERR_ENAMETOOLONG,
61 GPG_ERR_ENOLCK,
62 GPG_ERR_ENOSYS,
63 GPG_ERR_ENOTEMPTY,
64 GPG_ERR_ELOOP,
65 GPG_ERR_ENOMSG,
66 GPG_ERR_EIDRM,
67 GPG_ERR_ECHRNG,
68 GPG_ERR_EL2NSYNC,
69 GPG_ERR_EL3HLT,
70 GPG_ERR_EL3RST,
71 GPG_ERR_ELNRNG,
72 GPG_ERR_EUNATCH,
73 GPG_ERR_ENOCSI,
74 GPG_ERR_EL2HLT,
75 GPG_ERR_EBADE,
76 GPG_ERR_EBADR,
77 GPG_ERR_EXFULL,
78 GPG_ERR_ENOANO,
79 GPG_ERR_EBADRQC,
80 GPG_ERR_EBADSLT,
81 GPG_ERR_EBFONT,
82 GPG_ERR_ENOSTR,
83 GPG_ERR_ENODATA,
84 GPG_ERR_ETIME,
85 GPG_ERR_ENOSR,
86 GPG_ERR_ENONET,
87 GPG_ERR_ENOPKG,
88 GPG_ERR_EREMOTE,
89 GPG_ERR_ENOLINK,
90 GPG_ERR_EADV,
91 GPG_ERR_ESRMNT,
92 GPG_ERR_ECOMM,
93 GPG_ERR_EPROTO,
94 GPG_ERR_EMULTIHOP,
95 GPG_ERR_EDOTDOT,
96 GPG_ERR_EBADMSG,
97 GPG_ERR_EOVERFLOW,
98 GPG_ERR_ENOTUNIQ,
99 GPG_ERR_EBADFD,
100 GPG_ERR_EREMCHG,
101 GPG_ERR_ELIBACC,
102 GPG_ERR_ELIBBAD,
103 GPG_ERR_ELIBSCN,
104 GPG_ERR_ELIBMAX,
105 GPG_ERR_ELIBEXEC,
106 GPG_ERR_EILSEQ,
107 GPG_ERR_ERESTART,
108 GPG_ERR_ESTRPIPE,
109 GPG_ERR_EUSERS,
110 GPG_ERR_ENOTSOCK,
111 GPG_ERR_EDESTADDRREQ,
112 GPG_ERR_EMSGSIZE,
113 GPG_ERR_EPROTOTYPE,
114 GPG_ERR_ENOPROTOOPT,
115 GPG_ERR_EPROTONOSUPPORT,
116 GPG_ERR_ESOCKTNOSUPPORT,
117 GPG_ERR_ENOTSUP,
118 GPG_ERR_EOPNOTSUPP,
119 GPG_ERR_EPFNOSUPPORT,
120 GPG_ERR_EAFNOSUPPORT,
121 GPG_ERR_EADDRINUSE,
122 GPG_ERR_EADDRNOTAVAIL,
123 GPG_ERR_ENETDOWN,
124 GPG_ERR_ENETUNREACH,
125 GPG_ERR_ENETRESET,
126 GPG_ERR_ECONNABORTED,
127 GPG_ERR_ECONNRESET,
128 GPG_ERR_ENOBUFS,
129 GPG_ERR_EISCONN,
130 GPG_ERR_ENOTCONN,
131 GPG_ERR_ESHUTDOWN,
132 GPG_ERR_ETOOMANYREFS,
133 GPG_ERR_ETIMEDOUT,
134 GPG_ERR_ECONNREFUSED,
135 GPG_ERR_EHOSTDOWN,
136 GPG_ERR_EHOSTUNREACH,
137 GPG_ERR_EALREADY,
138 GPG_ERR_EINPROGRESS,
139 GPG_ERR_ESTALE,
140 GPG_ERR_EUCLEAN,
141 GPG_ERR_ENOTNAM,
142 GPG_ERR_ENAVAIL,
143 GPG_ERR_EISNAM,
144 GPG_ERR_EREMOTEIO,
145 GPG_ERR_EDQUOT,
146 GPG_ERR_ENOMEDIUM,
147 GPG_ERR_EMEDIUMTYPE,
148 GPG_ERR_ECANCELED,
149};
150
151#define errno_to_idx(code) (0 ? -1 \
152 : ((code >= 1) && (code <= 11)) ? (code - 1) \
153 : ((code >= 11) && (code <= 35)) ? (code - 0) \
154 : ((code >= 35) && (code <= 40)) ? (code - -1) \
155 : ((code >= 42) && (code <= 57)) ? (code - 0) \
156 : ((code >= 59) && (code <= 95)) ? (code - 1) \
157 : ((code >= 95) && (code <= 125)) ? (code - 0) \
158 : -1)
diff --git a/pwmanager/libcrypt/error/code-to-errno.c b/pwmanager/libcrypt/error/code-to-errno.c
new file mode 100644
index 0000000..5873aad
--- a/dev/null
+++ b/pwmanager/libcrypt/error/code-to-errno.c
@@ -0,0 +1,42 @@
1/* code-to-errno.c - Mapping error codes to errnos.
2 Copyright (C) 2003 g10 Code GmbH
3
4 This file is part of libgpg-error.
5
6 libgpg-error is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License
8 as published by the Free Software Foundation; either version 2.1 of
9 the License, or (at your option) any later version.
10
11 libgpg-error is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with libgpg-error; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21#if HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25#include <gpg-error.h>
26
27#include "code-to-errno.h"
28
29/* Retrieve the system error for the error code CODE. This returns 0
30 if CODE is not a system error code. */
31int
32gpg_err_code_to_errno (gpg_err_code_t code)
33{
34 if (!(code & GPG_ERR_SYSTEM_ERROR))
35 return 0;
36 code &= ~GPG_ERR_SYSTEM_ERROR;
37
38 if (code < sizeof (err_code_to_errno) / sizeof (err_code_to_errno[0]))
39 return err_code_to_errno[code];
40 else
41 return 0;
42}
diff --git a/pwmanager/libcrypt/error/code-to-errno.h b/pwmanager/libcrypt/error/code-to-errno.h
new file mode 100644
index 0000000..81bf4e9
--- a/dev/null
+++ b/pwmanager/libcrypt/error/code-to-errno.h
@@ -0,0 +1,733 @@
1/* Output of mkerrnos.awk. DO NOT EDIT. */
2
3/* errnos.h - List of system error values.
4 Copyright (C) 2003, 2004 g10 Code GmbH
5
6 This file is part of libgpg-error.
7
8 libgpg-error is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public License
10 as published by the Free Software Foundation; either version 2.1 of
11 the License, or (at your option) any later version.
12
13 libgpg-error is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with libgpg-error; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
22
23
24
25#include <errno.h>
26
27static const int err_code_to_errno [] = {
28#ifdef E2BIG
29 E2BIG,
30#else
31 0,
32#endif
33#ifdef EACCES
34 EACCES,
35#else
36 0,
37#endif
38#ifdef EADDRINUSE
39 EADDRINUSE,
40#else
41 0,
42#endif
43#ifdef EADDRNOTAVAIL
44 EADDRNOTAVAIL,
45#else
46 0,
47#endif
48#ifdef EADV
49 EADV,
50#else
51 0,
52#endif
53#ifdef EAFNOSUPPORT
54 EAFNOSUPPORT,
55#else
56 0,
57#endif
58#ifdef EAGAIN
59 EAGAIN,
60#else
61 0,
62#endif
63#ifdef EALREADY
64 EALREADY,
65#else
66 0,
67#endif
68#ifdef EAUTH
69 EAUTH,
70#else
71 0,
72#endif
73#ifdef EBACKGROUND
74 EBACKGROUND,
75#else
76 0,
77#endif
78#ifdef EBADE
79 EBADE,
80#else
81 0,
82#endif
83#ifdef EBADF
84 EBADF,
85#else
86 0,
87#endif
88#ifdef EBADFD
89 EBADFD,
90#else
91 0,
92#endif
93#ifdef EBADMSG
94 EBADMSG,
95#else
96 0,
97#endif
98#ifdef EBADR
99 EBADR,
100#else
101 0,
102#endif
103#ifdef EBADRPC
104 EBADRPC,
105#else
106 0,
107#endif
108#ifdef EBADRQC
109 EBADRQC,
110#else
111 0,
112#endif
113#ifdef EBADSLT
114 EBADSLT,
115#else
116 0,
117#endif
118#ifdef EBFONT
119 EBFONT,
120#else
121 0,
122#endif
123#ifdef EBUSY
124 EBUSY,
125#else
126 0,
127#endif
128#ifdef ECANCELED
129 ECANCELED,
130#else
131 0,
132#endif
133#ifdef ECHILD
134 ECHILD,
135#else
136 0,
137#endif
138#ifdef ECHRNG
139 ECHRNG,
140#else
141 0,
142#endif
143#ifdef ECOMM
144 ECOMM,
145#else
146 0,
147#endif
148#ifdef ECONNABORTED
149 ECONNABORTED,
150#else
151 0,
152#endif
153#ifdef ECONNREFUSED
154 ECONNREFUSED,
155#else
156 0,
157#endif
158#ifdef ECONNRESET
159 ECONNRESET,
160#else
161 0,
162#endif
163#ifdef ED
164 ED,
165#else
166 0,
167#endif
168#ifdef EDEADLK
169 EDEADLK,
170#else
171 0,
172#endif
173#ifdef EDEADLOCK
174 EDEADLOCK,
175#else
176 0,
177#endif
178#ifdef EDESTADDRREQ
179 EDESTADDRREQ,
180#else
181 0,
182#endif
183#ifdef EDIED
184 EDIED,
185#else
186 0,
187#endif
188#ifdef EDOM
189 EDOM,
190#else
191 0,
192#endif
193#ifdef EDOTDOT
194 EDOTDOT,
195#else
196 0,
197#endif
198#ifdef EDQUOT
199 EDQUOT,
200#else
201 0,
202#endif
203#ifdef EEXIST
204 EEXIST,
205#else
206 0,
207#endif
208#ifdef EFAULT
209 EFAULT,
210#else
211 0,
212#endif
213#ifdef EFBIG
214 EFBIG,
215#else
216 0,
217#endif
218#ifdef EFTYPE
219 EFTYPE,
220#else
221 0,
222#endif
223#ifdef EGRATUITOUS
224 EGRATUITOUS,
225#else
226 0,
227#endif
228#ifdef EGREGIOUS
229 EGREGIOUS,
230#else
231 0,
232#endif
233#ifdef EHOSTDOWN
234 EHOSTDOWN,
235#else
236 0,
237#endif
238#ifdef EHOSTUNREACH
239 EHOSTUNREACH,
240#else
241 0,
242#endif
243#ifdef EIDRM
244 EIDRM,
245#else
246 0,
247#endif
248#ifdef EIEIO
249 EIEIO,
250#else
251 0,
252#endif
253#ifdef EILSEQ
254 EILSEQ,
255#else
256 0,
257#endif
258#ifdef EINPROGRESS
259 EINPROGRESS,
260#else
261 0,
262#endif
263#ifdef EINTR
264 EINTR,
265#else
266 0,
267#endif
268#ifdef EINVAL
269 EINVAL,
270#else
271 0,
272#endif
273#ifdef EIO
274 EIO,
275#else
276 0,
277#endif
278#ifdef EISCONN
279 EISCONN,
280#else
281 0,
282#endif
283#ifdef EISDIR
284 EISDIR,
285#else
286 0,
287#endif
288#ifdef EISNAM
289 EISNAM,
290#else
291 0,
292#endif
293#ifdef EL2HLT
294 EL2HLT,
295#else
296 0,
297#endif
298#ifdef EL2NSYNC
299 EL2NSYNC,
300#else
301 0,
302#endif
303#ifdef EL3HLT
304 EL3HLT,
305#else
306 0,
307#endif
308#ifdef EL3RST
309 EL3RST,
310#else
311 0,
312#endif
313#ifdef ELIBACC
314 ELIBACC,
315#else
316 0,
317#endif
318#ifdef ELIBBAD
319 ELIBBAD,
320#else
321 0,
322#endif
323#ifdef ELIBEXEC
324 ELIBEXEC,
325#else
326 0,
327#endif
328#ifdef ELIBMAX
329 ELIBMAX,
330#else
331 0,
332#endif
333#ifdef ELIBSCN
334 ELIBSCN,
335#else
336 0,
337#endif
338#ifdef ELNRNG
339 ELNRNG,
340#else
341 0,
342#endif
343#ifdef ELOOP
344 ELOOP,
345#else
346 0,
347#endif
348#ifdef EMEDIUMTYPE
349 EMEDIUMTYPE,
350#else
351 0,
352#endif
353#ifdef EMFILE
354 EMFILE,
355#else
356 0,
357#endif
358#ifdef EMLINK
359 EMLINK,
360#else
361 0,
362#endif
363#ifdef EMSGSIZE
364 EMSGSIZE,
365#else
366 0,
367#endif
368#ifdef EMULTIHOP
369 EMULTIHOP,
370#else
371 0,
372#endif
373#ifdef ENAMETOOLONG
374 ENAMETOOLONG,
375#else
376 0,
377#endif
378#ifdef ENAVAIL
379 ENAVAIL,
380#else
381 0,
382#endif
383#ifdef ENEEDAUTH
384 ENEEDAUTH,
385#else
386 0,
387#endif
388#ifdef ENETDOWN
389 ENETDOWN,
390#else
391 0,
392#endif
393#ifdef ENETRESET
394 ENETRESET,
395#else
396 0,
397#endif
398#ifdef ENETUNREACH
399 ENETUNREACH,
400#else
401 0,
402#endif
403#ifdef ENFILE
404 ENFILE,
405#else
406 0,
407#endif
408#ifdef ENOANO
409 ENOANO,
410#else
411 0,
412#endif
413#ifdef ENOBUFS
414 ENOBUFS,
415#else
416 0,
417#endif
418#ifdef ENOCSI
419 ENOCSI,
420#else
421 0,
422#endif
423#ifdef ENODATA
424 ENODATA,
425#else
426 0,
427#endif
428#ifdef ENODEV
429 ENODEV,
430#else
431 0,
432#endif
433#ifdef ENOENT
434 ENOENT,
435#else
436 0,
437#endif
438#ifdef ENOEXEC
439 ENOEXEC,
440#else
441 0,
442#endif
443#ifdef ENOLCK
444 ENOLCK,
445#else
446 0,
447#endif
448#ifdef ENOLINK
449 ENOLINK,
450#else
451 0,
452#endif
453#ifdef ENOMEDIUM
454 ENOMEDIUM,
455#else
456 0,
457#endif
458#ifdef ENOMEM
459 ENOMEM,
460#else
461 0,
462#endif
463#ifdef ENOMSG
464 ENOMSG,
465#else
466 0,
467#endif
468#ifdef ENONET
469 ENONET,
470#else
471 0,
472#endif
473#ifdef ENOPKG
474 ENOPKG,
475#else
476 0,
477#endif
478#ifdef ENOPROTOOPT
479 ENOPROTOOPT,
480#else
481 0,
482#endif
483#ifdef ENOSPC
484 ENOSPC,
485#else
486 0,
487#endif
488#ifdef ENOSR
489 ENOSR,
490#else
491 0,
492#endif
493#ifdef ENOSTR
494 ENOSTR,
495#else
496 0,
497#endif
498#ifdef ENOSYS
499 ENOSYS,
500#else
501 0,
502#endif
503#ifdef ENOTBLK
504 ENOTBLK,
505#else
506 0,
507#endif
508#ifdef ENOTCONN
509 ENOTCONN,
510#else
511 0,
512#endif
513#ifdef ENOTDIR
514 ENOTDIR,
515#else
516 0,
517#endif
518#ifdef ENOTEMPTY
519 ENOTEMPTY,
520#else
521 0,
522#endif
523#ifdef ENOTNAM
524 ENOTNAM,
525#else
526 0,
527#endif
528#ifdef ENOTSOCK
529 ENOTSOCK,
530#else
531 0,
532#endif
533#ifdef ENOTSUP
534 ENOTSUP,
535#else
536 0,
537#endif
538#ifdef ENOTTY
539 ENOTTY,
540#else
541 0,
542#endif
543#ifdef ENOTUNIQ
544 ENOTUNIQ,
545#else
546 0,
547#endif
548#ifdef ENXIO
549 ENXIO,
550#else
551 0,
552#endif
553#ifdef EOPNOTSUPP
554 EOPNOTSUPP,
555#else
556 0,
557#endif
558#ifdef EOVERFLOW
559 EOVERFLOW,
560#else
561 0,
562#endif
563#ifdef EPERM
564 EPERM,
565#else
566 0,
567#endif
568#ifdef EPFNOSUPPORT
569 EPFNOSUPPORT,
570#else
571 0,
572#endif
573#ifdef EPIPE
574 EPIPE,
575#else
576 0,
577#endif
578#ifdef EPROCLIM
579 EPROCLIM,
580#else
581 0,
582#endif
583#ifdef EPROCUNAVAIL
584 EPROCUNAVAIL,
585#else
586 0,
587#endif
588#ifdef EPROGMISMATCH
589 EPROGMISMATCH,
590#else
591 0,
592#endif
593#ifdef EPROGUNAVAIL
594 EPROGUNAVAIL,
595#else
596 0,
597#endif
598#ifdef EPROTO
599 EPROTO,
600#else
601 0,
602#endif
603#ifdef EPROTONOSUPPORT
604 EPROTONOSUPPORT,
605#else
606 0,
607#endif
608#ifdef EPROTOTYPE
609 EPROTOTYPE,
610#else
611 0,
612#endif
613#ifdef ERANGE
614 ERANGE,
615#else
616 0,
617#endif
618#ifdef EREMCHG
619 EREMCHG,
620#else
621 0,
622#endif
623#ifdef EREMOTE
624 EREMOTE,
625#else
626 0,
627#endif
628#ifdef EREMOTEIO
629 EREMOTEIO,
630#else
631 0,
632#endif
633#ifdef ERESTART
634 ERESTART,
635#else
636 0,
637#endif
638#ifdef EROFS
639 EROFS,
640#else
641 0,
642#endif
643#ifdef ERPCMISMATCH
644 ERPCMISMATCH,
645#else
646 0,
647#endif
648#ifdef ESHUTDOWN
649 ESHUTDOWN,
650#else
651 0,
652#endif
653#ifdef ESOCKTNOSUPPORT
654 ESOCKTNOSUPPORT,
655#else
656 0,
657#endif
658#ifdef ESPIPE
659 ESPIPE,
660#else
661 0,
662#endif
663#ifdef ESRCH
664 ESRCH,
665#else
666 0,
667#endif
668#ifdef ESRMNT
669 ESRMNT,
670#else
671 0,
672#endif
673#ifdef ESTALE
674 ESTALE,
675#else
676 0,
677#endif
678#ifdef ESTRPIPE
679 ESTRPIPE,
680#else
681 0,
682#endif
683#ifdef ETIME
684 ETIME,
685#else
686 0,
687#endif
688#ifdef ETIMEDOUT
689 ETIMEDOUT,
690#else
691 0,
692#endif
693#ifdef ETOOMANYREFS
694 ETOOMANYREFS,
695#else
696 0,
697#endif
698#ifdef ETXTBSY
699 ETXTBSY,
700#else
701 0,
702#endif
703#ifdef EUCLEAN
704 EUCLEAN,
705#else
706 0,
707#endif
708#ifdef EUNATCH
709 EUNATCH,
710#else
711 0,
712#endif
713#ifdef EUSERS
714 EUSERS,
715#else
716 0,
717#endif
718#ifdef EWOULDBLOCK
719 EWOULDBLOCK,
720#else
721 0,
722#endif
723#ifdef EXDEV
724 EXDEV,
725#else
726 0,
727#endif
728#ifdef EXFULL
729 EXFULL,
730#else
731 0,
732#endif
733};
diff --git a/pwmanager/libcrypt/error/err-codes-sym.h b/pwmanager/libcrypt/error/err-codes-sym.h
new file mode 100644
index 0000000..a9d7a75
--- a/dev/null
+++ b/pwmanager/libcrypt/error/err-codes-sym.h
@@ -0,0 +1,446 @@
1/* Output of mkstrtable.awk. DO NOT EDIT. */
2
3/* err-codes.h - List of error codes and their description.
4 Copyright (C) 2003, 2004 g10 Code GmbH
5
6 This file is part of libgpg-error.
7
8 libgpg-error is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public License
10 as published by the Free Software Foundation; either version 2.1 of
11 the License, or (at your option) any later version.
12
13 libgpg-error is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with libgpg-error; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
22
23
24/* The purpose of this complex string table is to produce
25 optimal code with a minimum of relocations. */
26
27static const char msgstr[] =
28 "GPG_ERR_NO_ERROR" "\0"
29 "GPG_ERR_GENERAL" "\0"
30 "GPG_ERR_UNKNOWN_PACKET" "\0"
31 "GPG_ERR_UNKNOWN_VERSION" "\0"
32 "GPG_ERR_PUBKEY_ALGO" "\0"
33 "GPG_ERR_DIGEST_ALGO" "\0"
34 "GPG_ERR_BAD_PUBKEY" "\0"
35 "GPG_ERR_BAD_SECKEY" "\0"
36 "GPG_ERR_BAD_SIGNATURE" "\0"
37 "GPG_ERR_NO_PUBKEY" "\0"
38 "GPG_ERR_CHECKSUM" "\0"
39 "GPG_ERR_BAD_PASSPHRASE" "\0"
40 "GPG_ERR_CIPHER_ALGO" "\0"
41 "GPG_ERR_KEYRING_OPEN" "\0"
42 "GPG_ERR_INV_PACKET" "\0"
43 "GPG_ERR_INV_ARMOR" "\0"
44 "GPG_ERR_NO_USER_ID" "\0"
45 "GPG_ERR_NO_SECKEY" "\0"
46 "GPG_ERR_WRONG_SECKEY" "\0"
47 "GPG_ERR_BAD_KEY" "\0"
48 "GPG_ERR_COMPR_ALGO" "\0"
49 "GPG_ERR_NO_PRIME" "\0"
50 "GPG_ERR_NO_ENCODING_METHOD" "\0"
51 "GPG_ERR_NO_ENCRYPTION_SCHEME" "\0"
52 "GPG_ERR_NO_SIGNATURE_SCHEME" "\0"
53 "GPG_ERR_INV_ATTR" "\0"
54 "GPG_ERR_NO_VALUE" "\0"
55 "GPG_ERR_NOT_FOUND" "\0"
56 "GPG_ERR_VALUE_NOT_FOUND" "\0"
57 "GPG_ERR_SYNTAX" "\0"
58 "GPG_ERR_BAD_MPI" "\0"
59 "GPG_ERR_INV_PASSPHRASE" "\0"
60 "GPG_ERR_SIG_CLASS" "\0"
61 "GPG_ERR_RESOURCE_LIMIT" "\0"
62 "GPG_ERR_INV_KEYRING" "\0"
63 "GPG_ERR_TRUSTDB" "\0"
64 "GPG_ERR_BAD_CERT" "\0"
65 "GPG_ERR_INV_USER_ID" "\0"
66 "GPG_ERR_UNEXPECTED" "\0"
67 "GPG_ERR_TIME_CONFLICT" "\0"
68 "GPG_ERR_KEYSERVER" "\0"
69 "GPG_ERR_WRONG_PUBKEY_ALGO" "\0"
70 "GPG_ERR_TRIBUTE_TO_D_A" "\0"
71 "GPG_ERR_WEAK_KEY" "\0"
72 "GPG_ERR_INV_KEYLEN" "\0"
73 "GPG_ERR_INV_ARG" "\0"
74 "GPG_ERR_BAD_URI" "\0"
75 "GPG_ERR_INV_URI" "\0"
76 "GPG_ERR_NETWORK" "\0"
77 "GPG_ERR_UNKNOWN_HOST" "\0"
78 "GPG_ERR_SELFTEST_FAILED" "\0"
79 "GPG_ERR_NOT_ENCRYPTED" "\0"
80 "GPG_ERR_NOT_PROCESSED" "\0"
81 "GPG_ERR_UNUSABLE_PUBKEY" "\0"
82 "GPG_ERR_UNUSABLE_SECKEY" "\0"
83 "GPG_ERR_INV_VALUE" "\0"
84 "GPG_ERR_BAD_CERT_CHAIN" "\0"
85 "GPG_ERR_MISSING_CERT" "\0"
86 "GPG_ERR_NO_DATA" "\0"
87 "GPG_ERR_BUG" "\0"
88 "GPG_ERR_NOT_SUPPORTED" "\0"
89 "GPG_ERR_INV_OP" "\0"
90 "GPG_ERR_TIMEOUT" "\0"
91 "GPG_ERR_INTERNAL" "\0"
92 "GPG_ERR_EOF_GCRYPT" "\0"
93 "GPG_ERR_INV_OBJ" "\0"
94 "GPG_ERR_TOO_SHORT" "\0"
95 "GPG_ERR_TOO_LARGE" "\0"
96 "GPG_ERR_NO_OBJ" "\0"
97 "GPG_ERR_NOT_IMPLEMENTED" "\0"
98 "GPG_ERR_CONFLICT" "\0"
99 "GPG_ERR_INV_CIPHER_MODE" "\0"
100 "GPG_ERR_INV_FLAG" "\0"
101 "GPG_ERR_INV_HANDLE" "\0"
102 "GPG_ERR_TRUNCATED" "\0"
103 "GPG_ERR_INCOMPLETE_LINE" "\0"
104 "GPG_ERR_INV_RESPONSE" "\0"
105 "GPG_ERR_NO_AGENT" "\0"
106 "GPG_ERR_AGENT" "\0"
107 "GPG_ERR_INV_DATA" "\0"
108 "GPG_ERR_ASSUAN_SERVER_FAULT" "\0"
109 "GPG_ERR_ASSUAN" "\0"
110 "GPG_ERR_INV_SESSION_KEY" "\0"
111 "GPG_ERR_INV_SEXP" "\0"
112 "GPG_ERR_UNSUPPORTED_ALGORITHM" "\0"
113 "GPG_ERR_NO_PIN_ENTRY" "\0"
114 "GPG_ERR_PIN_ENTRY" "\0"
115 "GPG_ERR_BAD_PIN" "\0"
116 "GPG_ERR_INV_NAME" "\0"
117 "GPG_ERR_BAD_DATA" "\0"
118 "GPG_ERR_INV_PARAMETER" "\0"
119 "GPG_ERR_WRONG_CARD" "\0"
120 "GPG_ERR_NO_DIRMNGR" "\0"
121 "GPG_ERR_DIRMNGR" "\0"
122 "GPG_ERR_CERT_REVOKED" "\0"
123 "GPG_ERR_NO_CRL_KNOWN" "\0"
124 "GPG_ERR_CRL_TOO_OLD" "\0"
125 "GPG_ERR_LINE_TOO_LONG" "\0"
126 "GPG_ERR_NOT_TRUSTED" "\0"
127 "GPG_ERR_CANCELED" "\0"
128 "GPG_ERR_BAD_CA_CERT" "\0"
129 "GPG_ERR_CERT_EXPIRED" "\0"
130 "GPG_ERR_CERT_TOO_YOUNG" "\0"
131 "GPG_ERR_UNSUPPORTED_CERT" "\0"
132 "GPG_ERR_UNKNOWN_SEXP" "\0"
133 "GPG_ERR_UNSUPPORTED_PROTECTION" "\0"
134 "GPG_ERR_CORRUPTED_PROTECTION" "\0"
135 "GPG_ERR_AMBIGUOUS_NAME" "\0"
136 "GPG_ERR_CARD" "\0"
137 "GPG_ERR_CARD_RESET" "\0"
138 "GPG_ERR_CARD_REMOVED" "\0"
139 "GPG_ERR_INV_CARD" "\0"
140 "GPG_ERR_CARD_NOT_PRESENT" "\0"
141 "GPG_ERR_NO_PKCS15_APP" "\0"
142 "GPG_ERR_NOT_CONFIRMED" "\0"
143 "GPG_ERR_CONFIGURATION" "\0"
144 "GPG_ERR_NO_POLICY_MATCH" "\0"
145 "GPG_ERR_INV_INDEX" "\0"
146 "GPG_ERR_INV_ID" "\0"
147 "GPG_ERR_NO_SCDAEMON" "\0"
148 "GPG_ERR_SCDAEMON" "\0"
149 "GPG_ERR_UNSUPPORTED_PROTOCOL" "\0"
150 "GPG_ERR_BAD_PIN_METHOD" "\0"
151 "GPG_ERR_CARD_NOT_INITIALIZED" "\0"
152 "GPG_ERR_UNSUPPORTED_OPERATION" "\0"
153 "GPG_ERR_WRONG_KEY_USAGE" "\0"
154 "GPG_ERR_NOTHING_FOUND" "\0"
155 "GPG_ERR_WRONG_BLOB_TYPE" "\0"
156 "GPG_ERR_MISSING_VALUE" "\0"
157 "GPG_ERR_HARDWARE" "\0"
158 "GPG_ERR_PIN_BLOCKED" "\0"
159 "GPG_ERR_USE_CONDITIONS" "\0"
160 "GPG_ERR_PIN_NOT_SYNCED" "\0"
161 "GPG_ERR_INV_CRL" "\0"
162 "GPG_ERR_BAD_BER" "\0"
163 "GPG_ERR_INV_BER" "\0"
164 "GPG_ERR_ELEMENT_NOT_FOUND" "\0"
165 "GPG_ERR_IDENTIFIER_NOT_FOUND" "\0"
166 "GPG_ERR_INV_TAG" "\0"
167 "GPG_ERR_INV_LENGTH" "\0"
168 "GPG_ERR_INV_KEYINFO" "\0"
169 "GPG_ERR_UNEXPECTED_TAG" "\0"
170 "GPG_ERR_NOT_DER_ENCODED" "\0"
171 "GPG_ERR_NO_CMS_OBJ" "\0"
172 "GPG_ERR_INV_CMS_OBJ" "\0"
173 "GPG_ERR_UNKNOWN_CMS_OBJ" "\0"
174 "GPG_ERR_UNSUPPORTED_CMS_OBJ" "\0"
175 "GPG_ERR_UNSUPPORTED_ENCODING" "\0"
176 "GPG_ERR_UNSUPPORTED_CMS_VERSION" "\0"
177 "GPG_ERR_UNKNOWN_ALGORITHM" "\0"
178 "GPG_ERR_INV_ENGINE" "\0"
179 "GPG_ERR_PUBKEY_NOT_TRUSTED" "\0"
180 "GPG_ERR_DECRYPT_FAILED" "\0"
181 "GPG_ERR_KEY_EXPIRED" "\0"
182 "GPG_ERR_SIG_EXPIRED" "\0"
183 "GPG_ERR_ENCODING_PROBLEM" "\0"
184 "GPG_ERR_INV_STATE" "\0"
185 "GPG_ERR_DUP_VALUE" "\0"
186 "GPG_ERR_MISSING_ACTION" "\0"
187 "GPG_ERR_MODULE_NOT_FOUND" "\0"
188 "GPG_ERR_INV_OID_STRING" "\0"
189 "GPG_ERR_INV_TIME" "\0"
190 "GPG_ERR_INV_CRL_OBJ" "\0"
191 "GPG_ERR_UNSUPPORTED_CRL_VERSION" "\0"
192 "GPG_ERR_INV_CERT_OBJ" "\0"
193 "GPG_ERR_UNKNOWN_NAME" "\0"
194 "GPG_ERR_LOCALE_PROBLEM" "\0"
195 "GPG_ERR_NOT_LOCKED" "\0"
196 "GPG_ERR_PROTOCOL_VIOLATION" "\0"
197 "GPG_ERR_INV_MAC" "\0"
198 "GPG_ERR_INV_REQUEST" "\0"
199 "GPG_ERR_BUFFER_TOO_SHORT" "\0"
200 "GPG_ERR_SEXP_INV_LEN_SPEC" "\0"
201 "GPG_ERR_SEXP_STRING_TOO_LONG" "\0"
202 "GPG_ERR_SEXP_UNMATCHED_PAREN" "\0"
203 "GPG_ERR_SEXP_NOT_CANONICAL" "\0"
204 "GPG_ERR_SEXP_BAD_CHARACTER" "\0"
205 "GPG_ERR_SEXP_BAD_QUOTATION" "\0"
206 "GPG_ERR_SEXP_ZERO_PREFIX" "\0"
207 "GPG_ERR_SEXP_NESTED_DH" "\0"
208 "GPG_ERR_SEXP_UNMATCHED_DH" "\0"
209 "GPG_ERR_SEXP_UNEXPECTED_PUNC" "\0"
210 "GPG_ERR_SEXP_BAD_HEX_CHAR" "\0"
211 "GPG_ERR_SEXP_ODD_HEX_NUMBERS" "\0"
212 "GPG_ERR_SEXP_BAD_OCT_CHAR" "\0"
213 "GPG_ERR_USER_1" "\0"
214 "GPG_ERR_USER_2" "\0"
215 "GPG_ERR_USER_3" "\0"
216 "GPG_ERR_USER_4" "\0"
217 "GPG_ERR_USER_5" "\0"
218 "GPG_ERR_USER_6" "\0"
219 "GPG_ERR_USER_7" "\0"
220 "GPG_ERR_USER_8" "\0"
221 "GPG_ERR_USER_9" "\0"
222 "GPG_ERR_USER_10" "\0"
223 "GPG_ERR_USER_11" "\0"
224 "GPG_ERR_USER_12" "\0"
225 "GPG_ERR_USER_13" "\0"
226 "GPG_ERR_USER_14" "\0"
227 "GPG_ERR_USER_15" "\0"
228 "GPG_ERR_USER_16" "\0"
229 "GPG_ERR_UNKNOWN_ERRNO" "\0"
230 "GPG_ERR_EOF" "\0"
231 "GPG_ERR_CODE_DIM";
232
233static const int msgidx[] =
234 {
235 0,
236 17,
237 33,
238 56,
239 80,
240 100,
241 120,
242 139,
243 158,
244 180,
245 198,
246 215,
247 238,
248 258,
249 279,
250 298,
251 316,
252 335,
253 353,
254 374,
255 390,
256 409,
257 426,
258 453,
259 482,
260 510,
261 527,
262 544,
263 562,
264 586,
265 601,
266 617,
267 640,
268 658,
269 681,
270 701,
271 717,
272 734,
273 754,
274 773,
275 795,
276 813,
277 839,
278 862,
279 879,
280 898,
281 914,
282 930,
283 946,
284 962,
285 983,
286 1007,
287 1029,
288 1051,
289 1075,
290 1099,
291 1117,
292 1140,
293 1161,
294 1177,
295 1189,
296 1211,
297 1226,
298 1242,
299 1259,
300 1278,
301 1294,
302 1312,
303 1330,
304 1345,
305 1369,
306 1386,
307 1410,
308 1427,
309 1446,
310 1464,
311 1488,
312 1509,
313 1526,
314 1540,
315 1557,
316 1585,
317 1600,
318 1624,
319 1641,
320 1671,
321 1692,
322 1710,
323 1726,
324 1743,
325 1760,
326 1782,
327 1801,
328 1820,
329 1836,
330 1857,
331 1878,
332 1898,
333 1920,
334 1940,
335 1957,
336 1977,
337 1998,
338 2021,
339 2046,
340 2067,
341 2098,
342 2127,
343 2150,
344 2163,
345 2182,
346 2203,
347 2220,
348 2245,
349 2267,
350 2289,
351 2311,
352 2335,
353 2353,
354 2368,
355 2388,
356 2405,
357 2434,
358 2457,
359 2486,
360 2516,
361 2540,
362 2562,
363 2586,
364 2608,
365 2625,
366 2645,
367 2668,
368 2691,
369 2707,
370 2723,
371 2739,
372 2765,
373 2794,
374 2810,
375 2829,
376 2849,
377 2872,
378 2896,
379 2915,
380 2935,
381 2959,
382 2987,
383 3016,
384 3048,
385 3074,
386 3093,
387 3120,
388 3143,
389 3163,
390 3183,
391 3208,
392 3226,
393 3244,
394 3267,
395 3292,
396 3315,
397 3332,
398 3352,
399 3384,
400 3405,
401 3426,
402 3449,
403 3468,
404 3495,
405 3511,
406 3531,
407 3556,
408 3582,
409 3611,
410 3640,
411 3667,
412 3694,
413 3721,
414 3746,
415 3769,
416 3795,
417 3824,
418 3850,
419 3879,
420 3905,
421 3920,
422 3935,
423 3950,
424 3965,
425 3980,
426 3995,
427 4010,
428 4025,
429 4040,
430 4056,
431 4072,
432 4088,
433 4104,
434 4120,
435 4136,
436 4152,
437 4174,
438 4186
439 };
440
441#define msgidxof(code) (0 ? -1 \
442 : ((code >= 0) && (code <= 170)) ? (code - 0) \
443 : ((code >= 200) && (code <= 213)) ? (code - 29) \
444 : ((code >= 1024) && (code <= 1039)) ? (code - 839) \
445 : ((code >= 16382) && (code <= 16383)) ? (code - 16181) \
446 : 16384 - 16181)
diff --git a/pwmanager/libcrypt/error/err-codes.h b/pwmanager/libcrypt/error/err-codes.h
new file mode 100644
index 0000000..524b738
--- a/dev/null
+++ b/pwmanager/libcrypt/error/err-codes.h
@@ -0,0 +1,446 @@
1/* Output of mkstrtable.awk. DO NOT EDIT. */
2
3/* err-codes.h - List of error codes and their description.
4 Copyright (C) 2003, 2004 g10 Code GmbH
5
6 This file is part of libgpg-error.
7
8 libgpg-error is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public License
10 as published by the Free Software Foundation; either version 2.1 of
11 the License, or (at your option) any later version.
12
13 libgpg-error is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with libgpg-error; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
22
23
24/* The purpose of this complex string table is to produce
25 optimal code with a minimum of relocations. */
26
27static const char msgstr[] =
28 gettext_noop ("Success") "\0"
29 gettext_noop ("General error") "\0"
30 gettext_noop ("Unknown packet") "\0"
31 gettext_noop ("Unknown version in packet") "\0"
32 gettext_noop ("Invalid public key algorithm") "\0"
33 gettext_noop ("Invalid digest algorithm") "\0"
34 gettext_noop ("Bad public key") "\0"
35 gettext_noop ("Bad secret key") "\0"
36 gettext_noop ("Bad signature") "\0"
37 gettext_noop ("No public key") "\0"
38 gettext_noop ("Checksum error") "\0"
39 gettext_noop ("Bad passphrase") "\0"
40 gettext_noop ("Invalid cipher algorithm") "\0"
41 gettext_noop ("Keyring open") "\0"
42 gettext_noop ("Invalid packet") "\0"
43 gettext_noop ("Invalid armor") "\0"
44 gettext_noop ("No user ID") "\0"
45 gettext_noop ("No secret key") "\0"
46 gettext_noop ("Wrong secret key used") "\0"
47 gettext_noop ("Bad session key") "\0"
48 gettext_noop ("Unknown compression algorithm") "\0"
49 gettext_noop ("Number is not prime") "\0"
50 gettext_noop ("Invalid encoding method") "\0"
51 gettext_noop ("Invalid encryption scheme") "\0"
52 gettext_noop ("Invalid signature scheme") "\0"
53 gettext_noop ("Invalid attribute") "\0"
54 gettext_noop ("No value") "\0"
55 gettext_noop ("Not found") "\0"
56 gettext_noop ("Value not found") "\0"
57 gettext_noop ("Syntax error") "\0"
58 gettext_noop ("Bad MPI value") "\0"
59 gettext_noop ("Invalid passphrase") "\0"
60 gettext_noop ("Invalid signature class") "\0"
61 gettext_noop ("Resources exhausted") "\0"
62 gettext_noop ("Invalid keyring") "\0"
63 gettext_noop ("Trust DB error") "\0"
64 gettext_noop ("Bad certificate") "\0"
65 gettext_noop ("Invalid user ID") "\0"
66 gettext_noop ("Unexpected error") "\0"
67 gettext_noop ("Time conflict") "\0"
68 gettext_noop ("Keyserver error") "\0"
69 gettext_noop ("Wrong public key algorithm") "\0"
70 gettext_noop ("Tribute to D. A.") "\0"
71 gettext_noop ("Weak encryption key") "\0"
72 gettext_noop ("Invalid key length") "\0"
73 gettext_noop ("Invalid argument") "\0"
74 gettext_noop ("Syntax error in URI") "\0"
75 gettext_noop ("Invalid URI") "\0"
76 gettext_noop ("Network error") "\0"
77 gettext_noop ("Unknown host") "\0"
78 gettext_noop ("Selftest failed") "\0"
79 gettext_noop ("Data not encrypted") "\0"
80 gettext_noop ("Data not processed") "\0"
81 gettext_noop ("Unusable public key") "\0"
82 gettext_noop ("Unusable secret key") "\0"
83 gettext_noop ("Invalid value") "\0"
84 gettext_noop ("Bad certificate chain") "\0"
85 gettext_noop ("Missing certificate") "\0"
86 gettext_noop ("No data") "\0"
87 gettext_noop ("Bug") "\0"
88 gettext_noop ("Not supported") "\0"
89 gettext_noop ("Invalid operation code") "\0"
90 gettext_noop ("Timeout") "\0"
91 gettext_noop ("Internal error") "\0"
92 gettext_noop ("EOF (gcrypt)") "\0"
93 gettext_noop ("Invalid object") "\0"
94 gettext_noop ("Provided object is too short") "\0"
95 gettext_noop ("Provided object is too large") "\0"
96 gettext_noop ("Missing item in object") "\0"
97 gettext_noop ("Not implemented") "\0"
98 gettext_noop ("Conflicting use") "\0"
99 gettext_noop ("Invalid cipher mode") "\0"
100 gettext_noop ("Invalid flag") "\0"
101 gettext_noop ("Invalid handle") "\0"
102 gettext_noop ("Result truncated") "\0"
103 gettext_noop ("Incomplete line") "\0"
104 gettext_noop ("Invalid response") "\0"
105 gettext_noop ("No agent running") "\0"
106 gettext_noop ("agent error") "\0"
107 gettext_noop ("Invalid data") "\0"
108 gettext_noop ("Assuan server fault") "\0"
109 gettext_noop ("Assuan error") "\0"
110 gettext_noop ("Invalid session key") "\0"
111 gettext_noop ("Invalid S-expression") "\0"
112 gettext_noop ("Unsupported algorithm") "\0"
113 gettext_noop ("No pinentry") "\0"
114 gettext_noop ("pinentry error") "\0"
115 gettext_noop ("Bad PIN") "\0"
116 gettext_noop ("Invalid name") "\0"
117 gettext_noop ("Bad data") "\0"
118 gettext_noop ("Invalid parameter") "\0"
119 gettext_noop ("Wrong card") "\0"
120 gettext_noop ("No dirmngr") "\0"
121 gettext_noop ("dirmngr error") "\0"
122 gettext_noop ("Certificate revoked") "\0"
123 gettext_noop ("No CRL known") "\0"
124 gettext_noop ("CRL too old") "\0"
125 gettext_noop ("Line too long") "\0"
126 gettext_noop ("Not trusted") "\0"
127 gettext_noop ("Operation cancelled") "\0"
128 gettext_noop ("Bad CA certificate") "\0"
129 gettext_noop ("Certificate expired") "\0"
130 gettext_noop ("Certificate too young") "\0"
131 gettext_noop ("Unsupported certificate") "\0"
132 gettext_noop ("Unknown S-expression") "\0"
133 gettext_noop ("Unsupported protection") "\0"
134 gettext_noop ("Corrupted protection") "\0"
135 gettext_noop ("Ambiguous name") "\0"
136 gettext_noop ("Card error") "\0"
137 gettext_noop ("Card reset required") "\0"
138 gettext_noop ("Card removed") "\0"
139 gettext_noop ("Invalid card") "\0"
140 gettext_noop ("Card not present") "\0"
141 gettext_noop ("No PKCS15 application") "\0"
142 gettext_noop ("Not confirmed") "\0"
143 gettext_noop ("Configuration error") "\0"
144 gettext_noop ("No policy match") "\0"
145 gettext_noop ("Invalid index") "\0"
146 gettext_noop ("Invalid ID") "\0"
147 gettext_noop ("No SmartCard daemon") "\0"
148 gettext_noop ("SmartCard daemon error") "\0"
149 gettext_noop ("Unsupported protocol") "\0"
150 gettext_noop ("Bad PIN method") "\0"
151 gettext_noop ("Card not initialized") "\0"
152 gettext_noop ("Unsupported operation") "\0"
153 gettext_noop ("Wrong key usage") "\0"
154 gettext_noop ("Nothing found") "\0"
155 gettext_noop ("Wrong blob type") "\0"
156 gettext_noop ("Missing value") "\0"
157 gettext_noop ("Hardware problem") "\0"
158 gettext_noop ("PIN blocked") "\0"
159 gettext_noop ("Conditions of use not satisfied") "\0"
160 gettext_noop ("PINs are not synced") "\0"
161 gettext_noop ("Invalid CRL") "\0"
162 gettext_noop ("BER error") "\0"
163 gettext_noop ("Invalid BER") "\0"
164 gettext_noop ("Element not found") "\0"
165 gettext_noop ("Identifier not found") "\0"
166 gettext_noop ("Invalid tag") "\0"
167 gettext_noop ("Invalid length") "\0"
168 gettext_noop ("Invalid key info") "\0"
169 gettext_noop ("Unexpected tag") "\0"
170 gettext_noop ("Not DER encoded") "\0"
171 gettext_noop ("No CMS object") "\0"
172 gettext_noop ("Invalid CMS object") "\0"
173 gettext_noop ("Unknown CMS object") "\0"
174 gettext_noop ("Unsupported CMS object") "\0"
175 gettext_noop ("Unsupported encoding") "\0"
176 gettext_noop ("Unsupported CMS version") "\0"
177 gettext_noop ("Unknown algorithm") "\0"
178 gettext_noop ("Invalid crypto engine") "\0"
179 gettext_noop ("Public key not trusted") "\0"
180 gettext_noop ("Decryption failed") "\0"
181 gettext_noop ("Key expired") "\0"
182 gettext_noop ("Signature expired") "\0"
183 gettext_noop ("Encoding problem") "\0"
184 gettext_noop ("Invalid state") "\0"
185 gettext_noop ("Duplicated value") "\0"
186 gettext_noop ("Missing action") "\0"
187 gettext_noop ("ASN.1 module not found") "\0"
188 gettext_noop ("Invalid OID string") "\0"
189 gettext_noop ("Invalid time") "\0"
190 gettext_noop ("Invalid CRL object") "\0"
191 gettext_noop ("Unsupported CRL version") "\0"
192 gettext_noop ("Invalid certificate object") "\0"
193 gettext_noop ("Unknown name") "\0"
194 gettext_noop ("A locale function failed") "\0"
195 gettext_noop ("Not locked") "\0"
196 gettext_noop ("Protocol violation") "\0"
197 gettext_noop ("Invalid MAC") "\0"
198 gettext_noop ("Invalid request") "\0"
199 gettext_noop ("Buffer too short") "\0"
200 gettext_noop ("Invalid length specifier in S-expression") "\0"
201 gettext_noop ("String too long in S-expression") "\0"
202 gettext_noop ("Unmatched parentheses in S-expression") "\0"
203 gettext_noop ("S-expression not canonical") "\0"
204 gettext_noop ("Bad character in S-expression") "\0"
205 gettext_noop ("Bad quotation in S-expression") "\0"
206 gettext_noop ("Zero prefix in S-expression") "\0"
207 gettext_noop ("Nested display hints in S-expression") "\0"
208 gettext_noop ("Unmatched display hints") "\0"
209 gettext_noop ("Unexpected reserved punctuation in S-expression") "\0"
210 gettext_noop ("Bad hexadecimal character in S-expression") "\0"
211 gettext_noop ("Odd hexadecimal numbers in S-expression") "\0"
212 gettext_noop ("Bad octadecimal character in S-expression") "\0"
213 gettext_noop ("User defined error code 1") "\0"
214 gettext_noop ("User defined error code 2") "\0"
215 gettext_noop ("User defined error code 3") "\0"
216 gettext_noop ("User defined error code 4") "\0"
217 gettext_noop ("User defined error code 5") "\0"
218 gettext_noop ("User defined error code 6") "\0"
219 gettext_noop ("User defined error code 7") "\0"
220 gettext_noop ("User defined error code 8") "\0"
221 gettext_noop ("User defined error code 9") "\0"
222 gettext_noop ("User defined error code 10") "\0"
223 gettext_noop ("User defined error code 11") "\0"
224 gettext_noop ("User defined error code 12") "\0"
225 gettext_noop ("User defined error code 13") "\0"
226 gettext_noop ("User defined error code 14") "\0"
227 gettext_noop ("User defined error code 15") "\0"
228 gettext_noop ("User defined error code 16") "\0"
229 gettext_noop ("Unknown system error") "\0"
230 gettext_noop ("End of file") "\0"
231 gettext_noop ("Unknown error code");
232
233static const int msgidx[] =
234 {
235 0,
236 8,
237 22,
238 37,
239 63,
240 92,
241 117,
242 132,
243 147,
244 161,
245 175,
246 190,
247 205,
248 230,
249 243,
250 258,
251 272,
252 283,
253 297,
254 319,
255 335,
256 365,
257 385,
258 409,
259 435,
260 460,
261 478,
262 487,
263 497,
264 513,
265 526,
266 540,
267 559,
268 583,
269 603,
270 619,
271 634,
272 650,
273 666,
274 683,
275 697,
276 713,
277 740,
278 757,
279 777,
280 796,
281 813,
282 833,
283 845,
284 859,
285 872,
286 888,
287 907,
288 926,
289 946,
290 966,
291 980,
292 1002,
293 1022,
294 1030,
295 1034,
296 1048,
297 1071,
298 1079,
299 1094,
300 1107,
301 1122,
302 1151,
303 1180,
304 1203,
305 1219,
306 1235,
307 1255,
308 1268,
309 1283,
310 1300,
311 1316,
312 1333,
313 1350,
314 1362,
315 1375,
316 1395,
317 1408,
318 1428,
319 1449,
320 1471,
321 1483,
322 1498,
323 1506,
324 1519,
325 1528,
326 1546,
327 1557,
328 1568,
329 1582,
330 1602,
331 1615,
332 1627,
333 1641,
334 1653,
335 1673,
336 1692,
337 1712,
338 1734,
339 1758,
340 1779,
341 1802,
342 1823,
343 1838,
344 1849,
345 1869,
346 1882,
347 1895,
348 1912,
349 1934,
350 1948,
351 1968,
352 1984,
353 1998,
354 2009,
355 2029,
356 2052,
357 2073,
358 2088,
359 2109,
360 2131,
361 2147,
362 2161,
363 2177,
364 2191,
365 2208,
366 2220,
367 2252,
368 2272,
369 2284,
370 2294,
371 2306,
372 2324,
373 2345,
374 2357,
375 2372,
376 2389,
377 2404,
378 2420,
379 2434,
380 2453,
381 2472,
382 2495,
383 2516,
384 2540,
385 2558,
386 2580,
387 2603,
388 2621,
389 2633,
390 2651,
391 2668,
392 2682,
393 2699,
394 2714,
395 2737,
396 2756,
397 2769,
398 2788,
399 2812,
400 2839,
401 2852,
402 2877,
403 2888,
404 2907,
405 2919,
406 2935,
407 2952,
408 2993,
409 3025,
410 3063,
411 3090,
412 3120,
413 3150,
414 3178,
415 3215,
416 3239,
417 3287,
418 3329,
419 3369,
420 3411,
421 3437,
422 3463,
423 3489,
424 3515,
425 3541,
426 3567,
427 3593,
428 3619,
429 3645,
430 3672,
431 3699,
432 3726,
433 3753,
434 3780,
435 3807,
436 3834,
437 3855,
438 3867
439 };
440
441#define msgidxof(code) (0 ? -1 \
442 : ((code >= 0) && (code <= 170)) ? (code - 0) \
443 : ((code >= 200) && (code <= 213)) ? (code - 29) \
444 : ((code >= 1024) && (code <= 1039)) ? (code - 839) \
445 : ((code >= 16382) && (code <= 16383)) ? (code - 16181) \
446 : 16384 - 16181)
diff --git a/pwmanager/libcrypt/error/err-sources-sym.h b/pwmanager/libcrypt/error/err-sources-sym.h
new file mode 100644
index 0000000..8213f6e
--- a/dev/null
+++ b/pwmanager/libcrypt/error/err-sources-sym.h
@@ -0,0 +1,70 @@
1/* Output of mkstrtable.awk. DO NOT EDIT. */
2
3/* err-sources.h - List of error sources and their description.
4 Copyright (C) 2003, 2004 g10 Code GmbH
5
6 This file is part of libgpg-error.
7
8 libgpg-error is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public License
10 as published by the Free Software Foundation; either version 2.1 of
11 the License, or (at your option) any later version.
12
13 libgpg-error is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with libgpg-error; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
22
23
24/* The purpose of this complex string table is to produce
25 optimal code with a minimum of relocations. */
26
27static const char msgstr[] =
28 "GPG_ERR_SOURCE_UNKNOWN" "\0"
29 "GPG_ERR_SOURCE_GCRYPT" "\0"
30 "GPG_ERR_SOURCE_GPG" "\0"
31 "GPG_ERR_SOURCE_GPGSM" "\0"
32 "GPG_ERR_SOURCE_GPGAGENT" "\0"
33 "GPG_ERR_SOURCE_PINENTRY" "\0"
34 "GPG_ERR_SOURCE_SCD" "\0"
35 "GPG_ERR_SOURCE_GPGME" "\0"
36 "GPG_ERR_SOURCE_KEYBOX" "\0"
37 "GPG_ERR_SOURCE_KSBA" "\0"
38 "GPG_ERR_SOURCE_DIRMNGR" "\0"
39 "GPG_ERR_SOURCE_GSTI" "\0"
40 "GPG_ERR_SOURCE_USER_1" "\0"
41 "GPG_ERR_SOURCE_USER_2" "\0"
42 "GPG_ERR_SOURCE_USER_3" "\0"
43 "GPG_ERR_SOURCE_USER_4" "\0"
44 "GPG_ERR_SOURCE_DIM";
45
46static const int msgidx[] =
47 {
48 0,
49 23,
50 45,
51 64,
52 85,
53 109,
54 133,
55 152,
56 173,
57 195,
58 215,
59 238,
60 258,
61 280,
62 302,
63 324,
64 346
65 };
66
67#define msgidxof(code) (0 ? -1 \
68 : ((code >= 0) && (code <= 11)) ? (code - 0) \
69 : ((code >= 32) && (code <= 35)) ? (code - 20) \
70 : 36 - 20)
diff --git a/pwmanager/libcrypt/error/err-sources.h b/pwmanager/libcrypt/error/err-sources.h
new file mode 100644
index 0000000..b41eaea
--- a/dev/null
+++ b/pwmanager/libcrypt/error/err-sources.h
@@ -0,0 +1,70 @@
1/* Output of mkstrtable.awk. DO NOT EDIT. */
2
3/* err-sources.h - List of error sources and their description.
4 Copyright (C) 2003, 2004 g10 Code GmbH
5
6 This file is part of libgpg-error.
7
8 libgpg-error is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public License
10 as published by the Free Software Foundation; either version 2.1 of
11 the License, or (at your option) any later version.
12
13 libgpg-error is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with libgpg-error; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
22
23
24/* The purpose of this complex string table is to produce
25 optimal code with a minimum of relocations. */
26
27static const char msgstr[] =
28 gettext_noop ("Unspecified source") "\0"
29 gettext_noop ("gcrypt") "\0"
30 gettext_noop ("GnuPG") "\0"
31 gettext_noop ("GpgSM") "\0"
32 gettext_noop ("GPG Agent") "\0"
33 gettext_noop ("Pinentry") "\0"
34 gettext_noop ("SCD") "\0"
35 gettext_noop ("GPGME") "\0"
36 gettext_noop ("Keybox") "\0"
37 gettext_noop ("KSBA") "\0"
38 gettext_noop ("Dirmngr") "\0"
39 gettext_noop ("GSTI") "\0"
40 gettext_noop ("User defined source 1") "\0"
41 gettext_noop ("User defined source 2") "\0"
42 gettext_noop ("User defined source 3") "\0"
43 gettext_noop ("User defined source 4") "\0"
44 gettext_noop ("Unknown source");
45
46static const int msgidx[] =
47 {
48 0,
49 19,
50 26,
51 32,
52 38,
53 48,
54 57,
55 61,
56 67,
57 74,
58 79,
59 87,
60 92,
61 114,
62 136,
63 158,
64 180
65 };
66
67#define msgidxof(code) (0 ? -1 \
68 : ((code >= 0) && (code <= 11)) ? (code - 0) \
69 : ((code >= 32) && (code <= 35)) ? (code - 20) \
70 : 36 - 20)
diff --git a/pwmanager/libcrypt/error/errnos-sym.h b/pwmanager/libcrypt/error/errnos-sym.h
new file mode 100644
index 0000000..a07654e
--- a/dev/null
+++ b/pwmanager/libcrypt/error/errnos-sym.h
@@ -0,0 +1,319 @@
1/* Output of mkstrtable.awk. DO NOT EDIT. */
2
3/* errnos.h - List of system error values.
4 Copyright (C) 2003, 2004 g10 Code GmbH
5
6 This file is part of libgpg-error.
7
8 libgpg-error is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public License
10 as published by the Free Software Foundation; either version 2.1 of
11 the License, or (at your option) any later version.
12
13 libgpg-error is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with libgpg-error; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
22
23
24
25/* The purpose of this complex string table is to produce
26 optimal code with a minimum of relocations. */
27
28static const char errnos_msgstr[] =
29 "GPG_ERR_E2BIG" "\0"
30 "GPG_ERR_EACCES" "\0"
31 "GPG_ERR_EADDRINUSE" "\0"
32 "GPG_ERR_EADDRNOTAVAIL" "\0"
33 "GPG_ERR_EADV" "\0"
34 "GPG_ERR_EAFNOSUPPORT" "\0"
35 "GPG_ERR_EAGAIN" "\0"
36 "GPG_ERR_EALREADY" "\0"
37 "GPG_ERR_EAUTH" "\0"
38 "GPG_ERR_EBACKGROUND" "\0"
39 "GPG_ERR_EBADE" "\0"
40 "GPG_ERR_EBADF" "\0"
41 "GPG_ERR_EBADFD" "\0"
42 "GPG_ERR_EBADMSG" "\0"
43 "GPG_ERR_EBADR" "\0"
44 "GPG_ERR_EBADRPC" "\0"
45 "GPG_ERR_EBADRQC" "\0"
46 "GPG_ERR_EBADSLT" "\0"
47 "GPG_ERR_EBFONT" "\0"
48 "GPG_ERR_EBUSY" "\0"
49 "GPG_ERR_ECANCELED" "\0"
50 "GPG_ERR_ECHILD" "\0"
51 "GPG_ERR_ECHRNG" "\0"
52 "GPG_ERR_ECOMM" "\0"
53 "GPG_ERR_ECONNABORTED" "\0"
54 "GPG_ERR_ECONNREFUSED" "\0"
55 "GPG_ERR_ECONNRESET" "\0"
56 "GPG_ERR_ED" "\0"
57 "GPG_ERR_EDEADLK" "\0"
58 "GPG_ERR_EDEADLOCK" "\0"
59 "GPG_ERR_EDESTADDRREQ" "\0"
60 "GPG_ERR_EDIED" "\0"
61 "GPG_ERR_EDOM" "\0"
62 "GPG_ERR_EDOTDOT" "\0"
63 "GPG_ERR_EDQUOT" "\0"
64 "GPG_ERR_EEXIST" "\0"
65 "GPG_ERR_EFAULT" "\0"
66 "GPG_ERR_EFBIG" "\0"
67 "GPG_ERR_EFTYPE" "\0"
68 "GPG_ERR_EGRATUITOUS" "\0"
69 "GPG_ERR_EGREGIOUS" "\0"
70 "GPG_ERR_EHOSTDOWN" "\0"
71 "GPG_ERR_EHOSTUNREACH" "\0"
72 "GPG_ERR_EIDRM" "\0"
73 "GPG_ERR_EIEIO" "\0"
74 "GPG_ERR_EILSEQ" "\0"
75 "GPG_ERR_EINPROGRESS" "\0"
76 "GPG_ERR_EINTR" "\0"
77 "GPG_ERR_EINVAL" "\0"
78 "GPG_ERR_EIO" "\0"
79 "GPG_ERR_EISCONN" "\0"
80 "GPG_ERR_EISDIR" "\0"
81 "GPG_ERR_EISNAM" "\0"
82 "GPG_ERR_EL2HLT" "\0"
83 "GPG_ERR_EL2NSYNC" "\0"
84 "GPG_ERR_EL3HLT" "\0"
85 "GPG_ERR_EL3RST" "\0"
86 "GPG_ERR_ELIBACC" "\0"
87 "GPG_ERR_ELIBBAD" "\0"
88 "GPG_ERR_ELIBEXEC" "\0"
89 "GPG_ERR_ELIBMAX" "\0"
90 "GPG_ERR_ELIBSCN" "\0"
91 "GPG_ERR_ELNRNG" "\0"
92 "GPG_ERR_ELOOP" "\0"
93 "GPG_ERR_EMEDIUMTYPE" "\0"
94 "GPG_ERR_EMFILE" "\0"
95 "GPG_ERR_EMLINK" "\0"
96 "GPG_ERR_EMSGSIZE" "\0"
97 "GPG_ERR_EMULTIHOP" "\0"
98 "GPG_ERR_ENAMETOOLONG" "\0"
99 "GPG_ERR_ENAVAIL" "\0"
100 "GPG_ERR_ENEEDAUTH" "\0"
101 "GPG_ERR_ENETDOWN" "\0"
102 "GPG_ERR_ENETRESET" "\0"
103 "GPG_ERR_ENETUNREACH" "\0"
104 "GPG_ERR_ENFILE" "\0"
105 "GPG_ERR_ENOANO" "\0"
106 "GPG_ERR_ENOBUFS" "\0"
107 "GPG_ERR_ENOCSI" "\0"
108 "GPG_ERR_ENODATA" "\0"
109 "GPG_ERR_ENODEV" "\0"
110 "GPG_ERR_ENOENT" "\0"
111 "GPG_ERR_ENOEXEC" "\0"
112 "GPG_ERR_ENOLCK" "\0"
113 "GPG_ERR_ENOLINK" "\0"
114 "GPG_ERR_ENOMEDIUM" "\0"
115 "GPG_ERR_ENOMEM" "\0"
116 "GPG_ERR_ENOMSG" "\0"
117 "GPG_ERR_ENONET" "\0"
118 "GPG_ERR_ENOPKG" "\0"
119 "GPG_ERR_ENOPROTOOPT" "\0"
120 "GPG_ERR_ENOSPC" "\0"
121 "GPG_ERR_ENOSR" "\0"
122 "GPG_ERR_ENOSTR" "\0"
123 "GPG_ERR_ENOSYS" "\0"
124 "GPG_ERR_ENOTBLK" "\0"
125 "GPG_ERR_ENOTCONN" "\0"
126 "GPG_ERR_ENOTDIR" "\0"
127 "GPG_ERR_ENOTEMPTY" "\0"
128 "GPG_ERR_ENOTNAM" "\0"
129 "GPG_ERR_ENOTSOCK" "\0"
130 "GPG_ERR_ENOTSUP" "\0"
131 "GPG_ERR_ENOTTY" "\0"
132 "GPG_ERR_ENOTUNIQ" "\0"
133 "GPG_ERR_ENXIO" "\0"
134 "GPG_ERR_EOPNOTSUPP" "\0"
135 "GPG_ERR_EOVERFLOW" "\0"
136 "GPG_ERR_EPERM" "\0"
137 "GPG_ERR_EPFNOSUPPORT" "\0"
138 "GPG_ERR_EPIPE" "\0"
139 "GPG_ERR_EPROCLIM" "\0"
140 "GPG_ERR_EPROCUNAVAIL" "\0"
141 "GPG_ERR_EPROGMISMATCH" "\0"
142 "GPG_ERR_EPROGUNAVAIL" "\0"
143 "GPG_ERR_EPROTO" "\0"
144 "GPG_ERR_EPROTONOSUPPORT" "\0"
145 "GPG_ERR_EPROTOTYPE" "\0"
146 "GPG_ERR_ERANGE" "\0"
147 "GPG_ERR_EREMCHG" "\0"
148 "GPG_ERR_EREMOTE" "\0"
149 "GPG_ERR_EREMOTEIO" "\0"
150 "GPG_ERR_ERESTART" "\0"
151 "GPG_ERR_EROFS" "\0"
152 "GPG_ERR_ERPCMISMATCH" "\0"
153 "GPG_ERR_ESHUTDOWN" "\0"
154 "GPG_ERR_ESOCKTNOSUPPORT" "\0"
155 "GPG_ERR_ESPIPE" "\0"
156 "GPG_ERR_ESRCH" "\0"
157 "GPG_ERR_ESRMNT" "\0"
158 "GPG_ERR_ESTALE" "\0"
159 "GPG_ERR_ESTRPIPE" "\0"
160 "GPG_ERR_ETIME" "\0"
161 "GPG_ERR_ETIMEDOUT" "\0"
162 "GPG_ERR_ETOOMANYREFS" "\0"
163 "GPG_ERR_ETXTBSY" "\0"
164 "GPG_ERR_EUCLEAN" "\0"
165 "GPG_ERR_EUNATCH" "\0"
166 "GPG_ERR_EUSERS" "\0"
167 "GPG_ERR_EWOULDBLOCK" "\0"
168 "GPG_ERR_EXDEV" "\0"
169 "GPG_ERR_GPG_ERR_EXFULL";
170
171static const int errnos_msgidx[] =
172 {
173 0,
174 14,
175 29,
176 48,
177 70,
178 83,
179 104,
180 119,
181 136,
182 150,
183 170,
184 184,
185 198,
186 213,
187 229,
188 243,
189 259,
190 275,
191 291,
192 306,
193 320,
194 338,
195 353,
196 368,
197 382,
198 403,
199 424,
200 443,
201 454,
202 470,
203 488,
204 509,
205 523,
206 536,
207 552,
208 567,
209 582,
210 597,
211 611,
212 626,
213 646,
214 664,
215 682,
216 703,
217 717,
218 731,
219 746,
220 766,
221 780,
222 795,
223 807,
224 823,
225 838,
226 853,
227 868,
228 885,
229 900,
230 915,
231 931,
232 947,
233 964,
234 980,
235 996,
236 1011,
237 1025,
238 1045,
239 1060,
240 1075,
241 1092,
242 1110,
243 1131,
244 1147,
245 1165,
246 1182,
247 1200,
248 1220,
249 1235,
250 1250,
251 1266,
252 1281,
253 1297,
254 1312,
255 1327,
256 1343,
257 1358,
258 1374,
259 1392,
260 1407,
261 1422,
262 1437,
263 1452,
264 1472,
265 1487,
266 1501,
267 1516,
268 1531,
269 1547,
270 1564,
271 1580,
272 1598,
273 1614,
274 1631,
275 1647,
276 1662,
277 1679,
278 1693,
279 1712,
280 1730,
281 1744,
282 1765,
283 1779,
284 1796,
285 1817,
286 1839,
287 1860,
288 1875,
289 1899,
290 1918,
291 1933,
292 1949,
293 1965,
294 1983,
295 2000,
296 2014,
297 2035,
298 2053,
299 2077,
300 2092,
301 2106,
302 2121,
303 2136,
304 2153,
305 2167,
306 2185,
307 2206,
308 2222,
309 2238,
310 2254,
311 2269,
312 2289,
313 2303,
314
315 };
316
317#define errnos_msgidxof(code) (0 ? -1 \
318 : ((code >= 0) && (code <= 140)) ? (code - 0) \
319 : -1)
diff --git a/pwmanager/libcrypt/error/error.pro b/pwmanager/libcrypt/error/error.pro
new file mode 100644
index 0000000..8c5f667
--- a/dev/null
+++ b/pwmanager/libcrypt/error/error.pro
@@ -0,0 +1,32 @@
1######################################################################
2# Automatically generated by qmake (1.07a) Tue Oct 19 15:09:32 2004
3######################################################################
4
5TEMPLATE = lib
6CONFIG += staticlib
7INCLUDEPATH += .
8OBJECTS_DIR = obj/$(PLATFORM)
9MOC_DIR = moc/$(PLATFORM)
10DESTDIR= ../$(PLATFORM)
11 TARGET = kpmicroerror
12# Input
13HEADERS += code-from-errno.h \
14 code-to-errno.h \
15 err-codes-sym.h \
16 err-codes.h \
17 err-sources-sym.h \
18 err-sources.h \
19 errnos-sym.h \
20 gettext.h
21
22SOURCES += code-from-errno.c \
23 code-to-errno.c \
24 strerror-sym.c \
25 strerror.c \
26 strsource-sym.c \
27 strsource.c
28
29 # gpg-error.c \
30 # gpg-error.h \
31 # mkerrcodes.h
32 # mkerrcodes.c \ \ No newline at end of file
diff --git a/pwmanager/libcrypt/error/gettext.h b/pwmanager/libcrypt/error/gettext.h
new file mode 100644
index 0000000..8b262f4
--- a/dev/null
+++ b/pwmanager/libcrypt/error/gettext.h
@@ -0,0 +1,69 @@
1/* Convenience header for conditional use of GNU <libintl.h>.
2 Copyright (C) 1995-1998, 2000-2002 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published
6 by the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 USA. */
18
19#ifndef _LIBGETTEXT_H
20#define _LIBGETTEXT_H 1
21
22/* NLS can be disabled through the configure --disable-nls option. */
23#if ENABLE_NLS
24
25/* Get declarations of GNU message catalog functions. */
26# include <libintl.h>
27
28#else
29
30/* Solaris /usr/include/locale.h includes /usr/include/libintl.h, which
31 chokes if dcgettext is defined as a macro. So include it now, to make
32 later inclusions of <locale.h> a NOP. We don't include <libintl.h>
33 as well because people using "gettext.h" will not include <libintl.h>,
34 and also including <libintl.h> would fail on SunOS 4, whereas <locale.h>
35 is OK. */
36#if defined(__sun)
37# include <locale.h>
38#endif
39
40/* Disabled NLS.
41 The casts to 'const char *' serve the purpose of producing warnings
42 for invalid uses of the value returned from these functions.
43 On pre-ANSI systems without 'const', the config.h file is supposed to
44 contain "#define const". */
45# define gettext(Msgid) ((const char *) (Msgid))
46# define dgettext(Domainname, Msgid) ((const char *) (Msgid))
47# define dcgettext(Domainname, Msgid, Category) ((const char *) (Msgid))
48# define ngettext(Msgid1, Msgid2, N) \
49 ((N) == 1 ? (const char *) (Msgid1) : (const char *) (Msgid2))
50# define dngettext(Domainname, Msgid1, Msgid2, N) \
51 ((N) == 1 ? (const char *) (Msgid1) : (const char *) (Msgid2))
52# define dcngettext(Domainname, Msgid1, Msgid2, N, Category) \
53 ((N) == 1 ? (const char *) (Msgid1) : (const char *) (Msgid2))
54# define textdomain(Domainname) ((const char *) (Domainname))
55# define bindtextdomain(Domainname, Dirname) ((const char *) (Dirname))
56# define bind_textdomain_codeset(Domainname, Codeset) ((const char *) (Codeset))
57
58#endif
59
60/* A pseudo function call that serves as a marker for the automated
61 extraction of messages, but does not call gettext(). The run-time
62 translation is done at a different place in the code.
63 The argument, String, should be a literal string. Concatenated strings
64 and other string expressions won't work.
65 The macro's expansion is not parenthesized, so that it is suitable as
66 initializer for static 'char[]' or 'const char[]' variables. */
67#define gettext_noop(String) String
68
69#endif /* _LIBGETTEXT_H */
diff --git a/pwmanager/libcrypt/error/strerror-sym.c b/pwmanager/libcrypt/error/strerror-sym.c
new file mode 100644
index 0000000..3d79f40
--- a/dev/null
+++ b/pwmanager/libcrypt/error/strerror-sym.c
@@ -0,0 +1,56 @@
1/* strerror-sym.c - Describing an error code with its symbol name.
2 Copyright (C) 2003, 2004 g10 Code GmbH
3
4 This file is part of libgpg-error.
5
6 libgpg-error is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License
8 as published by the Free Software Foundation; either version 2.1 of
9 the License, or (at your option) any later version.
10
11 libgpg-error is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with libgpg-error; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21#if HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25#include <stddef.h>
26
27#include <gpg-error.h>
28
29#include "err-codes-sym.h"
30#include "errnos-sym.h"
31
32/* Return a pointer to a string containing the name of the symbol of
33 the error code in the error value ERR. Returns NULL if the error
34 code is not known. */
35const char *
36gpg_strerror_sym (gpg_error_t err)
37{
38 gpg_err_code_t code = gpg_err_code (err);
39
40 if (code & GPG_ERR_SYSTEM_ERROR)
41 {
42 int idx;
43
44 code &= ~GPG_ERR_SYSTEM_ERROR;
45 idx = errnos_msgidxof (code);
46 if (idx >= 0)
47 return errnos_msgstr + errnos_msgidx[idx];
48 else
49 return NULL;
50 }
51
52 if (msgidxof (code) == msgidxof (GPG_ERR_CODE_DIM))
53 return NULL;
54
55 return msgstr + msgidx[msgidxof (code)];
56}
diff --git a/pwmanager/libcrypt/error/strerror.c b/pwmanager/libcrypt/error/strerror.c
new file mode 100644
index 0000000..410c4ab
--- a/dev/null
+++ b/pwmanager/libcrypt/error/strerror.c
@@ -0,0 +1,169 @@
1/* strerror.c - Describing an error code.
2 Copyright (C) 2003 g10 Code GmbH
3
4 This file is part of libgpg-error.
5
6 libgpg-error is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License
8 as published by the Free Software Foundation; either version 2.1 of
9 the License, or (at your option) any later version.
10
11 libgpg-error is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with libgpg-error; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21#if HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <errno.h>
29
30#include <gpg-error.h>
31
32#include "gettext.h"
33#include "err-codes.h"
34
35/* Return a pointer to a string containing a description of the error
36 code in the error value ERR. This function is not thread-safe. */
37const char *
38gpg_strerror (gpg_error_t err)
39{
40 gpg_err_code_t code = gpg_err_code (err);
41
42 if (code & GPG_ERR_SYSTEM_ERROR)
43 {
44 int no = gpg_err_code_to_errno (code);
45 if (no)
46 return strerror (no);
47 else
48 code = GPG_ERR_UNKNOWN_ERRNO;
49 }
50 return dgettext (PACKAGE, msgstr + msgidx[msgidxof (code)]);
51}
52
53
54#ifdef HAVE_STRERROR_R
55#ifdef STRERROR_R_CHAR_P
56/* The GNU C library and probably some other systems have this weird
57 variant of strerror_r. */
58
59/* Return a dynamically allocated string in *STR describing the system
60 error NO. If this call succeeds, return 1. If this call fails due
61 to a resource shortage, set *STR to NULL and return 1. If this
62 call fails because the error number is not valid, don't set *STR
63 and return 0. */
64int
65system_strerror_r (int no, char *buf, size_t buflen)
66{
67 char *errstr;
68
69 errstr = strerror_r (no, buf, buflen);
70 if (errstr != buf)
71 {
72 size_t errstr_len = strlen (errstr) + 1;
73 size_t cpy_len = errstr_len < buflen ? errstr_len : buflen;
74 memcpy (buf, errstr, cpy_len);
75
76 return cpy_len == errstr_len ? 0 : ERANGE;
77 }
78 else
79 {
80 /* We can not tell if the buffer was large enough, but we can
81 try to make a guess. */
82 if (strlen (buf) + 1 >= buflen)
83 return ERANGE;
84
85 return 0;
86 }
87}
88
89 #else/* STRERROR_R_CHAR_P */
90/* Now the POSIX version. */
91
92int
93system_strerror_r (int no, char *buf, size_t buflen)
94{
95 return strerror_r (no, buf, buflen);
96}
97
98 #endif/* STRERROR_R_CHAR_P */
99
100 #else/* HAVE_STRERROR_H */
101/* Without strerror_r(), we can still provide a non-thread-safe
102 version. Maybe we are even lucky and the system's strerror() is
103 already thread-safe. */
104
105int
106system_strerror_r (int no, char *buf, size_t buflen)
107{
108 char *errstr = strerror (no);
109
110 if (!errstr)
111 {
112 int saved_errno = errno;
113
114 if (saved_errno != EINVAL)
115 snprintf (buf, buflen, "strerror failed: %i\n", errno);
116 return saved_errno;
117 }
118 else
119 {
120 size_t errstr_len = strlen (errstr) + 1;
121 size_t cpy_len = errstr_len < buflen ? errstr_len : buflen;
122 memcpy (buf, errstr, cpy_len);
123 return cpy_len == errstr_len ? 0 : ERANGE;
124 }
125}
126#endif
127
128
129/* Return the error string for ERR in the user-supplied buffer BUF of
130 size BUFLEN. This function is, in contrast to gpg_strerror,
131 thread-safe if a thread-safe strerror_r() function is provided by
132 the system. If the function succeeds, 0 is returned and BUF
133 contains the string describing the error. If the buffer was not
134 large enough, ERANGE is returned and BUF contains as much of the
135 beginning of the error string as fits into the buffer. */
136int
137gpg_strerror_r (gpg_error_t err, char *buf, size_t buflen)
138{
139 gpg_err_code_t code = gpg_err_code (err);
140 const char *errstr;
141 size_t errstr_len;
142 size_t cpy_len;
143
144 if (code & GPG_ERR_SYSTEM_ERROR)
145 {
146 int no = gpg_err_code_to_errno (code);
147 if (no)
148 {
149 int system_err = system_strerror_r (no, buf, buflen);
150
151 if (system_err != EINVAL)
152 {
153 if (buflen)
154 buf[buflen - 1] = '\0';
155 return system_err;
156 }
157 }
158 code = GPG_ERR_UNKNOWN_ERRNO;
159 }
160
161 errstr = dgettext (PACKAGE, msgstr + msgidx[msgidxof (code)]);
162 errstr_len = strlen (errstr) + 1;
163 cpy_len = errstr_len < buflen ? errstr_len : buflen;
164 memcpy (buf, errstr, cpy_len);
165 if (buflen)
166 buf[buflen - 1] = '\0';
167
168 return cpy_len == errstr_len ? 0 : ERANGE;
169}
diff --git a/pwmanager/libcrypt/error/strsource-sym.c b/pwmanager/libcrypt/error/strsource-sym.c
new file mode 100644
index 0000000..a191310
--- a/dev/null
+++ b/pwmanager/libcrypt/error/strsource-sym.c
@@ -0,0 +1,43 @@
1/* strsource-sym.c - Describing an error source with its symbol name.
2 Copyright (C) 2003, 2004 g10 Code GmbH
3
4 This file is part of libgpg-error.
5
6 libgpg-error is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License
8 as published by the Free Software Foundation; either version 2.1 of
9 the License, or (at your option) any later version.
10
11 libgpg-error is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with libgpg-error; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21#if HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25#include <stddef.h>
26
27#include <gpg-error.h>
28
29#include "err-sources-sym.h"
30
31/* Return a pointer to a string containing the name of the symbol of
32 the error source in the error value ERR. Returns NULL if the error
33 code is not known. */
34const char *
35gpg_strsource_sym (gpg_error_t err)
36{
37 gpg_err_source_t source = gpg_err_source (err);
38
39 if (msgidxof (source) == msgidxof (GPG_ERR_SOURCE_DIM))
40 return NULL;
41
42 return msgstr + msgidx[msgidxof (source)];
43}
diff --git a/pwmanager/libcrypt/error/strsource.c b/pwmanager/libcrypt/error/strsource.c
new file mode 100644
index 0000000..66d783c
--- a/dev/null
+++ b/pwmanager/libcrypt/error/strsource.c
@@ -0,0 +1,37 @@
1/* strsource.c - Describing an error source.
2 Copyright (C) 2003 g10 Code GmbH
3
4 This file is part of libgpg-error.
5
6 libgpg-error is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License
8 as published by the Free Software Foundation; either version 2.1 of
9 the License, or (at your option) any later version.
10
11 libgpg-error is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with libgpg-error; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21#if HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25#include <gpg-error.h>
26
27#include "gettext.h"
28#include "err-sources.h"
29
30/* Return a pointer to a string containing a description of the error
31 source in the error value ERR. */
32const char *
33gpg_strsource (gpg_error_t err)
34{
35 gpg_err_source_t source = gpg_err_source (err);
36 return dgettext (PACKAGE, msgstr + msgidx[msgidxof (source)]);
37}
diff --git a/pwmanager/libcrypt/mpi/ChangeLog b/pwmanager/libcrypt/mpi/ChangeLog
new file mode 100644
index 0000000..777fc7b
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/ChangeLog
@@ -0,0 +1,572 @@
12004-03-02 Werner Koch <wk@gnupg.org>
2
3 * hppa1.1/udiv-qrnnd.S: Alignment fix from Lamont Jones for
4 Debian. Taken from gnupg-1.3.
5
6 * longlong.h: Added PowerPC 64 bit code from GPM-4.1.2 but didn't
7 enable it yet. Some whitespace changes in HPPA to fix assembler
8 problems on HP-UX. From gnupg 1.3
9
10 * mpiutil.c (_gcry_mpi_alloc_limb_space): Better allocate
11 something even if NLIMBS is passed as 0.
12
13 * config.links: Updated system list to match gnupg 1.3.
14
152003-12-19 Werner Koch <wk@gnupg.org>
16
17 * mpi-internal.h [M_DEBUG]: Removed this unused code.
18 (struct karatsuba_ctx): Added TSPACE_NLIMBS andTP_NLIMBS.
19 * mpiutil.c (_gcry_mpi_free_limb_space): Add arg NLIMBS and wipe
20 out the memory. Changed all callers.
21 * mpih-mul.c (_gcry_mpih_mul_karatsuba_case): Keep track of
22 allocated limbs.
23 * mpi-div.c (_gcry_mpi_tdiv_qr): Keep track of allocated limbs.
24 * mpi-mul.c (gcry_mpi_mul): Ditto.
25 * mpi-pow.c (gcry_mpi_powm): Ditto.
26
27 * Manifest: Empty new file. Also add Manifest files to all CPU
28 specific directories.
29 * Makefile.am: Added.
30
31 * mpiutil.c (gcry_mpi_randomize): Use gcry_create_nonce if WEAK
32 random has been requested.
33
342003-10-31 Werner Koch <wk@gnupg.org>
35
36 * i386/mpih-rshift.S, i386/mpih-lshift.S: Use %dl and not %edx for
37 testb; this avoids an assembler warning.
38
39 * mpi-pow.c (gcry_mpi_powm): s/exp/expo/ to avoid shadowing warning.
40
412003-08-19 Marcus Brinkmann <marcus@g10code.de>
42
43 * Makefile.am (SUFFIXES): New variable.
44 (.S.o, .S.lo, .S.obj): Rewritten.
45
462003-07-30 Moritz Schulte <moritz@g10code.com>
47
48 * longlong.h (__clz_tab): Renamed to _gcry_clz_tab.
49 * mpi-bit.c (__clz_tab): Likewise.
50
512003-07-27 Werner Koch <wk@gnupg.org>
52
53 * mpicoder.c (gcry_mpi_scan): New argument BUFLEN to replace the
54 use of the intial value of NBYTES. Changed BUFFER to unsigned.
55 (gcry_mpi_print): Likewise.
56 (gcry_mpi_dump): New.
57 (_gcry_log_mpidump): Make use of gcry_mpi_dump.
58 (mpi_print): Removed.
59 (gcry_mpi_scan): Allocated mpi in secure memory when required.
60 (gcry_mpi_aprint): Changed BUFFER to unsigned char*.
61
622003-07-14 Moritz Schulte <moritz@g10code.com>
63
64 * mpicoder.c: Used gcry_err* wrappers for libgpg-error symbols.
65
662003-06-16 Moritz Schulte <moritz@g10code.com>
67
68 * mpi-add.c: Replace last occurences of old type names with newer
69 names (i.e. replace MPI with gcry_mpi_t).
70 * mpi-bit.c: Likewise.
71 * mpi-cmp.c: Likewise.
72 * mpi-div.c: Likewise.
73 * mpi-gcd.c: Likewise.
74 * mpi-internal.h: Likewise.
75 * mpi-inv.c: Likewise.
76 * mpi-mpow.c: Likewise.
77 * mpi-mul.c: Likewise.
78 * mpi-pow.c: Likewise.
79 * mpi-scan.c: Likewise.
80 * mpicoder.c: Likewise.
81 * mpiutil.c: Likewise.
82
832003-06-09 Moritz Schulte <moritz@g10code.com>
84
85 * mpicoder.c (gcry_mpi_scan): Adjust for libgpg-error.
86 (gcry_mpi_print): Likewise.
87 (gcry_mpi_aprint): Likewise.
88
892003-06-07 Moritz Schulte <moritz@g10code.com>
90
91 * longlong.h, mpi-add.c, mpi-bit.c, mpi-cmp.c, mpi-div.c,
92 mpi-gcd.c, mpi-inline.c, mpi-inline.h, mpi-internal.h, mpi-inv.c,
93 mpi-mpow.c, mpi-mul.c, mpi-pow.c, mpi-scan.c, mpicoder.c,
94 mpih-div.c, mpih-mul.c, mpiutil.c, generic/mpi-asm-defs.h,
95 generic/mpih-add1.c, generic/mpih-lshift.c, generic/mpih-mul1.c,
96 generic/mpih-mul2.c, generic/mpih-mul3.c, generic/mpih-rshift.c,
97 generic/mpih-sub1.c, generic/udiv-w-sdiv.c, i386/syntax.h,
98 m68k/syntax.h, mips3/mpi-asm-defs.h, powerpc32/syntax.h: Edited
99 all preprocessor instructions to remove whitespace before the '#'.
100 This is not required by C89, but there are some compilers out
101 there that don't like it. Replaced any occurence of the now
102 deprecated type names with the new ones.
103
1042003-05-21 Moritz Schulte <moritz@g10code.com>
105
106 * mpiutil.c (_gcry_mpi_alloc_limb_space): Only try to allocate
107 memory in case the amount of bytes to allocate is non-zero.
108
1092003-04-27 Moritz Schulte <moritz@g10code.com>
110
111 * mpiutil.c (_gcry_mpi_resize): Allocate secure memory, in case
112 bit zero of `flags' is set.
113
114 * mpi-add.c (gcry_mpi_sub): Simplify function; always use a
115 temporary variable now.
116
1172003-04-15 Werner Koch <wk@gnupg.org>
118
119 * longlong.h (umul_ppmm): Support SH3 and SH4. Thanks to
120 kazuya.s@jp.yokogawa.com.
121
1222003-04-02 Werner Koch <wk@gnupg.org>
123
124 * mpicoder.c (gcry_mpi_print): Fixed testing against possible
125 uninitialized LEN. Valgrinded by Nikos Mavroyanopoulos.
126
1272003-01-15 Werner Koch <wk@gnupg.org>
128
129 * longlong.h: Removed some spaces between backslashes and newlines.
130
1312002-09-20 Werner Koch <wk@gnupg.org>
132
133 * mpi-mul.c (gcry_mpi_mul_2exp): New. This was declared in
134 gcrypt.h but only implemented as internal function. Noted by Timo
135 but a few minutes to late for today's release.
136
137 * Makefile.am (DISTCLEANFILES): Include mpi-asm-defs.h
138
1392002-09-18 Werner Koch <wk@gnupg.org>
140
141 * Makefile.am (.S.lo): Pass -DPIC. i386, PPC and Sparc code
142 require it. It worked for me because I am using the i586 code.
143
1442002-08-23 Werner Koch <wk@gnupg.org>
145
146 * Makefile.am (.S.lo): Fixed for libtool build with --disable-shared.
147
1482002-07-24 Werner Koch <wk@gnupg.org>
149
150 * longlong.h: Replaced all K&R multiline strings by ISO ones for
151 the sake of modern compilers. Suggested by Marco Parrone.
152
1532002-06-24 Werner Koch <wk@gnupg.org>
154
155 * mpiutil.c (gcry_mpi_swap): New.
156
157 * mpi-div.c (gcry_mpi_div): New.
158 (gcry_mpi_mod): New.
159 * mpi-inv.c (gcry_mpi_invm): New.
160
161 * mpicoder.c (do_get_buffer): Make sure that we allocate at least
162 one byte.
163
1642002-06-12 Werner Koch <wk@gnupg.org>
165
166 * hppa1.1/udiv-qrnnd.S: Changes for PIC by Randolph Chung.
167
1682002-05-15 Werner Koch <wk@gnupg.org>
169
170 * config.links: Chnage the way the mpi modules are determined.
171 * Makefile.am: Revamped to better handle modules
172
1732002-05-14 Werner Koch <wk@gnupg.org>
174
175 Changed license of all files to the LGPL.
176
1772002-04-18 Werner Koch <wk@gnupg.org>
178
179 * mpicoder.c (gcry_mpi_scan): Don't use normalize on a NULL MPI.
180
1812002-03-20 Werner Koch <wk@gnupg.org>
182
183 * mpicoder.c (mpi_read_from_buffer): Bail out on a zero length
184 buffer because we can't eventually do an malloc of this size.
185 Reported by Timo.
186
1872002-01-14 Werner Koch <wk@gnupg.org>
188
189 * mpi-inv.c (_gcry_mpi_invm): Typo fixes, noted by Carlo Perassi.
190
1912001-11-01 Werner Koch <wk@gnupg.org>
192
193 * mpicoder.c (gcry_mpi_scan): Allow to pass a nbytes as NULL or
194 with value 0 for format GCRY_FMT_SSH, so that the length is not
195 used for any checks, only the length stored in the bufer is used.
196 This is a nice format becuase we can just pass a buffer around and
197 don't need to care about its length.
198
1992001-08-03 Werner Koch <wk@gnupg.org>
200
201 * config.links: Changed the way the list of files to be
202 symlinked is returned.
203
2042001-05-31 Werner Koch <wk@gnupg.org>
205
206 * mpih-cmp.c: Removed and moved mpihelp_cmp to ..
207 * mpi-inline.h: .. here.
208
209 Major function renaming. All global functions are now prefixed
210 with _gcry_ or gcry_. Renamed also all mpihelp_ to just mpih_ so
211 that functions names are not getting to long an unreadable and for
212 better matching with the filenames.
213
2142001-05-28 Werner Koch <wk@gnupg.org>
215
216 * mpicoder.c (mpi_fromstr): Made static and assume that all input
217 is in hexformat.
218
219 Updated all CPU specific code with the one from GnuPG-1.0.5. This
220 is just a change of text formatting and the use of .label
221 instead of labels for hppa and pa7100.
222
223 * longlong.h: Fixes for ARM by Phil Blundell.
224
2252001-03-29 Werner Koch <wk@gnupg.org>
226
227 * mpi-mul.c (mpi_mul): Make sure that secret temporary results are
228 not stored in w. Suggested by Florian Weimer.
229
230 * config.links: Use i386 code for i386. According to tests by
231 Kevin Ryde the i586 code runs slow on i386 CPUs. Ditto for i786.
232
2332001-01-11 Werner Koch <wk@gnupg.org>
234
235 * Makefile.am: Removed mpi.h.
236
2372000-12-19 Werner Koch <wk@gnupg.org>
238
239 * mpi-internal.h: Put limb_t definition in an ifdef.
240
241 Major change:
242 Removed all GnuPG stuff and renamed this piece of software
243 to gcrypt.
244
2452000-11-14 Werner Koch <wk@gnupg.org>
246
247 * mpi-internal.h, mpi.h: Changed the way they are called and
248 introduced DID_MPI_LIMP_TYPEDEF hack. Very ugly, should all be
249 revamped.
250
251 * Makefile.am (OMIT_DEPENDENCIES): Hack to work around dependency
252 problems.
253
2542000-10-11 Werner Koch <wk@gnupg.org>
255
256 * generic/mpi-asm-defs.h: New.
257 * mips3/mpi-asm-defs.h: New.
258 * config.links: Create a link to one of the above files.
259
260Fri Jul 28 18:19:11 CEST 2000 Werner Koch <wk@openit.de>
261
262 * mpicoder.c (gcry_mpi_scan): Normalize the returned MPI.
263
264Tue Jul 25 17:44:15 CEST 2000 Werner Koch <wk@openit.de>
265
266 * config.links: Support for powerpc--netbsd by Gabriel Rosenkoetter.
267
268Mon Jul 17 16:35:47 CEST 2000 Werner Koch <wk@>
269
270 * power/: Add all files from GMP for this CPU. Converted comments to
271 CPP comments because some ASes complain about ' in comments.
272
273 * config.links: Support for BSDI 4.x; by Wayne Chapeskie. Add support
274 for FreeBSD 5 and made the case stmt looking nicer; by Jun Kuriyama.
275 Add support for NetBSD.
276 (sparc8): Made the search path the same as sparc9
277 (sparc64-unknown-linux-gnu): use udiv module; by Adam Mitchell.
278
279 * Makefile.am: c/SFLAGS/ASFLAGS/. This has only been used by the
280 powerpc and actually never passed the -Wa,foo to the cc.
281
282 * mpih-div.c (mpihelp_divrem): The MPN_COPY_DECR copied one element
283 too many. This is a gmp2.0.2p9.txt patch.
284
285 * longlong.h (umul_ppmm): Fixes for ARM-4. By Sean MacLennan.
286
287 * mpi-internal.h (karatsuba_ctx): New.
288 * mpih-mul.c (mpihelp_release_karatsuba_ctx): New.
289 (mpihelp_mul_karatsuba_case): New.
290 (mpihelp_mul): Splitted to make use of the new functions.
291 * mpi-pow.c (mpi_powm): Make use of the new splitted function to avoid
292 multiple allocation of temporary memory during the karatsuba operations.
293 * mpi_mpow.c: Removed the unused Barrett code.
294
2952000-03-21 16:17:30 Werner Koch (wk@habibti.openit.de)
296
297 * config.links: Add support for FreeBSD 5.
298
299Mon Jan 24 22:24:38 CET 2000 Werner Koch <wk@gnupg.de>
300
301 * mpicoder.c (gcry_mpi_aprint): Now really returns the length.
302
303Mon Jan 24 13:04:28 CET 2000 Werner Koch <wk@gnupg.de>
304
305 * mpiutil.c: Removed all memory debugging code.
306
307 * mpicoder.c (gcry_mpi_aprint): New.
308
309 * Replaced all m_ memory functions by g10_ ones.
310
311Fri Dec 31 14:06:56 CET 1999 Werner Koch <wk@gnupg.de>
312
313 * mpi-bit.c (gcry_mpi_get_nbits): New.
314
315 * mpiutil.c (mpi_set_secure): made static.
316 (gcry_mpi_get_flag): New.
317 (gcry_mpi_set_flag): New.
318 (gcry_mpi_clear_flag): New.
319 (mpi_set_opaque): renamed to gcry_mpi_set_opaque.
320 (mpi_get_opaque): renamed to gcry_mpi_get_opaque.
321
322Fri Dec 31 12:48:31 CET 1999 Werner Koch <wk@gnupg.de>
323
324 * mpicoder.c (mpi_read_from_buffer): Made static.
325 (gcry_mpi_print): A buffer of NULL is now allowed to get the required
326 length back.
327 (mpi_get_keyid): Removed.
328 (mpi_print): Made static - should be removed.
329
330Wed Dec 8 21:58:32 CET 1999 Werner Koch <wk@gnupg.de>
331
332 * Makefile.am (INCLUDES): Add ../gcrypt.
333
334 * g10m.c : Removed.
335
336 * mpicoder.c (mpi_write): Removed.
337 (mpi_read): Removed.
338 (gcry_mpi_scan): New. Taken from ../gcrypt/mpiapi.c.
339 (gcry_mpi_print): Ditto.
340
341 * mpi-pow.c (mpi_powm): Renamed to ...
342 (gcry_mpi_powm): ... this.
343
344 * mpiutil.c (gcry_mpi_new): New as a wrapper around the old function.
345 Taken from ../gcrypt/mpiapi.c.
346 (gcry_mpi_snew): Ditto.
347 (gcry_mpi_release): Ditto.
348 (gcry_mpi_copy): Ditto.
349 (gcry_mpi_set): Ditto.
350 (gcry_mpi_set_ui): Ditto.
351 (gcry_mpi_cmp): Ditto.
352 (gcry_mpi_cmp_ui): Ditto.
353 (gcry_mpi_randomize): Ditto.
354
355 * mpicoder.c (mpi_print): Removed the nbit_info kludge.
356 * mpi-bits.c (mpi_get_nbits): Replaced the is_protected stuff by
357 checking whether it is an opaque mpi and then returns it's length
358 in bits.
359 * mpiutil.c (mpi_set_opaque): Changed the interface to take a number
360 of bits for the length. Adjusted all users.
361 (mpi_get_opaque): Ditto.
362
363Fri Nov 19 17:15:20 CET 1999 Werner Koch <wk@gnupg.de>
364
365 * mpicoder.c (g10_log_mpidump): Add a temporary workaround
366
367 * mpih-mul.c (mpihelp_mul_n): s/m_is_ecure/g10_is_secure/
368
369 * mpiutil.c (mpi_alloc): Remved the debug mode because it has turned
370 out, that this feature was not very useful in the past. Use the
371 new alloc functions.
372 (mpi_alloc_secure): Ditto.
373 (mpi_alloc_limb_space): Ditto.
374 (mpi_free_limb_space): Ditto.
375 (mpi_resize): Ditto.
376 (mpi_free): Ditto.
377 (mpi_set_secure): Removed the debug stuff.
378 (mpi_set_opaque): Ditto.
379 (mpi_copy): Ditto.
380 (mpi_alloc_set_ui): Ditto.
381 (mpi_m_check): Use g10_ wrapper.
382
383Mon Aug 30 20:38:33 CEST 1999 Werner Koch <wk@isil.d.shuttle.de>
384
385
386 * config.links: Add case label for DJGPP
387
388Wed Jul 14 19:42:08 CEST 1999 Werner Koch <wk@isil.d.shuttle.de>
389
390
391 * Makefile.am: Use .s files as temporaries, disabled other .S rules.
392
393Wed Jul 7 13:08:40 CEST 1999 Werner Koch <wk@isil.d.shuttle.de>
394
395
396 * mpicoder.c (g10_log_mpidump): New.
397
398 * Makefile.am: Support for libtool.
399
400Fri Jul 2 11:45:54 CEST 1999 Werner Koch <wk@isil.d.shuttle.de>
401
402
403 * mpi-bit.c (mpi_lshift_limbs,mpi_rshift_limbs): New.
404 * mpi-mpow.c (barrett_mulm): New but diabled.
405
406Tue Jun 1 16:01:46 CEST 1999 Werner Koch <wk@isil.d.shuttle.de>
407
408 * config.links (i[56]86*-*-freebsdelf*): New.
409
410Sun May 23 14:20:22 CEST 1999 Werner Koch <wk@isil.d.shuttle.de>
411
412 * config.links (sysdep.h): Not any more conditionally created.
413
414Tue May 4 15:47:53 CEST 1999 Werner Koch <wk@isil.d.shuttle.de>
415
416 * mpiutil.c (mpi_alloc_like): New.
417
418Mon Apr 26 17:48:15 CEST 1999 Werner Koch <wk@isil.d.shuttle.de>
419
420 * mpih-add.c, mpih-sub.c: Removed
421 * mpi-inline.c: New.
422 * mpi-inline.h: Make it usable by mpi-inline.c.
423
424Sun Apr 18 10:11:28 CEST 1999 Werner Koch <wk@isil.d.shuttle.de>
425
426 * mpih-mul.c (mpihelp_mul_n): Fixed use of memory region.
427 (mpihelp_mul): Ditto.
428
429Wed Apr 7 20:51:39 CEST 1999 Werner Koch <wk@isil.d.shuttle.de>
430
431 * Makefile.am: Explicit rules to invoke cpp on *.S
432
433Mon Mar 8 20:47:17 CET 1999 Werner Koch <wk@isil.d.shuttle.de>
434
435 * config.links: Take advantage of the with_symbol_underscore macro.
436 Add support for freebsd 4.
437
438Wed Feb 24 11:07:27 CET 1999 Werner Koch <wk@isil.d.shuttle.de>
439
440 * mips3/mpih-sub1.S: Removed left over junk in last line. (Should I
441 blame me or my editor?).
442
443Sat Feb 13 12:04:43 CET 1999 Werner Koch <wk@isil.d.shuttle.de>
444
445 * Makefile.am: Removed the +=. Add MPI_OPT_FLAGS.
446
447Sat Jan 9 16:02:23 CET 1999 Werner Koch <wk@isil.d.shuttle.de>
448
449 * mpi-cmp.c (mpi_cmp_ui): Normalized the arg.
450
451Thu Jan 7 18:00:58 CET 1999 Werner Koch <wk@isil.d.shuttle.de>
452
453 * mpi-bit.c (mpi_normalize): New.
454 (mpi_get_nbits): Normalize the MPI.
455 * mpi-bit.c (mpi_cmp): Normalize the MPI before the compare.
456
457
458Tue Dec 8 13:15:16 CET 1998 Werner Koch <wk@isil.d.shuttle.de>
459
460 * config.links: Moved the case for powerpc*linux
461 * powerpcp32/*.S: Removed some underscores.
462
463Thu Nov 26 07:27:52 1998 Werner Koch <werner.koch@guug.de>
464
465 * config.links: Support for ppc with ELF
466 * powerpc32/syntax.h: New.
467 * powerpc32/*.S: Applied ELF patches (glibc patches)
468
469Tue Nov 10 19:31:37 1998 Werner Koch (wk@isil.d.shuttle.de)
470
471 * power*/ : Started with stuff for PPC
472 * config.links: Some stuff for PPC.
473 * generic/udiv-w-sdiv.c: New but disabled.
474
475Tue Oct 27 12:37:46 1998 Werner Koch (wk@isil.d.shuttle.de)
476
477 * config.links (freebsd): Fixes for FreeBSD 3.0
478
479Wed Oct 14 09:59:30 1998 Werner Koch (wk@isil.d.shuttle.de)
480
481 * config.links (freebsd): ELF patches from Jun Kuriyama.
482
483Thu Oct 8 13:28:17 1998 Werner Koch (wk@isil.d.shuttle.de)
484
485 * mpi-mpow.c (mpi_mulpowm): Fixed mem leak (m_free/mpi_free).
486
487Thu Sep 17 18:08:50 1998 Werner Koch (wk@(none))
488
489 * hppa1.1/udiv-qrnnd.S: Fix from Steffen Zahn for HPUX 10.20
490
491 Thu Aug 6 16:39:28 1998 Werner Koch,mobil,,,(wk@tobold)
492
493 * mpi-bit.c (mpi_set_bytes): Removed.
494
495Wed Aug 5 15:11:12 1998 Werner Koch (wk@(none))
496
497 * mpicoder.c (mpi_read_from_buffer): New.
498
499 * mpiutil.c (mpi_set_opaque): New.
500 (mpi_get_opaque): New.
501 (mpi_copy): Changed to support opauqe flag
502 (mpi_free): Ditto.
503
504Sat Jul 4 10:11:11 1998 Werner Koch (wk@isil.d.shuttle.de)
505
506 * mpiutil.c (mpi_clear): Reset flags.
507 (mpi_set): Ditto.
508 (mpi_alloc_secure): Set flag to 1 and not ored the 1 in, tsss..
509
510Fri Jun 26 11:19:06 1998 Werner Koch (wk@isil.d.shuttle.de)
511
512 * mpiutil.c (mpi_alloc): set nbits to 0.
513 (mpi_alloc_secure): Ditto.
514 (mpi_clear): Ditto.
515
516Thu Jun 25 11:50:01 1998 Werner Koch (wk@isil.d.shuttle.de)
517
518 * mips3/*.S: New
519
520Mon May 18 13:47:06 1998 Werner Koch (wk@isil.d.shuttle.de)
521
522 * config.links: split mpih-shift into mpih-[lr]shift and
523 changed all implementations.
524 * mpi/alpha: add some new assembler stuff.
525
526Wed May 13 11:04:29 1998 Werner Koch (wk@isil.d.shuttle.de)
527
528 * config.links: Add support for MIPS
529
530Thu Apr 9 11:31:36 1998 Werner Koch (wk@isil.d.shuttle.de)
531
532 * mpicoder.c (mpi_get_secure_buffer): New.
533
534Wed Apr 8 09:44:33 1998 Werner Koch (wk@isil.d.shuttle.de)
535
536 * config.links: Applied small fix from Ulf Möller.
537
538Mon Apr 6 12:38:52 1998 Werner Koch (wk@isil.d.shuttle.de)
539
540 * mpicoder.c (mpi_get_buffer): Removed returned leading zeroes
541 and changed all callers.
542
543Tue Mar 10 13:40:34 1998 Werner Koch (wk@isil.d.shuttle.de)
544
545 * mpi-bit.c (mpi_clear_highbit): New.
546
547Mon Mar 2 19:29:00 1998 Werner Koch (wk@isil.d.shuttle.de)
548
549 * Makefile.am (DISTCLEANFILES): New
550
551Thu Feb 26 06:48:54 1998 Werner Koch (wk@isil.d.shuttle.de)
552
553 * config.links (X86_BROKEN_ALIGN): Added for some systems.
554
555Mon Feb 23 12:21:40 1998 Werner Koch (wk@isil.d.shuttle.de)
556
557 * mpi/m68k/mpih-shift.S (Lspecial): Changed duplicate symbol.
558
559Mon Feb 16 13:00:27 1998 Werner Koch (wk@isil.d.shuttle.de)
560
561 * config.links : Add detection of m68k cpus
562
563
564 Copyright 1998,1999,2000,2001,2002,2003 Free Software Foundation, Inc.
565
566 This file is free software; as a special exception the author gives
567 unlimited permission to copy and/or distribute it, with or without
568 modifications, as long as this notice is preserved.
569
570 This file is distributed in the hope that it will be useful, but
571 WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
572 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
diff --git a/pwmanager/libcrypt/mpi/Manifest b/pwmanager/libcrypt/mpi/Manifest
new file mode 100644
index 0000000..3b0d673
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/Manifest
@@ -0,0 +1,41 @@
1# Manifest - checksums of the mpi directory
2# Copyright 2003 Free Software Foundation, Inc.
3#
4# This file is part of Libgcrypt.
5#
6# Libgcrypt is free software; you can redistribute it and/or modify
7# it under the terms of the GNU Lesser general Public License as
8# published by the Free Software Foundation; either version 2.1 of
9# the License, or (at your option) any later version.
10#
11# Libgcrypt is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public
17# License along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19
20Makefile.am
21config.links
22longlong.h
23mpi-add.c
24mpi-bit.c
25mpi-cmp.c
26mpi-div.c
27mpi-gcd.c
28mpi-inline.c
29mpi-inline.h
30mpi-internal.h
31mpi-inv.c
32mpi-mpow.c
33mpi-mul.c
34mpi-pow.c
35mpi-scan.c
36mpicoder.c
37mpih-div.c
38mpih-mul.c
39mpiutil.c
40$names$ iQCVAwUAP+LmfDEAnp832S/7AQKZJQQAkR/gQITUM+6Ygy9WAOAO17btyKAlCtGTXp5XSZ+J3X0o/rYneRdSCW89IJvwFRJjAOcFJd52MXs6ZVFF/RQBC8MvJzuQChbEzvihK8o2VgK34YWjU+6XH9sFgRMIgzkHs/51ZZxeQUOPy1XF7TyKB0WE7YBUVisFiRaqB1qGIOs==Z3qB
41
diff --git a/pwmanager/libcrypt/mpi/asm-syntax.h b/pwmanager/libcrypt/mpi/asm-syntax.h
new file mode 100644
index 0000000..ca94a8e
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/asm-syntax.h
@@ -0,0 +1,71 @@
1/* created by config.links - do not edit */
2/* Target: i686-pc-linux-gnu */
3#define ELF_SYNTAX
4/* syntax.h -- Definitions for x86 syntax variations.
5 *
6 * Copyright (C) 1992, 1994, 1995, 1998,
7 * 2001, 2002 Free Software Foundation, Inc.
8 *
9 * This file is part of Libgcrypt.
10 *
11 * Libgcrypt is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as
13 * published by the Free Software Foundation; either version 2.1 of
14 * the License, or (at your option) any later version.
15 *
16 * Libgcrypt is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
24 *
25 * Note: This code is heavily based on the GNU MP Library.
26 * Actually it's the same code with only minor changes in the
27 * way the data is stored; this is to support the abstraction
28 * of an optional secure memory allocation which may be used
29 * to avoid revealing of sensitive data due to paging etc.
30 */
31
32#undef ALIGN
33
34#if defined (BSD_SYNTAX) || defined (ELF_SYNTAX)
35#define R(r) %r
36#define MEM(base)(base)
37#define MEM_DISP(base,displacement)displacement(R(base))
38#define MEM_INDEX(base,index,size)(R(base),R(index),size)
39#ifdef __STDC__
40#define INSN1(mnemonic,size_suffix,dst)mnemonic##size_suffix dst
41#define INSN2(mnemonic,size_suffix,dst,src)mnemonic##size_suffix src,dst
42#else
43#define INSN1(mnemonic,size_suffix,dst)mnemonic/**/size_suffix dst
44#define INSN2(mnemonic,size_suffix,dst,src)mnemonic/**/size_suffix src,dst
45#endif
46#define TEXT .text
47#if defined (BSD_SYNTAX)
48#define ALIGN(log) .align log
49#endif
50#if defined (ELF_SYNTAX)
51#define ALIGN(log) .align 1<<(log)
52#endif
53#define GLOBL .globl
54#endif
55
56#ifdef INTEL_SYNTAX
57#define R(r) r
58#define MEM(base)[base]
59#define MEM_DISP(base,displacement)[base+(displacement)]
60#define MEM_INDEX(base,index,size)[base+index*size]
61#define INSN1(mnemonic,size_suffix,dst)mnemonic dst
62#define INSN2(mnemonic,size_suffix,dst,src)mnemonic dst,src
63#define TEXT .text
64#define ALIGN(log) .align log
65#define GLOBL .globl
66#endif
67
68#ifdef X86_BROKEN_ALIGN
69#undef ALIGN
70#define ALIGN(log) .align log,0x90
71#endif
diff --git a/pwmanager/libcrypt/mpi/generic/Manifest b/pwmanager/libcrypt/mpi/generic/Manifest
new file mode 100644
index 0000000..c429fde
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/generic/Manifest
@@ -0,0 +1,29 @@
1# Manifest - checksums
2# Copyright 2003 Free Software Foundation, Inc.
3#
4# This file is part of Libgcrypt.
5#
6# Libgcrypt is free software; you can redistribute it and/or modify
7# it under the terms of the GNU Lesser general Public License as
8# published by the Free Software Foundation; either version 2.1 of
9# the License, or (at your option) any later version.
10#
11# Libgcrypt is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public
17# License along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19
20mpih-add1.c iQCVAwUAP+Lj2DEAnp832S/7AQKn/AQAwQLWggl6zNQ5EZ+lE+jKV8W3FsogW3/6tp9T5rrSR5JnlWyoHQ9/Pu4knOcLjS6nIfVOiAEifu3nuIysQr9jDSSSJA2LylSUBSXKLKDamPsOCwXOLxiZODslJT3CCGAUtLvXJrWDbTZQrkEuwnLnjQFDzuA7iY9JLrG9kAoXD6Q==WoWm
21mpih-mul1.c iQCVAwUAP+LkCTEAnp832S/7AQKFVQP+MhBNjcY73JtnsHZfnaVZq3TiKwN151cWV51nDc1RnTaMhSIFeuNlj3vNML2W0Gn8n+GnyiWE2XXdQEaik6BL02eekUn9aq7I/rdpnTHuOjQPK1uwjuNl8RuJ9YrERBAxq4oB71f+iwMab8dsMSUlVC+NdeAocRqLLgnR/efkdLc==2Tkb
22mpih-mul2.c iQCVAwUAP+LkMjEAnp832S/7AQLPeAQAqmRzxFe/mDqTdZr/pTXT8RVyB1vKB0Ei2THV05BxmI4OPv39uysfFpLMt/INsX7AGqdOlj4jOZ/qNaFXR1ceMrlSXvo8u/epk6rCXFp82kM7Qs983LjoP//PrMCkYkXwblaVrgUGiBUCbuPMliWTK6qKkxxXtEfqZ7nVbEWdBx8==Kwhl
23mpih-mul3.c iQCVAwUAP+LkVDEAnp832S/7AQL91gP/Qd5iZWxRiN5DdEIVHAedoNvl23NPrT2UUdXvnSK49DpplTxkLiMBj0WqCayG/YIET2NpMRCeLvAZNcSt6lOm0bSZDYo1Hv/N+UoqD3V1McjY16REBv/nnPaMWMZcx7rl5yKTVZiX2PgV6oQOL7Yfrt5ZIOlrHBRs9S2/zcCaVz0==9BQe
24mpih-lshift.c iQCVAwUAP+LlATEAnp832S/7AQIACAQAhMrpx0SRXE/LN1NkjMO9n74nMrvmzYJyru0gw2O4BYrUPvD/LWGju2FZaggKV0IBjmi0cDoCrNeK9EGjKOO1lfgODbX2IZ1LUhr9jDuMj0QRqj6T9YkAFYTNUk4GfpwIf7T6Ybo7c78Jx93PidCJt7d39eMMEalooC7LZ4IU3NM==nZ4k
25mpih-rshift.c iQCVAwUAP+LlIjEAnp832S/7AQKiuAP/eYC2ZScd+taBx/kNzRvGjA0eAXvORMkMLV6Ot+OXVzVUi04eoP2yXdxSNFKwUj12p8GWXkdoMG3aOGBKg2a7bY5Q5RUho3hUWb9UsVYVUfXLf7IOTt/3a6MLh2CmV5dFPWJmSlbCyQRcn6n/fLDeJ3A2bWTS/BhqGfpOXUIU1ws==jCf8
26mpih-sub1.c iQCVAwUAP+LlZzEAnp832S/7AQIEPgP/dLHTDRbPrYJhsLp9SjGstU1M8/IC5XytcDtO3NQeu4mx6vaXjpujtsTvKIbX4QL5IahNntVVKv1xFLEm2yFg7L2ns0uD/mfwGgOhCG1j2o/SaTAWP5KxP7ae5UDcZl2w6NWvEuMj9t32zmziAZjP8W73A37FUspeRDYiL9sQzkI==QQzk
27udiv-w-sdiv.c iQCVAwUAP+Lk0TEAnp832S/7AQICXAQAsxe1SQD4+xZaZTqBC0V9Cyuo0mrdccnRFzthOtm0ARwKFXU2cuLW/ZBOkmeWOVmOFhBp22/I8dEGYnMA3gcfmOMCpNu9i9zk/XHfptdunA1MnOe3GsoWgfHL0rhpAyPhp/X043ICB41NElnnuxADuQQlD4Z1fca5ygYxMr2crJg==EI/6
28mpi-asm-defs.h iQCVAwUAP+LkgDEAnp832S/7AQK0FgQAxJZ7xvXhoZa33GWe23LRb3asrno/loZSyAIXrntqtVH8M3pEsCY0OyW4ry4hX2RnxpuhRCM/PdRNLG3xXyMSVIhkHU8WVRLqzF2LLjEkyU3cAmHnnTQ9aO/XpUWtJGTZ8q2bv7ZsAEi4aPl0p6KhPXcPgM9vQ2XcyOPn3Dl0d6Q==xpjI
29$names$ iQCVAwUAP+LmNDEAnp832S/7AQJa+gP+KQNJpbNOgc+s2UX+Ya2gDaOFcAROImIllhg3ej8EaBF8xxdHmWT1zaKwTwi3moEEleykMR104YAGWyQeMbFYiuPPBW+ohrT6KxRBVJpIA9auOOqqJMyglZyoR3Hv7gduVYUW1h/DebnqiKXKEfzQDFqYuT0ayuteoOR4B5NICbE==nLSh
diff --git a/pwmanager/libcrypt/mpi/generic/distfiles b/pwmanager/libcrypt/mpi/generic/distfiles
new file mode 100644
index 0000000..9810eef
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/generic/distfiles
@@ -0,0 +1,11 @@
1Manifest
2mpih-add1.c
3mpih-mul1.c
4mpih-mul2.c
5mpih-mul3.c
6mpih-lshift.c
7mpih-rshift.c
8mpih-sub1.c
9udiv-w-sdiv.c
10mpi-asm-defs.h
11
diff --git a/pwmanager/libcrypt/mpi/generic/mpi-asm-defs.h b/pwmanager/libcrypt/mpi/generic/mpi-asm-defs.h
new file mode 100644
index 0000000..13424e2
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/generic/mpi-asm-defs.h
@@ -0,0 +1,10 @@
1/* This file defines some basic constants for the MPI machinery. We
2 * need to define the types on a per-CPU basis, so it is done with
3 * this file here. */
4#define BYTES_PER_MPI_LIMB (SIZEOF_UNSIGNED_LONG)
5
6
7
8
9
10
diff --git a/pwmanager/libcrypt/mpi/generic/mpih-add1.c b/pwmanager/libcrypt/mpi/generic/mpih-add1.c
new file mode 100644
index 0000000..4a84df6
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/generic/mpih-add1.c
@@ -0,0 +1,65 @@
1/* mpihelp-add_1.c - MPI helper functions
2 * Copyright (C) 1994, 1996, 1997, 1998,
3 * 2000, 2002 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 *
21 * Note: This code is heavily based on the GNU MP Library.
22 * Actually it's the same code with only minor changes in the
23 * way the data is stored; this is to support the abstraction
24 * of an optional secure memory allocation which may be used
25 * to avoid revealing of sensitive data due to paging etc.
26 */
27
28#include <config.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include "mpi-internal.h"
32#include "longlong.h"
33
34mpi_limb_t
35_gcry_mpih_add_n (mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
36 mpi_ptr_t s2_ptr, mpi_size_t size)
37{
38 mpi_limb_t x, y, cy;
39 mpi_size_t j;
40
41 /* The loop counter and index J goes from -SIZE to -1. This way
42 the loop becomes faster. */
43 j = -size;
44
45 /* Offset the base pointers to compensate for the negative indices. */
46 s1_ptr -= j;
47 s2_ptr -= j;
48 res_ptr -= j;
49
50 cy = 0;
51 do
52 {
53 y = s2_ptr[j];
54 x = s1_ptr[j];
55 y += cy; /* add previous carry to one addend */
56 cy = y < cy; /* get out carry from that addition */
57 y += x; /* add other addend */
58 cy += y < x; /* get out carry from that add, combine */
59 res_ptr[j] = y;
60 }
61 while ( ++j );
62
63 return cy;
64}
65
diff --git a/pwmanager/libcrypt/mpi/generic/mpih-lshift.c b/pwmanager/libcrypt/mpi/generic/mpih-lshift.c
new file mode 100644
index 0000000..f48c12c
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/generic/mpih-lshift.c
@@ -0,0 +1,68 @@
1/* mpi-lshift.c - MPI helper functions
2 * Copyright (C) 1994, 1996, 1998, 2001, 2002 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 *
20 * Note: This code is heavily based on the GNU MP Library.
21 * Actually it's the same code with only minor changes in the
22 * way the data is stored; this is to support the abstraction
23 * of an optional secure memory allocation which may be used
24 * to avoid revealing of sensitive data due to paging etc.
25 */
26
27#include <config.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include "mpi-internal.h"
31
32/* Shift U (pointed to by UP and USIZE digits long) CNT bits to the left
33 * and store the USIZE least significant digits of the result at WP.
34 * Return the bits shifted out from the most significant digit.
35 *
36 * Argument constraints:
37 * 1. 0 < CNT < BITS_PER_MP_LIMB
38 * 2. If the result is to be written over the input, WP must be >= UP.
39 */
40
41mpi_limb_t
42_gcry_mpih_lshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
43 unsigned int cnt)
44{
45 mpi_limb_t high_limb, low_limb;
46 unsigned sh_1, sh_2;
47 mpi_size_t i;
48 mpi_limb_t retval;
49
50 sh_1 = cnt;
51 wp += 1;
52 sh_2 = BITS_PER_MPI_LIMB - sh_1;
53 i = usize - 1;
54 low_limb = up[i];
55 retval = low_limb >> sh_2;
56 high_limb = low_limb;
57 while ( --i >= 0 )
58 {
59 low_limb = up[i];
60 wp[i] = (high_limb << sh_1) | (low_limb >> sh_2);
61 high_limb = low_limb;
62 }
63 wp[i] = high_limb << sh_1;
64
65 return retval;
66}
67
68
diff --git a/pwmanager/libcrypt/mpi/generic/mpih-mul1.c b/pwmanager/libcrypt/mpi/generic/mpih-mul1.c
new file mode 100644
index 0000000..0e8197d
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/generic/mpih-mul1.c
@@ -0,0 +1,62 @@
1/* mpihelp-mul_1.c - MPI helper functions
2 * Copyright (C) 1994, 1996, 1997, 1998, 2001,
3 * 2002 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 *
21 * Note: This code is heavily based on the GNU MP Library.
22 * Actually it's the same code with only minor changes in the
23 * way the data is stored; this is to support the abstraction
24 * of an optional secure memory allocation which may be used
25 * to avoid revealing of sensitive data due to paging etc.
26 */
27
28#include <config.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include "mpi-internal.h"
32#include "longlong.h"
33
34mpi_limb_t
35_gcry_mpih_mul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
36 mpi_limb_t s2_limb)
37{
38 mpi_limb_t cy_limb;
39 mpi_size_t j;
40 mpi_limb_t prod_high, prod_low;
41
42 /* The loop counter and index J goes from -S1_SIZE to -1. This way
43 * the loop becomes faster. */
44 j = -s1_size;
45
46 /* Offset the base pointers to compensate for the negative indices. */
47 s1_ptr -= j;
48 res_ptr -= j;
49
50 cy_limb = 0;
51 do
52 {
53 umul_ppmm( prod_high, prod_low, s1_ptr[j], s2_limb );
54 prod_low += cy_limb;
55 cy_limb = (prod_low < cy_limb?1:0) + prod_high;
56 res_ptr[j] = prod_low;
57 }
58 while( ++j );
59
60 return cy_limb;
61}
62
diff --git a/pwmanager/libcrypt/mpi/generic/mpih-mul2.c b/pwmanager/libcrypt/mpi/generic/mpih-mul2.c
new file mode 100644
index 0000000..3b75496
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/generic/mpih-mul2.c
@@ -0,0 +1,68 @@
1/* mpih-mul2.c - MPI helper functions
2 * Copyright (C) 1994, 1996, 1997, 1998, 2001,
3 * 2002 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 *
21 * Note: This code is heavily based on the GNU MP Library.
22 * Actually it's the same code with only minor changes in the
23 * way the data is stored; this is to support the abstraction
24 * of an optional secure memory allocation which may be used
25 * to avoid revealing of sensitive data due to paging etc.
26 */
27
28#include <config.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include "mpi-internal.h"
32#include "longlong.h"
33
34
35mpi_limb_t
36_gcry_mpih_addmul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
37 mpi_size_t s1_size, mpi_limb_t s2_limb)
38{
39 mpi_limb_t cy_limb;
40 mpi_size_t j;
41 mpi_limb_t prod_high, prod_low;
42 mpi_limb_t x;
43
44 /* The loop counter and index J goes from -SIZE to -1. This way
45 * the loop becomes faster. */
46 j = -s1_size;
47 res_ptr -= j;
48 s1_ptr -= j;
49
50 cy_limb = 0;
51 do
52 {
53 umul_ppmm( prod_high, prod_low, s1_ptr[j], s2_limb );
54
55 prod_low += cy_limb;
56 cy_limb = (prod_low < cy_limb?1:0) + prod_high;
57
58 x = res_ptr[j];
59 prod_low = x + prod_low;
60 cy_limb += prod_low < x?1:0;
61 res_ptr[j] = prod_low;
62 }
63 while ( ++j );
64
65 return cy_limb;
66}
67
68
diff --git a/pwmanager/libcrypt/mpi/generic/mpih-mul3.c b/pwmanager/libcrypt/mpi/generic/mpih-mul3.c
new file mode 100644
index 0000000..5e84f94
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/generic/mpih-mul3.c
@@ -0,0 +1,68 @@
1/* mpih-mul3.c - MPI helper functions
2 * Copyright (C) 1994, 1996, 1997, 1998, 2001,
3 * 2002 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 *
21 * Note: This code is heavily based on the GNU MP Library.
22 * Actually it's the same code with only minor changes in the
23 * way the data is stored; this is to support the abstraction
24 * of an optional secure memory allocation which may be used
25 * to avoid revealing of sensitive data due to paging etc.
26 */
27
28#include <config.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include "mpi-internal.h"
32#include "longlong.h"
33
34
35mpi_limb_t
36_gcry_mpih_submul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
37 mpi_size_t s1_size, mpi_limb_t s2_limb)
38{
39 mpi_limb_t cy_limb;
40 mpi_size_t j;
41 mpi_limb_t prod_high, prod_low;
42 mpi_limb_t x;
43
44 /* The loop counter and index J goes from -SIZE to -1. This way
45 * the loop becomes faster. */
46 j = -s1_size;
47 res_ptr -= j;
48 s1_ptr -= j;
49
50 cy_limb = 0;
51 do
52 {
53 umul_ppmm( prod_high, prod_low, s1_ptr[j], s2_limb);
54
55 prod_low += cy_limb;
56 cy_limb = (prod_low < cy_limb?1:0) + prod_high;
57
58 x = res_ptr[j];
59 prod_low = x - prod_low;
60 cy_limb += prod_low > x?1:0;
61 res_ptr[j] = prod_low;
62 }
63 while( ++j );
64
65 return cy_limb;
66}
67
68
diff --git a/pwmanager/libcrypt/mpi/generic/mpih-rshift.c b/pwmanager/libcrypt/mpi/generic/mpih-rshift.c
new file mode 100644
index 0000000..e40794f
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/generic/mpih-rshift.c
@@ -0,0 +1,67 @@
1/* mpih-rshift.c - MPI helper functions
2 * Copyright (C) 1994, 1996, 1998, 1999,
3 * 2000, 2001, 2002 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 *
21 * Note: This code is heavily based on the GNU MP Library.
22 * Actually it's the same code with only minor changes in the
23 * way the data is stored; this is to support the abstraction
24 * of an optional secure memory allocation which may be used
25 * to avoid revealing of sensitive data due to paging etc.
26 */
27
28#include <config.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include "mpi-internal.h"
32
33
34/* Shift U (pointed to by UP and USIZE limbs long) CNT bits to the right
35 * and store the USIZE least significant limbs of the result at WP.
36 * The bits shifted out to the right are returned.
37 *
38 * Argument constraints:
39 * 1. 0 < CNT < BITS_PER_MP_LIMB
40 * 2. If the result is to be written over the input, WP must be <= UP.
41 */
42
43mpi_limb_t
44_gcry_mpih_rshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize, unsigned cnt)
45{
46 mpi_limb_t high_limb, low_limb;
47 unsigned sh_1, sh_2;
48 mpi_size_t i;
49 mpi_limb_t retval;
50
51 sh_1 = cnt;
52 wp -= 1;
53 sh_2 = BITS_PER_MPI_LIMB - sh_1;
54 high_limb = up[0];
55 retval = high_limb << sh_2;
56 low_limb = high_limb;
57 for (i=1; i < usize; i++)
58 {
59 high_limb = up[i];
60 wp[i] = (low_limb >> sh_1) | (high_limb << sh_2);
61 low_limb = high_limb;
62 }
63 wp[i] = low_limb >> sh_1;
64
65 return retval;
66}
67
diff --git a/pwmanager/libcrypt/mpi/generic/mpih-sub1.c b/pwmanager/libcrypt/mpi/generic/mpih-sub1.c
new file mode 100644
index 0000000..e88821b
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/generic/mpih-sub1.c
@@ -0,0 +1,66 @@
1/* mpihelp-add_2.c - MPI helper functions
2 * Copyright (C) 1994, 1996, 1997, 1998, 2001,
3 * 2002 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 *
21 * Note: This code is heavily based on the GNU MP Library.
22 * Actually it's the same code with only minor changes in the
23 * way the data is stored; this is to support the abstraction
24 * of an optional secure memory allocation which may be used
25 * to avoid revealing of sensitive data due to paging etc.
26 */
27
28#include <config.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include "mpi-internal.h"
32#include "longlong.h"
33
34mpi_limb_t
35_gcry_mpih_sub_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
36 mpi_ptr_t s2_ptr, mpi_size_t size)
37{
38 mpi_limb_t x, y, cy;
39 mpi_size_t j;
40
41 /* The loop counter and index J goes from -SIZE to -1. This way
42 the loop becomes faster. */
43 j = -size;
44
45 /* Offset the base pointers to compensate for the negative indices. */
46 s1_ptr -= j;
47 s2_ptr -= j;
48 res_ptr -= j;
49
50 cy = 0;
51 do
52 {
53 y = s2_ptr[j];
54 x = s1_ptr[j];
55 y += cy; /* add previous carry to subtrahend */
56 cy = y < cy; /* get out carry from that addition */
57 y = x - y; /* main subtract */
58 cy += y > x; /* get out carry from the subtract, combine */
59 res_ptr[j] = y;
60 }
61 while( ++j );
62
63 return cy;
64}
65
66
diff --git a/pwmanager/libcrypt/mpi/generic/udiv-w-sdiv.c b/pwmanager/libcrypt/mpi/generic/udiv-w-sdiv.c
new file mode 100644
index 0000000..e80d98b
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/generic/udiv-w-sdiv.c
@@ -0,0 +1,133 @@
1/* mpih-w-sdiv -- implement udiv_qrnnd on machines with only signed
2 * division.
3 * Copyright (C) 1992, 1994, 1996, 1998, 2002 Free Software Foundation, Inc.
4 * Contributed by Peter L. Montgomery.
5 *
6 * This file is part of Libgcrypt.
7 *
8 * Libgcrypt is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
12 *
13 * Libgcrypt is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 */
22
23#include <config.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include "mpi-internal.h"
27#include "longlong.h"
28
29
30#if 0 /* not yet ported to MPI */
31
32mpi_limb_t
33mpihelp_udiv_w_sdiv( mpi_limp_t *rp,
34 mpi_limp_t *a1,
35 mpi_limp_t *a0,
36 mpi_limp_t *d )
37{
38 mp_limb_t q, r;
39 mp_limb_t c0, c1, b1;
40
41 if ((mpi_limb_signed_t) d >= 0)
42 {
43 if (a1 < d - a1 - (a0 >> (BITS_PER_MP_LIMB - 1)))
44 {
45 /* dividend, divisor, and quotient are nonnegative */
46 sdiv_qrnnd (q, r, a1, a0, d);
47 }
48 else
49 {
50 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
51 sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (BITS_PER_MP_LIMB - 1));
52 /* Divide (c1*2^32 + c0) by d */
53 sdiv_qrnnd (q, r, c1, c0, d);
54 /* Add 2^31 to quotient */
55 q += (mp_limb_t) 1 << (BITS_PER_MP_LIMB - 1);
56 }
57 }
58 else
59 {
60 b1 = d >> 1; /* d/2, between 2^30 and 2^31 - 1 */
61 c1 = a1 >> 1; /* A/2 */
62 c0 = (a1 << (BITS_PER_MP_LIMB - 1)) + (a0 >> 1);
63
64 if (a1 < b1) /* A < 2^32*b1, so A/2 < 2^31*b1 */
65 {
66 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
67
68 r = 2*r + (a0 & 1); /* Remainder from A/(2*b1) */
69 if ((d & 1) != 0)
70 {
71 if (r >= q)
72 r = r - q;
73 else if (q - r <= d)
74 {
75 r = r - q + d;
76 q--;
77 }
78 else
79 {
80 r = r - q + 2*d;
81 q -= 2;
82 }
83 }
84 }
85 else if (c1 < b1) /* So 2^31 <= (A/2)/b1 < 2^32 */
86 {
87 c1 = (b1 - 1) - c1;
88 c0 = ~c0; /* logical NOT */
89
90 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
91
92 q = ~q; /* (A/2)/b1 */
93 r = (b1 - 1) - r;
94
95 r = 2*r + (a0 & 1); /* A/(2*b1) */
96
97 if ((d & 1) != 0)
98 {
99 if (r >= q)
100 r = r - q;
101 else if (q - r <= d)
102 {
103 r = r - q + d;
104 q--;
105 }
106 else
107 {
108 r = r - q + 2*d;
109 q -= 2;
110 }
111 }
112 }
113 else /* Implies c1 = b1 */
114 { /* Hence a1 = d - 1 = 2*b1 - 1 */
115 if (a0 >= -d)
116 {
117 q = -1;
118 r = a0 + d;
119 }
120 else
121 {
122 q = -2;
123 r = a0 + 2*d;
124 }
125 }
126 }
127
128 *rp = r;
129 return q;
130}
131
132#endif
133
diff --git a/pwmanager/libcrypt/mpi/longlong.h b/pwmanager/libcrypt/mpi/longlong.h
new file mode 100644
index 0000000..34bd50c
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/longlong.h
@@ -0,0 +1,1561 @@
1/* longlong.h -- definitions for mixed size 32/64 bit arithmetic.
2 Note: I added some stuff for use with gnupg
3
4Copyright (C) 1991, 1992, 1993, 1994, 1996, 1998,
5 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
6
7This file is free software; you can redistribute it and/or modify
8it under the terms of the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 2.1 of the License, or (at your
10option) any later version.
11
12This file is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Library General Public License
18along with this file; see the file COPYING.LIB. If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20MA 02111-1307, USA. */
21
22/* You have to define the following before including this file:
23
24 UWtype -- An unsigned type, default type for operations (typically a "word")
25 UHWtype -- An unsigned type, at least half the size of UWtype.
26 UDWtype -- An unsigned type, at least twice as large a UWtype
27 W_TYPE_SIZE -- size in bits of UWtype
28
29 SItype, USItype -- Signed and unsigned 32 bit types.
30 DItype, UDItype -- Signed and unsigned 64 bit types.
31
32 On a 32 bit machine UWtype should typically be USItype;
33 on a 64 bit machine, UWtype should typically be UDItype.
34*/
35
36#define __BITS4 (W_TYPE_SIZE / 4)
37#define __ll_B ((UWtype) 1 << (W_TYPE_SIZE / 2))
38#define __ll_lowpart(t) ((UWtype) (t) & (__ll_B - 1))
39#define __ll_highpart(t) ((UWtype) (t) >> (W_TYPE_SIZE / 2))
40
41/* This is used to make sure no undesirable sharing between different libraries
42 that use this file takes place. */
43#ifndef __MPN
44#define __MPN(x) __##x
45#endif
46
47/* Define auxiliary asm macros.
48
49 1) umul_ppmm(high_prod, low_prod, multipler, multiplicand) multiplies two
50 UWtype integers MULTIPLER and MULTIPLICAND, and generates a two UWtype
51 word product in HIGH_PROD and LOW_PROD.
52
53 2) __umulsidi3(a,b) multiplies two UWtype integers A and B, and returns a
54 UDWtype product. This is just a variant of umul_ppmm.
55
56 3) udiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
57 denominator) divides a UDWtype, composed by the UWtype integers
58 HIGH_NUMERATOR and LOW_NUMERATOR, by DENOMINATOR and places the quotient
59 in QUOTIENT and the remainder in REMAINDER.HIGH_NUMERATOR must be less
60 than DENOMINATOR for correct operation. If, in addition, the most
61 significant bit of DENOMINATOR must be 1, then the pre-processor symbol
62 UDIV_NEEDS_NORMALIZATION is defined to 1.
63
64 4) sdiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
65 denominator). Like udiv_qrnnd but the numbers are signed. The quotient
66 is rounded towards 0.
67
68 5) count_leading_zeros(count, x) counts the number of zero-bits from the
69 msb to the first non-zero bit in the UWtype X. This is the number of
70 steps X needs to be shifted left to set the msb. Undefined for X == 0,
71 unless the symbol COUNT_LEADING_ZEROS_0 is defined to some value.
72
73 6) count_trailing_zeros(count, x) like count_leading_zeros, but counts
74 from the least significant end.
75
76 7) add_ssaaaa(high_sum, low_sum, high_addend_1, low_addend_1,
77 high_addend_2, low_addend_2) adds two UWtype integers, composed by
78 HIGH_ADDEND_1 and LOW_ADDEND_1, and HIGH_ADDEND_2 and LOW_ADDEND_2
79 respectively. The result is placed in HIGH_SUM and LOW_SUM. Overflow
80 (i.e. carry out) is not stored anywhere, and is lost.
81
82 8) sub_ddmmss(high_difference, low_difference, high_minuend, low_minuend,
83 high_subtrahend, low_subtrahend) subtracts two two-word UWtype integers,
84 composed by HIGH_MINUEND_1 and LOW_MINUEND_1, and HIGH_SUBTRAHEND_2 and
85 LOW_SUBTRAHEND_2 respectively. The result is placed in HIGH_DIFFERENCE
86 and LOW_DIFFERENCE.Overflow (i.e. carry out) is not stored anywhere,
87 and is lost.
88
89 If any of these macros are left undefined for a particular CPU,
90 C macros are used. */
91
92/* The CPUs come in alphabetical order below.
93
94 Please add support for more CPUs here, or improve the current support
95 for the CPUs below!*/
96
97#ifdef __riscos__
98#pragma continue_after_hash_error
99#else /* !__riscos__ */
100#if defined (__GNUC__) && !defined (NO_ASM)
101
102/* We sometimes need to clobber "cc" with gcc2, but that would not be
103 understood by gcc1.Use cpp to avoid major code duplication. */
104#if __GNUC__ < 2
105#define __CLOBBER_CC
106#define __AND_CLOBBER_CC
107#else /* __GNUC__ >= 2 */
108#define __CLOBBER_CC : "cc"
109#define __AND_CLOBBER_CC , "cc"
110#endif /* __GNUC__ < 2 */
111
112
113/***************************************
114 ************** A29K *****************
115 ***************************************/
116#if (defined (__a29k__) || defined (_AM29K)) && W_TYPE_SIZE == 32
117#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
118 __asm__ ("add %1,%4,%5\n" \
119 "addc %0,%2,%3" \
120 : "=r" ((USItype)(sh)), \
121 "=&r" ((USItype)(sl)) \
122 : "%r" ((USItype)(ah)), \
123 "rI" ((USItype)(bh)), \
124 "%r" ((USItype)(al)), \
125 "rI" ((USItype)(bl)))
126#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
127 __asm__ ("sub %1,%4,%5\n" \
128 "subc %0,%2,%3" \
129 : "=r" ((USItype)(sh)), \
130 "=&r" ((USItype)(sl)) \
131 : "r" ((USItype)(ah)), \
132 "rI" ((USItype)(bh)), \
133 "r" ((USItype)(al)), \
134 "rI" ((USItype)(bl)))
135#define umul_ppmm(xh, xl, m0, m1) \
136 do { \
137 USItype __m0 = (m0), __m1 = (m1); \
138 __asm__ ("multiplu %0,%1,%2" \
139 : "=r" ((USItype)(xl)) \
140 : "r" (__m0), \
141 "r" (__m1)); \
142 __asm__ ("multmu %0,%1,%2" \
143 : "=r" ((USItype)(xh)) \
144 : "r" (__m0), \
145 "r" (__m1)); \
146 } while (0)
147#define udiv_qrnnd(q, r, n1, n0, d) \
148 __asm__ ("dividu %0,%3,%4" \
149 : "=r" ((USItype)(q)), \
150 "=q" ((USItype)(r)) \
151 : "1" ((USItype)(n1)), \
152 "r" ((USItype)(n0)), \
153 "r" ((USItype)(d)))
154#define count_leading_zeros(count, x) \
155 __asm__ ("clz %0,%1" \
156 : "=r" ((USItype)(count)) \
157 : "r" ((USItype)(x)))
158#define COUNT_LEADING_ZEROS_0 32
159#endif /* __a29k__ */
160
161
162#if defined (__alpha) && W_TYPE_SIZE == 64
163#define umul_ppmm(ph, pl, m0, m1) \
164 do { \
165 UDItype __m0 = (m0), __m1 = (m1); \
166 __asm__ ("umulh %r1,%2,%0" \
167 : "=r" ((UDItype) ph) \
168 : "%rJ" (__m0), \
169 "rI" (__m1)); \
170 (pl) = __m0 * __m1; \
171 } while (0)
172#define UMUL_TIME 46
173#ifndef LONGLONG_STANDALONE
174#define udiv_qrnnd(q, r, n1, n0, d) \
175 do { UDItype __r; \
176 (q) = __udiv_qrnnd (&__r, (n1), (n0), (d)); \
177 (r) = __r; \
178 } while (0)
179extern UDItype __udiv_qrnnd ();
180#define UDIV_TIME 220
181#endif /* LONGLONG_STANDALONE */
182#endif /* __alpha */
183
184/***************************************
185 ************** ARM ******************
186 ***************************************/
187#if defined (__arm__) && W_TYPE_SIZE == 32
188#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
189 __asm__ ("adds %1, %4, %5\n" \
190 "adc %0, %2, %3" \
191 : "=r" ((USItype)(sh)), \
192 "=&r" ((USItype)(sl)) \
193 : "%r" ((USItype)(ah)), \
194 "rI" ((USItype)(bh)), \
195 "%r" ((USItype)(al)), \
196 "rI" ((USItype)(bl)))
197#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
198 __asm__ ("subs %1, %4, %5\n" \
199 "sbc %0, %2, %3" \
200 : "=r" ((USItype)(sh)), \
201 "=&r" ((USItype)(sl)) \
202 : "r" ((USItype)(ah)), \
203 "rI" ((USItype)(bh)), \
204 "r" ((USItype)(al)), \
205 "rI" ((USItype)(bl)))
206#if defined __ARM_ARCH_2__ || defined __ARM_ARCH_3__
207#define umul_ppmm(xh, xl, a, b) \
208 __asm__ ("%@ Inlined umul_ppmm\n" \
209 "mov %|r0, %2, lsr #16 @ AAAA\n" \
210 "mov %|r2, %3, lsr #16 @ BBBB\n" \
211 "bic %|r1, %2, %|r0, lsl #16 @ aaaa\n" \
212 "bic %0, %3, %|r2, lsl #16 @ bbbb\n" \
213 "mul %1, %|r1, %|r2 @ aaaa * BBBB\n" \
214 "mul %|r2, %|r0, %|r2 @ AAAA * BBBB\n" \
215 "mul %|r1, %0, %|r1 @ aaaa * bbbb\n" \
216 "mul %0, %|r0, %0 @ AAAA * bbbb\n" \
217 "adds %|r0, %1, %0 @ central sum\n" \
218 "addcs%|r2, %|r2, #65536\n" \
219 "adds%1, %|r1, %|r0, lsl #16\n" \
220 "adc%0, %|r2, %|r0, lsr #16" \
221 : "=&r" ((USItype)(xh)), \
222 "=r" ((USItype)(xl)) \
223 : "r" ((USItype)(a)), \
224 "r" ((USItype)(b)) \
225 : "r0", "r1", "r2")
226#else
227#define umul_ppmm(xh, xl, a, b) \
228 __asm__ ("%@ Inlined umul_ppmm\n" \
229 "umull %r1, %r0, %r2, %r3" \
230 : "=&r" ((USItype)(xh)), \
231 "=r" ((USItype)(xl)) \
232 : "r" ((USItype)(a)), \
233 "r" ((USItype)(b)) \
234 : "r0", "r1")
235#endif
236#define UMUL_TIME 20
237#define UDIV_TIME 100
238#endif /* __arm__ */
239
240/***************************************
241 ************** CLIPPER **************
242 ***************************************/
243#if defined (__clipper__) && W_TYPE_SIZE == 32
244#define umul_ppmm(w1, w0, u, v) \
245 ({union {UDItype __ll; \
246 struct {USItype __l, __h;} __i; \
247 } __xx; \
248 __asm__ ("mulwux %2,%0" \
249 : "=r" (__xx.__ll) \
250 : "%0" ((USItype)(u)), \
251 "r" ((USItype)(v))); \
252 (w1) = __xx.__i.__h; (w0) = __xx.__i.__l;})
253#define smul_ppmm(w1, w0, u, v) \
254 ({union {DItype __ll; \
255 struct {SItype __l, __h;} __i; \
256 } __xx; \
257 __asm__ ("mulwx %2,%0" \
258 : "=r" (__xx.__ll) \
259 : "%0" ((SItype)(u)), \
260 "r" ((SItype)(v))); \
261 (w1) = __xx.__i.__h; (w0) = __xx.__i.__l;})
262#define __umulsidi3(u, v) \
263 ({UDItype __w; \
264 __asm__ ("mulwux %2,%0" \
265 : "=r" (__w) \
266 : "%0" ((USItype)(u)), \
267 "r" ((USItype)(v))); \
268 __w; })
269#endif /* __clipper__ */
270
271
272/***************************************
273 ************** GMICRO ***************
274 ***************************************/
275#if defined (__gmicro__) && W_TYPE_SIZE == 32
276#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
277 __asm__ ("add.w %5,%1\n" \
278 "addx %3,%0" \
279 : "=g" ((USItype)(sh)), \
280 "=&g" ((USItype)(sl)) \
281 : "%0" ((USItype)(ah)), \
282 "g" ((USItype)(bh)), \
283 "%1" ((USItype)(al)), \
284 "g" ((USItype)(bl)))
285#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
286 __asm__ ("sub.w %5,%1\n" \
287 "subx %3,%0" \
288 : "=g" ((USItype)(sh)), \
289 "=&g" ((USItype)(sl)) \
290 : "0" ((USItype)(ah)), \
291 "g" ((USItype)(bh)), \
292 "1" ((USItype)(al)), \
293 "g" ((USItype)(bl)))
294#define umul_ppmm(ph, pl, m0, m1) \
295 __asm__ ("mulx %3,%0,%1" \
296 : "=g" ((USItype)(ph)), \
297 "=r" ((USItype)(pl)) \
298 : "%0" ((USItype)(m0)), \
299 "g" ((USItype)(m1)))
300#define udiv_qrnnd(q, r, nh, nl, d) \
301 __asm__ ("divx %4,%0,%1" \
302 : "=g" ((USItype)(q)), \
303 "=r" ((USItype)(r)) \
304 : "1" ((USItype)(nh)), \
305 "0" ((USItype)(nl)), \
306 "g" ((USItype)(d)))
307#define count_leading_zeros(count, x) \
308 __asm__ ("bsch/1 %1,%0" \
309 : "=g" (count) \
310 : "g" ((USItype)(x)), \
311 "0" ((USItype)0))
312#endif
313
314
315/***************************************
316 ************** HPPA *****************
317 ***************************************/
318#if defined (__hppa) && W_TYPE_SIZE == 32
319#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
320 __asm__ ("add %4,%5,%1\n" \
321 "addc %2,%3,%0" \
322 : "=r" ((USItype)(sh)), \
323 "=&r" ((USItype)(sl)) \
324 : "%rM" ((USItype)(ah)), \
325 "rM" ((USItype)(bh)), \
326 "%rM" ((USItype)(al)), \
327 "rM" ((USItype)(bl)))
328#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
329 __asm__ ("sub %4,%5,%1\n" \
330 "subb %2,%3,%0" \
331 : "=r" ((USItype)(sh)), \
332 "=&r" ((USItype)(sl)) \
333 : "rM" ((USItype)(ah)), \
334 "rM" ((USItype)(bh)), \
335 "rM" ((USItype)(al)), \
336 "rM" ((USItype)(bl)))
337#if defined (_PA_RISC1_1)
338#define umul_ppmm(wh, wl, u, v) \
339 do { \
340 union {UDItype __ll; \
341 struct {USItype __h, __l;} __i; \
342 } __xx; \
343 __asm__ ("xmpyu %1,%2,%0" \
344 : "=*f" (__xx.__ll) \
345 : "*f" ((USItype)(u)), \
346 "*f" ((USItype)(v))); \
347 (wh) = __xx.__i.__h; \
348 (wl) = __xx.__i.__l; \
349 } while (0)
350#define UMUL_TIME 8
351#define UDIV_TIME 60
352#else
353#define UMUL_TIME 40
354#define UDIV_TIME 80
355#endif
356#ifndef LONGLONG_STANDALONE
357#define udiv_qrnnd(q, r, n1, n0, d) \
358 do { USItype __r; \
359 (q) = __udiv_qrnnd (&__r, (n1), (n0), (d)); \
360 (r) = __r; \
361 } while (0)
362extern USItype __udiv_qrnnd ();
363#endif /* LONGLONG_STANDALONE */
364#define count_leading_zeros(count, x) \
365 do { \
366 USItype __tmp; \
367 __asm__ ( \
368 "ldi 1,%0 \n" \
369 " extru,= %1,15,16,%%r0 ; Bits 31..16 zero? \n" \
370 " extru,tr%1,15,16,%1 ; No. Shift down, skip add.\n" \
371 " ldo 16(%0),%0 ; Yes.Perform add. \n" \
372 " extru,= %1,23,8,%%r0 ; Bits 15..8 zero? \n" \
373 " extru,tr%1,23,8,%1 ; No. Shift down, skip add.\n" \
374 " ldo 8(%0),%0 ; Yes.Perform add. \n" \
375 " extru,= %1,27,4,%%r0 ; Bits 7..4 zero? \n" \
376 " extru,tr%1,27,4,%1 ; No. Shift down, skip add.\n" \
377 " ldo 4(%0),%0 ; Yes.Perform add. \n" \
378 " extru,= %1,29,2,%%r0 ; Bits 3..2 zero? \n" \
379 " extru,tr%1,29,2,%1 ; No. Shift down, skip add.\n" \
380 " ldo 2(%0),%0 ; Yes.Perform add. \n" \
381 " extru %1,30,1,%1 ; Extract bit 1. \n" \
382 " sub %0,%1,%0 ; Subtract it. " \
383 : "=r" (count), "=r" (__tmp) : "1" (x)); \
384 } while (0)
385#endif /* hppa */
386
387
388/***************************************
389 ************** I370 *****************
390 ***************************************/
391#if (defined (__i370__) || defined (__mvs__)) && W_TYPE_SIZE == 32
392#define umul_ppmm(xh, xl, m0, m1) \
393 do { \
394 union {UDItype __ll; \
395 struct {USItype __h, __l;} __i; \
396 } __xx; \
397 USItype __m0 = (m0), __m1 = (m1); \
398 __asm__ ("mr %0,%3" \
399 : "=r" (__xx.__i.__h), \
400 "=r" (__xx.__i.__l) \
401 : "%1" (__m0), \
402 "r" (__m1)); \
403 (xh) = __xx.__i.__h; (xl) = __xx.__i.__l; \
404 (xh) += ((((SItype) __m0 >> 31) & __m1) \
405 + (((SItype) __m1 >> 31) & __m0)); \
406 } while (0)
407#define smul_ppmm(xh, xl, m0, m1) \
408 do { \
409 union {DItype __ll; \
410 struct {USItype __h, __l;} __i; \
411 } __xx; \
412 __asm__ ("mr %0,%3" \
413 : "=r" (__xx.__i.__h), \
414 "=r" (__xx.__i.__l) \
415 : "%1" (m0), \
416 "r" (m1)); \
417 (xh) = __xx.__i.__h; (xl) = __xx.__i.__l; \
418 } while (0)
419#define sdiv_qrnnd(q, r, n1, n0, d) \
420 do { \
421 union {DItype __ll; \
422 struct {USItype __h, __l;} __i; \
423 } __xx; \
424 __xx.__i.__h = n1; __xx.__i.__l = n0; \
425 __asm__ ("dr %0,%2" \
426 : "=r" (__xx.__ll) \
427 : "0" (__xx.__ll), "r" (d)); \
428 (q) = __xx.__i.__l; (r) = __xx.__i.__h; \
429 } while (0)
430#endif
431
432
433/***************************************
434 ************** I386 *****************
435 ***************************************/
436#if (defined (__i386__) || defined (__i486__)) && W_TYPE_SIZE == 32
437#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
438 __asm__ ("addl %5,%1\n" \
439 "adcl %3,%0" \
440 : "=r" ((USItype)(sh)), \
441 "=&r" ((USItype)(sl)) \
442 : "%0" ((USItype)(ah)), \
443 "g" ((USItype)(bh)), \
444 "%1" ((USItype)(al)), \
445 "g" ((USItype)(bl)))
446#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
447 __asm__ ("subl %5,%1\n" \
448 "sbbl %3,%0" \
449 : "=r" ((USItype)(sh)), \
450 "=&r" ((USItype)(sl)) \
451 : "0" ((USItype)(ah)), \
452 "g" ((USItype)(bh)), \
453 "1" ((USItype)(al)), \
454 "g" ((USItype)(bl)))
455#define umul_ppmm(w1, w0, u, v) \
456 __asm__ ("mull %3" \
457 : "=a" ((USItype)(w0)), \
458 "=d" ((USItype)(w1)) \
459 : "%0" ((USItype)(u)), \
460 "rm" ((USItype)(v)))
461#define udiv_qrnnd(q, r, n1, n0, d) \
462 __asm__ ("divl %4" \
463 : "=a" ((USItype)(q)), \
464 "=d" ((USItype)(r)) \
465 : "0" ((USItype)(n0)), \
466 "1" ((USItype)(n1)), \
467 "rm" ((USItype)(d)))
468#define count_leading_zeros(count, x) \
469 do { \
470 USItype __cbtmp; \
471 __asm__ ("bsrl %1,%0" \
472 : "=r" (__cbtmp) : "rm" ((USItype)(x))); \
473 (count) = __cbtmp ^ 31; \
474 } while (0)
475#define count_trailing_zeros(count, x) \
476 __asm__ ("bsfl %1,%0" : "=r" (count) : "rm" ((USItype)(x)))
477#ifndef UMUL_TIME
478#define UMUL_TIME 40
479#endif
480#ifndef UDIV_TIME
481#define UDIV_TIME 40
482#endif
483#endif /* 80x86 */
484
485
486/***************************************
487 ************** I860 *****************
488 ***************************************/
489#if defined (__i860__) && W_TYPE_SIZE == 32
490#define rshift_rhlc(r,h,l,c) \
491 __asm__ ("shr %3,r0,r0\n" \
492 "shrd %1,%2,%0" \
493 "=r" (r) : "r" (h), "r" (l), "rn" (c))
494#endif /* i860 */
495
496/***************************************
497 ************** I960 *****************
498 ***************************************/
499#if defined (__i960__) && W_TYPE_SIZE == 32
500#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
501 __asm__ ("cmpo 1,0\n" \
502 "addc %5,%4,%1\n" \
503 "addc %3,%2,%0" \
504 : "=r" ((USItype)(sh)), \
505 "=&r" ((USItype)(sl)) \
506 : "%dI" ((USItype)(ah)), \
507 "dI" ((USItype)(bh)), \
508 "%dI" ((USItype)(al)), \
509 "dI" ((USItype)(bl)))
510#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
511 __asm__ ("cmpo 0,0\n" \
512 "subc %5,%4,%1\n" \
513 "subc %3,%2,%0" \
514 : "=r" ((USItype)(sh)), \
515 "=&r" ((USItype)(sl)) \
516 : "dI" ((USItype)(ah)), \
517 "dI" ((USItype)(bh)), \
518 "dI" ((USItype)(al)), \
519 "dI" ((USItype)(bl)))
520#define umul_ppmm(w1, w0, u, v) \
521 ({union {UDItype __ll; \
522 struct {USItype __l, __h;} __i; \
523 } __xx; \
524 __asm__ ("emul %2,%1,%0" \
525 : "=d" (__xx.__ll) \
526 : "%dI" ((USItype)(u)), \
527 "dI" ((USItype)(v))); \
528 (w1) = __xx.__i.__h; (w0) = __xx.__i.__l;})
529#define __umulsidi3(u, v) \
530 ({UDItype __w; \
531 __asm__ ("emul %2,%1,%0" \
532 : "=d" (__w) \
533 : "%dI" ((USItype)(u)), \
534 "dI" ((USItype)(v))); \
535 __w; })
536#define udiv_qrnnd(q, r, nh, nl, d) \
537 do { \
538 union {UDItype __ll; \
539 struct {USItype __l, __h;} __i; \
540 } __nn; \
541 __nn.__i.__h = (nh); __nn.__i.__l = (nl); \
542 __asm__ ("ediv %d,%n,%0" \
543 : "=d" (__rq.__ll) \
544 : "dI" (__nn.__ll), \
545 "dI" ((USItype)(d))); \
546 (r) = __rq.__i.__l; (q) = __rq.__i.__h; \
547 } while (0)
548#define count_leading_zeros(count, x) \
549 do { \
550 USItype __cbtmp; \
551 __asm__ ("scanbit %1,%0" \
552 : "=r" (__cbtmp) \
553 : "r" ((USItype)(x))); \
554 (count) = __cbtmp ^ 31; \
555 } while (0)
556#define COUNT_LEADING_ZEROS_0 (-32) /* sic */
557 #if defined (__i960mx) /* what is the proper symbol to test??? */
558#define rshift_rhlc(r,h,l,c) \
559 do { \
560 union {UDItype __ll; \
561 struct {USItype __l, __h;} __i; \
562 } __nn; \
563 __nn.__i.__h = (h); __nn.__i.__l = (l); \
564 __asm__ ("shre %2,%1,%0" \
565 : "=d" (r) : "dI" (__nn.__ll), "dI" (c)); \
566 }
567#endif /* i960mx */
568#endif /* i960 */
569
570
571/***************************************
572 ************** 68000****************
573 ***************************************/
574#if (defined (__mc68000__) || defined (__mc68020__) || defined (__NeXT__) || defined(mc68020)) && W_TYPE_SIZE == 32
575#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
576 __asm__ ("add%.l %5,%1\n" \
577 "addx%.l %3,%0" \
578 : "=d" ((USItype)(sh)), \
579 "=&d" ((USItype)(sl)) \
580 : "%0" ((USItype)(ah)), \
581 "d" ((USItype)(bh)), \
582 "%1" ((USItype)(al)), \
583 "g" ((USItype)(bl)))
584#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
585 __asm__ ("sub%.l %5,%1\n" \
586 "subx%.l %3,%0" \
587 : "=d" ((USItype)(sh)), \
588 "=&d" ((USItype)(sl)) \
589 : "0" ((USItype)(ah)), \
590 "d" ((USItype)(bh)), \
591 "1" ((USItype)(al)), \
592 "g" ((USItype)(bl)))
593#if (defined (__mc68020__) || defined (__NeXT__) || defined(mc68020))
594#define umul_ppmm(w1, w0, u, v) \
595 __asm__ ("mulu%.l %3,%1:%0" \
596 : "=d" ((USItype)(w0)), \
597 "=d" ((USItype)(w1)) \
598 : "%0" ((USItype)(u)), \
599 "dmi" ((USItype)(v)))
600#define UMUL_TIME 45
601#define udiv_qrnnd(q, r, n1, n0, d) \
602 __asm__ ("divu%.l %4,%1:%0" \
603 : "=d" ((USItype)(q)), \
604 "=d" ((USItype)(r)) \
605 : "0" ((USItype)(n0)), \
606 "1" ((USItype)(n1)), \
607 "dmi" ((USItype)(d)))
608#define UDIV_TIME 90
609#define sdiv_qrnnd(q, r, n1, n0, d) \
610 __asm__ ("divs%.l %4,%1:%0" \
611 : "=d" ((USItype)(q)), \
612 "=d" ((USItype)(r)) \
613 : "0" ((USItype)(n0)), \
614 "1" ((USItype)(n1)), \
615 "dmi" ((USItype)(d)))
616#define count_leading_zeros(count, x) \
617 __asm__ ("bfffo %1{%b2:%b2},%0" \
618 : "=d" ((USItype)(count)) \
619 : "od" ((USItype)(x)), "n" (0))
620#define COUNT_LEADING_ZEROS_0 32
621#else /* not mc68020 */
622#define umul_ppmm(xh, xl, a, b) \
623 do { USItype __umul_tmp1, __umul_tmp2; \
624 __asm__ ("| Inlined umul_ppmm \n" \
625 " move%.l %5,%3 \n" \
626 " move%.l %2,%0 \n" \
627 " move%.w %3,%1 \n" \
628 " swap%3 \n" \
629 " swap%0 \n" \
630 " mulu%2,%1 \n" \
631 " mulu%3,%0 \n" \
632 " mulu%2,%3 \n" \
633 " swap%2 \n" \
634 " mulu%5,%2 \n" \
635 " add%.l%3,%2 \n" \
636 " jcc1f \n" \
637 " add%.l%#0x10000,%0 \n" \
638 "1:move%.l %2,%3 \n" \
639 " clr%.w%2 \n" \
640 " swap%2 \n" \
641 " swap%3 \n" \
642 " clr%.w%3 \n" \
643 " add%.l%3,%1 \n" \
644 " addx%.l %2,%0 \n" \
645 " | End inlined umul_ppmm" \
646 : "=&d" ((USItype)(xh)), "=&d" ((USItype)(xl)), \
647 "=d" (__umul_tmp1), "=&d" (__umul_tmp2) \
648 : "%2" ((USItype)(a)), "d" ((USItype)(b))); \
649 } while (0)
650#define UMUL_TIME 100
651#define UDIV_TIME 400
652#endif /* not mc68020 */
653#endif /* mc68000 */
654
655
656/***************************************
657 ************** 88000****************
658 ***************************************/
659#if defined (__m88000__) && W_TYPE_SIZE == 32
660#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
661 __asm__ ("addu.co %1,%r4,%r5\n" \
662 "addu.ci %0,%r2,%r3" \
663 : "=r" ((USItype)(sh)), \
664 "=&r" ((USItype)(sl)) \
665 : "%rJ" ((USItype)(ah)), \
666 "rJ" ((USItype)(bh)), \
667 "%rJ" ((USItype)(al)), \
668 "rJ" ((USItype)(bl)))
669#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
670 __asm__ ("subu.co %1,%r4,%r5\n" \
671 "subu.ci %0,%r2,%r3" \
672 : "=r" ((USItype)(sh)), \
673 "=&r" ((USItype)(sl)) \
674 : "rJ" ((USItype)(ah)), \
675 "rJ" ((USItype)(bh)), \
676 "rJ" ((USItype)(al)), \
677 "rJ" ((USItype)(bl)))
678#define count_leading_zeros(count, x) \
679 do { \
680 USItype __cbtmp; \
681 __asm__ ("ff1 %0,%1" \
682 : "=r" (__cbtmp) \
683 : "r" ((USItype)(x))); \
684 (count) = __cbtmp ^ 31; \
685 } while (0)
686#define COUNT_LEADING_ZEROS_0 63 /* sic */
687#if defined (__m88110__)
688#define umul_ppmm(wh, wl, u, v) \
689 do { \
690 union {UDItype __ll; \
691 struct {USItype __h, __l;} __i; \
692 } __x; \
693 __asm__ ("mulu.d %0,%1,%2" : "=r" (__x.__ll) : "r" (u), "r" (v)); \
694 (wh) = __x.__i.__h; \
695 (wl) = __x.__i.__l; \
696 } while (0)
697#define udiv_qrnnd(q, r, n1, n0, d) \
698 ({union {UDItype __ll; \
699 struct {USItype __h, __l;} __i; \
700 } __x, __q; \
701 __x.__i.__h = (n1); __x.__i.__l = (n0); \
702 __asm__ ("divu.d %0,%1,%2" \
703 : "=r" (__q.__ll) : "r" (__x.__ll), "r" (d)); \
704 (r) = (n0) - __q.__l * (d); (q) = __q.__l; })
705#define UMUL_TIME 5
706#define UDIV_TIME 25
707#else
708#define UMUL_TIME 17
709#define UDIV_TIME 150
710#endif /* __m88110__ */
711#endif /* __m88000__ */
712
713/***************************************
714 ************** MIPS *****************
715 ***************************************/
716#if defined (__mips__) && W_TYPE_SIZE == 32
717#if __GNUC__ > 2 || __GNUC_MINOR__ >= 7
718#define umul_ppmm(w1, w0, u, v) \
719 __asm__ ("multu %2,%3" \
720 : "=l" ((USItype)(w0)), \
721 "=h" ((USItype)(w1)) \
722 : "d" ((USItype)(u)), \
723 "d" ((USItype)(v)))
724#else
725#define umul_ppmm(w1, w0, u, v) \
726 __asm__ ("multu %2,%3 \n" \
727 "mflo %0 \n" \
728 "mfhi %1" \
729 : "=d" ((USItype)(w0)), \
730 "=d" ((USItype)(w1)) \
731 : "d" ((USItype)(u)), \
732 "d" ((USItype)(v)))
733#endif
734#define UMUL_TIME 10
735#define UDIV_TIME 100
736#endif /* __mips__ */
737
738/***************************************
739 ************** MIPS/64 **************
740 ***************************************/
741#if (defined (__mips) && __mips >= 3) && W_TYPE_SIZE == 64
742#if __GNUC__ > 2 || __GNUC_MINOR__ >= 7
743#define umul_ppmm(w1, w0, u, v) \
744 __asm__ ("dmultu %2,%3" \
745 : "=l" ((UDItype)(w0)), \
746 "=h" ((UDItype)(w1)) \
747 : "d" ((UDItype)(u)), \
748 "d" ((UDItype)(v)))
749#else
750#define umul_ppmm(w1, w0, u, v) \
751 __asm__ ("dmultu %2,%3 \n" \
752 "mflo %0 \n" \
753 "mfhi %1" \
754 : "=d" ((UDItype)(w0)), \
755 "=d" ((UDItype)(w1)) \
756 : "d" ((UDItype)(u)), \
757 "d" ((UDItype)(v)))
758#endif
759#define UMUL_TIME 20
760#define UDIV_TIME 140
761#endif /* __mips__ */
762
763
764/***************************************
765 ************** 32000****************
766 ***************************************/
767#if defined (__ns32000__) && W_TYPE_SIZE == 32
768#define umul_ppmm(w1, w0, u, v) \
769 ({union {UDItype __ll; \
770 struct {USItype __l, __h;} __i; \
771 } __xx; \
772 __asm__ ("meid %2,%0" \
773 : "=g" (__xx.__ll) \
774 : "%0" ((USItype)(u)), \
775 "g" ((USItype)(v))); \
776 (w1) = __xx.__i.__h; (w0) = __xx.__i.__l;})
777#define __umulsidi3(u, v) \
778 ({UDItype __w; \
779 __asm__ ("meid %2,%0" \
780 : "=g" (__w) \
781 : "%0" ((USItype)(u)), \
782 "g" ((USItype)(v))); \
783 __w; })
784#define udiv_qrnnd(q, r, n1, n0, d) \
785 ({union {UDItype __ll; \
786 struct {USItype __l, __h;} __i; \
787 } __xx; \
788 __xx.__i.__h = (n1); __xx.__i.__l = (n0); \
789 __asm__ ("deid %2,%0" \
790 : "=g" (__xx.__ll) \
791 : "0" (__xx.__ll), \
792 "g" ((USItype)(d))); \
793 (r) = __xx.__i.__l; (q) = __xx.__i.__h; })
794#define count_trailing_zeros(count,x) \
795 do {
796 __asm__ ("ffsd %2,%0" \
797 : "=r" ((USItype) (count)) \
798 : "0" ((USItype) 0), \
799 "r" ((USItype) (x))); \
800 } while (0)
801#endif /* __ns32000__ */
802
803
804/***************************************
805 ************** PPC ******************
806 ***************************************/
807#if (defined (_ARCH_PPC) || defined (_IBMR2)) && W_TYPE_SIZE == 32
808#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
809 do { \
810 if (__builtin_constant_p (bh) && (bh) == 0) \
811 __asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{aze|addze} %0,%2" \
812 : "=r" ((USItype)(sh)), \
813 "=&r" ((USItype)(sl)) \
814 : "%r" ((USItype)(ah)), \
815 "%r" ((USItype)(al)), \
816 "rI" ((USItype)(bl))); \
817 else if (__builtin_constant_p (bh) && (bh) ==~(USItype) 0) \
818 __asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{ame|addme} %0,%2" \
819 : "=r" ((USItype)(sh)), \
820 "=&r" ((USItype)(sl)) \
821 : "%r" ((USItype)(ah)), \
822 "%r" ((USItype)(al)), \
823 "rI" ((USItype)(bl))); \
824 else \
825 __asm__ ("{a%I5|add%I5c} %1,%4,%5\n\t{ae|adde} %0,%2,%3" \
826 : "=r" ((USItype)(sh)), \
827 "=&r" ((USItype)(sl)) \
828 : "%r" ((USItype)(ah)), \
829 "r" ((USItype)(bh)), \
830 "%r" ((USItype)(al)), \
831 "rI" ((USItype)(bl))); \
832 } while (0)
833#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
834 do { \
835 if (__builtin_constant_p (ah) && (ah) == 0) \
836 __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfze|subfze} %0,%2" \
837 : "=r" ((USItype)(sh)), \
838 "=&r" ((USItype)(sl)) \
839 : "r" ((USItype)(bh)), \
840 "rI" ((USItype)(al)), \
841 "r" ((USItype)(bl))); \
842 else if (__builtin_constant_p (ah) && (ah) ==~(USItype) 0) \
843 __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfme|subfme} %0,%2" \
844 : "=r" ((USItype)(sh)), \
845 "=&r" ((USItype)(sl)) \
846 : "r" ((USItype)(bh)), \
847 "rI" ((USItype)(al)), \
848 "r" ((USItype)(bl))); \
849 else if (__builtin_constant_p (bh) && (bh) == 0) \
850 __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{ame|addme} %0,%2" \
851 : "=r" ((USItype)(sh)), \
852 "=&r" ((USItype)(sl)) \
853 : "r" ((USItype)(ah)), \
854 "rI" ((USItype)(al)), \
855 "r" ((USItype)(bl))); \
856 else if (__builtin_constant_p (bh) && (bh) ==~(USItype) 0) \
857 __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{aze|addze} %0,%2" \
858 : "=r" ((USItype)(sh)), \
859 "=&r" ((USItype)(sl)) \
860 : "r" ((USItype)(ah)), \
861 "rI" ((USItype)(al)), \
862 "r" ((USItype)(bl))); \
863 else \
864 __asm__ ("{sf%I4|subf%I4c} %1,%5,%4\n\t{sfe|subfe} %0,%3,%2" \
865 : "=r" ((USItype)(sh)), \
866 "=&r" ((USItype)(sl)) \
867 : "r" ((USItype)(ah)), \
868 "r" ((USItype)(bh)), \
869 "rI" ((USItype)(al)), \
870 "r" ((USItype)(bl))); \
871 } while (0)
872#define count_leading_zeros(count, x) \
873 __asm__ ("{cntlz|cntlzw} %0,%1" \
874 : "=r" ((USItype)(count)) \
875 : "r" ((USItype)(x)))
876#define COUNT_LEADING_ZEROS_0 32
877#if defined (_ARCH_PPC)
878#define umul_ppmm(ph, pl, m0, m1) \
879 do { \
880 USItype __m0 = (m0), __m1 = (m1); \
881 __asm__ ("mulhwu %0,%1,%2" \
882 : "=r" ((USItype) ph) \
883 : "%r" (__m0), \
884 "r" (__m1)); \
885 (pl) = __m0 * __m1; \
886 } while (0)
887#define UMUL_TIME 15
888#define smul_ppmm(ph, pl, m0, m1) \
889 do { \
890 SItype __m0 = (m0), __m1 = (m1); \
891 __asm__ ("mulhw %0,%1,%2" \
892 : "=r" ((SItype) ph) \
893 : "%r" (__m0), \
894 "r" (__m1)); \
895 (pl) = __m0 * __m1; \
896 } while (0)
897#define SMUL_TIME 14
898#define UDIV_TIME 120
899#else
900#define umul_ppmm(xh, xl, m0, m1) \
901 do { \
902 USItype __m0 = (m0), __m1 = (m1); \
903 __asm__ ("mul %0,%2,%3" \
904 : "=r" ((USItype)(xh)), \
905 "=q" ((USItype)(xl)) \
906 : "r" (__m0), \
907 "r" (__m1)); \
908 (xh) += ((((SItype) __m0 >> 31) & __m1) \
909 + (((SItype) __m1 >> 31) & __m0)); \
910 } while (0)
911#define UMUL_TIME 8
912#define smul_ppmm(xh, xl, m0, m1) \
913 __asm__ ("mul %0,%2,%3" \
914 : "=r" ((SItype)(xh)), \
915 "=q" ((SItype)(xl)) \
916 : "r" (m0), \
917 "r" (m1))
918#define SMUL_TIME 4
919#define sdiv_qrnnd(q, r, nh, nl, d) \
920 __asm__ ("div %0,%2,%4" \
921 : "=r" ((SItype)(q)), "=q" ((SItype)(r)) \
922 : "r" ((SItype)(nh)), "1" ((SItype)(nl)), "r" ((SItype)(d)))
923#define UDIV_TIME 100
924#endif
925 #endif /* Power architecture variants.*/
926
927/* Powerpc 64 bit support taken from gmp-4.1.2. */
928/* We should test _IBMR2 here when we add assembly support for the system
929 vendor compilers. */
930#if 0 /* Not yet enabled becuase we don't have hardware for a test. */
931#if (defined (_ARCH_PPC) || defined (__powerpc__)) && W_TYPE_SIZE == 64
932#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
933 do { \
934 if (__builtin_constant_p (bh) && (bh) == 0) \
935 __asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{aze|addze} %0,%2" \
936 : "=r" (sh), "=&r" (sl) : "r" (ah), "%r" (al), "rI" (bl));\
937 else if (__builtin_constant_p (bh) && (bh) == ~(UDItype) 0) \
938 __asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{ame|addme} %0,%2" \
939 : "=r" (sh), "=&r" (sl) : "r" (ah), "%r" (al), "rI" (bl));\
940 else \
941 __asm__ ("{a%I5|add%I5c} %1,%4,%5\n\t{ae|adde} %0,%2,%3" \
942 : "=r" (sh), "=&r" (sl) \
943 : "%r" (ah), "r" (bh), "%r" (al), "rI" (bl)); \
944 } while (0)
945#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
946 do { \
947 if (__builtin_constant_p (ah) && (ah) == 0) \
948 __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfze|subfze} %0,%2"\
949 : "=r" (sh), "=&r" (sl) : "r" (bh), "rI" (al), "r" (bl));\
950 else if (__builtin_constant_p (ah) && (ah) == ~(UDItype) 0) \
951 __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfme|subfme} %0,%2"\
952 : "=r" (sh), "=&r" (sl) : "r" (bh), "rI" (al), "r" (bl));\
953 else if (__builtin_constant_p (bh) && (bh) == 0) \
954 __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{ame|addme} %0,%2" \
955 : "=r" (sh), "=&r" (sl) : "r" (ah), "rI" (al), "r" (bl));\
956 else if (__builtin_constant_p (bh) && (bh) == ~(UDItype) 0) \
957 __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{aze|addze} %0,%2" \
958 : "=r" (sh), "=&r" (sl) : "r" (ah), "rI" (al), "r" (bl));\
959 else \
960 __asm__ ("{sf%I4|subf%I4c} %1,%5,%4\n\t{sfe|subfe} %0,%3,%2"\
961 : "=r" (sh), "=&r" (sl) \
962 : "r" (ah), "r" (bh), "rI" (al), "r" (bl)); \
963 } while (0)
964#define count_leading_zeros(count, x) \
965 __asm__ ("cntlzd %0,%1" : "=r" (count) : "r" (x))
966#define COUNT_LEADING_ZEROS_0 64
967#define umul_ppmm(ph, pl, m0, m1) \
968 do { \
969 UDItype __m0 = (m0), __m1 = (m1); \
970 __asm__ ("mulhdu %0,%1,%2" : "=r" (ph) : "%r" (m0), "r" (m1));\
971 (pl) = __m0 * __m1; \
972 } while (0)
973#define UMUL_TIME 15
974#define smul_ppmm(ph, pl, m0, m1) \
975 do { \
976 DItype __m0 = (m0), __m1 = (m1); \
977 __asm__ ("mulhd %0,%1,%2" : "=r" (ph) : "%r" (m0), "r" (m1));\
978 (pl) = __m0 * __m1; \
979 } while (0)
980#define SMUL_TIME 14 /* ??? */
981#define UDIV_TIME 120 /* ??? */
982#endif /* 64-bit PowerPC. */
983#endif /* if 0 */
984
985/***************************************
986 ************** PYR ******************
987 ***************************************/
988#if defined (__pyr__) && W_TYPE_SIZE == 32
989#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
990 __asm__ ("addw %5,%1 \n" \
991 "addwc%3,%0" \
992 : "=r" ((USItype)(sh)), \
993 "=&r" ((USItype)(sl)) \
994 : "%0" ((USItype)(ah)), \
995 "g" ((USItype)(bh)), \
996 "%1" ((USItype)(al)), \
997 "g" ((USItype)(bl)))
998#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
999 __asm__ ("subw %5,%1 \n" \
1000 "subwb%3,%0" \
1001 : "=r" ((USItype)(sh)), \
1002 "=&r" ((USItype)(sl)) \
1003 : "0" ((USItype)(ah)), \
1004 "g" ((USItype)(bh)), \
1005 "1" ((USItype)(al)), \
1006 "g" ((USItype)(bl)))
1007/* This insn works on Pyramids with AP, XP, or MI CPUs, but not with SP. */
1008#define umul_ppmm(w1, w0, u, v) \
1009 ({union {UDItype __ll; \
1010 struct {USItype __h, __l;} __i; \
1011 } __xx; \
1012 __asm__ ("movw %1,%R0 \n" \
1013 "uemul %2,%0" \
1014 : "=&r" (__xx.__ll) \
1015 : "g" ((USItype) (u)), \
1016 "g" ((USItype)(v))); \
1017 (w1) = __xx.__i.__h; (w0) = __xx.__i.__l;})
1018#endif /* __pyr__ */
1019
1020
1021/***************************************
1022 ************** RT/ROMP **************
1023 ***************************************/
1024 #if defined (__ibm032__) /* RT/ROMP */&& W_TYPE_SIZE == 32
1025#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
1026 __asm__ ("a %1,%5 \n" \
1027 "ae %0,%3" \
1028 : "=r" ((USItype)(sh)), \
1029 "=&r" ((USItype)(sl)) \
1030 : "%0" ((USItype)(ah)), \
1031 "r" ((USItype)(bh)), \
1032 "%1" ((USItype)(al)), \
1033 "r" ((USItype)(bl)))
1034#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
1035 __asm__ ("s %1,%5\n" \
1036 "se %0,%3" \
1037 : "=r" ((USItype)(sh)), \
1038 "=&r" ((USItype)(sl)) \
1039 : "0" ((USItype)(ah)), \
1040 "r" ((USItype)(bh)), \
1041 "1" ((USItype)(al)), \
1042 "r" ((USItype)(bl)))
1043#define umul_ppmm(ph, pl, m0, m1) \
1044 do { \
1045 USItype __m0 = (m0), __m1 = (m1); \
1046 __asm__ ( \
1047 "s r2,r2 \n" \
1048 "mtsr10,%2 \n" \
1049 "mr2,%3 \n" \
1050 "mr2,%3 \n" \
1051 "mr2,%3 \n" \
1052 "mr2,%3 \n" \
1053 "mr2,%3 \n" \
1054 "mr2,%3 \n" \
1055 "mr2,%3 \n" \
1056 "mr2,%3 \n" \
1057 "mr2,%3 \n" \
1058 "mr2,%3 \n" \
1059 "mr2,%3 \n" \
1060 "mr2,%3 \n" \
1061 "mr2,%3 \n" \
1062 "mr2,%3 \n" \
1063 "mr2,%3 \n" \
1064 "mr2,%3 \n" \
1065 "cas%0,r2,r0 \n" \
1066 "mfsr10,%1" \
1067 : "=r" ((USItype)(ph)), \
1068 "=r" ((USItype)(pl)) \
1069 : "%r" (__m0), \
1070 "r" (__m1) \
1071 : "r2"); \
1072 (ph) += ((((SItype) __m0 >> 31) & __m1) \
1073 + (((SItype) __m1 >> 31) & __m0)); \
1074 } while (0)
1075#define UMUL_TIME 20
1076#define UDIV_TIME 200
1077#define count_leading_zeros(count, x) \
1078 do { \
1079 if ((x) >= 0x10000) \
1080 __asm__ ("clz %0,%1" \
1081 : "=r" ((USItype)(count)) \
1082 : "r" ((USItype)(x) >> 16)); \
1083 else \
1084 { \
1085 __asm__ ("clz %0,%1" \
1086 : "=r" ((USItype)(count)) \
1087 : "r" ((USItype)(x))); \
1088 (count) += 16; \
1089 } \
1090 } while (0)
1091#endif /* RT/ROMP */
1092
1093
1094/***************************************
1095 ************** SH2 ******************
1096 ***************************************/
1097#if (defined (__sh2__) || defined(__sh3__) || defined(__SH4__) ) \
1098 && W_TYPE_SIZE == 32
1099#define umul_ppmm(w1, w0, u, v) \
1100 __asm__ ( \
1101 "dmulu.l %2,%3\n" \
1102 "stsmacl,%1\n" \
1103 "stsmach,%0" \
1104 : "=r" ((USItype)(w1)), \
1105 "=r" ((USItype)(w0)) \
1106 : "r" ((USItype)(u)), \
1107 "r" ((USItype)(v)) \
1108 : "macl", "mach")
1109#define UMUL_TIME 5
1110#endif
1111
1112/***************************************
1113 ************** SPARC****************
1114 ***************************************/
1115#if defined (__sparc__) && W_TYPE_SIZE == 32
1116#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
1117 __asm__ ("addcc %r4,%5,%1\n" \
1118 "addx %r2,%3,%0" \
1119 : "=r" ((USItype)(sh)), \
1120 "=&r" ((USItype)(sl)) \
1121 : "%rJ" ((USItype)(ah)), \
1122 "rI" ((USItype)(bh)), \
1123 "%rJ" ((USItype)(al)), \
1124 "rI" ((USItype)(bl)) \
1125 __CLOBBER_CC)
1126#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
1127 __asm__ ("subcc %r4,%5,%1\n" \
1128 "subx %r2,%3,%0" \
1129 : "=r" ((USItype)(sh)), \
1130 "=&r" ((USItype)(sl)) \
1131 : "rJ" ((USItype)(ah)), \
1132 "rI" ((USItype)(bh)), \
1133 "rJ" ((USItype)(al)), \
1134 "rI" ((USItype)(bl)) \
1135 __CLOBBER_CC)
1136#if defined (__sparc_v8__)
1137/* Don't match immediate range because, 1) it is not often useful,
1138 2) the 'I' flag thinks of the range as a 13 bit signed interval,
1139 while we want to match a 13 bit interval, sign extended to 32 bits,
1140 but INTERPRETED AS UNSIGNED. */
1141#define umul_ppmm(w1, w0, u, v) \
1142 __asm__ ("umul %2,%3,%1;rd %%y,%0" \
1143 : "=r" ((USItype)(w1)), \
1144 "=r" ((USItype)(w0)) \
1145 : "r" ((USItype)(u)), \
1146 "r" ((USItype)(v)))
1147#define UMUL_TIME 5
1148 #ifndef SUPERSPARC/* SuperSPARC's udiv only handles 53 bit dividends */
1149#define udiv_qrnnd(q, r, n1, n0, d) \
1150 do { \
1151 USItype __q; \
1152 __asm__ ("mov %1,%%y;nop;nop;nop;udiv %2,%3,%0" \
1153 : "=r" ((USItype)(__q)) \
1154 : "r" ((USItype)(n1)), \
1155 "r" ((USItype)(n0)), \
1156 "r" ((USItype)(d))); \
1157 (r) = (n0) - __q * (d); \
1158 (q) = __q; \
1159 } while (0)
1160#define UDIV_TIME 25
1161#endif /* SUPERSPARC */
1162#else /* ! __sparc_v8__ */
1163#if defined (__sparclite__)
1164/* This has hardware multiply but not divide. It also has two additional
1165 instructions scan (ffs from high bit) and divscc. */
1166#define umul_ppmm(w1, w0, u, v) \
1167 __asm__ ("umul %2,%3,%1;rd %%y,%0" \
1168 : "=r" ((USItype)(w1)), \
1169 "=r" ((USItype)(w0)) \
1170 : "r" ((USItype)(u)), \
1171 "r" ((USItype)(v)))
1172#define UMUL_TIME 5
1173#define udiv_qrnnd(q, r, n1, n0, d) \
1174 __asm__ ("! Inlined udiv_qrnnd \n" \
1175 " wr %%g0,%2,%%y! Not a delayed write for sparclite \n" \
1176 " tst%%g0 \n" \
1177 " divscc%3,%4,%%g1 \n" \
1178 " divscc%%g1,%4,%%g1 \n" \
1179 " divscc%%g1,%4,%%g1 \n" \
1180 " divscc%%g1,%4,%%g1 \n" \
1181 " divscc%%g1,%4,%%g1 \n" \
1182 " divscc%%g1,%4,%%g1 \n" \
1183 " divscc%%g1,%4,%%g1 \n" \
1184 " divscc%%g1,%4,%%g1 \n" \
1185 " divscc%%g1,%4,%%g1 \n" \
1186 " divscc%%g1,%4,%%g1 \n" \
1187 " divscc%%g1,%4,%%g1 \n" \
1188 " divscc%%g1,%4,%%g1 \n" \
1189 " divscc%%g1,%4,%%g1 \n" \
1190 " divscc%%g1,%4,%%g1 \n" \
1191 " divscc%%g1,%4,%%g1 \n" \
1192 " divscc%%g1,%4,%%g1 \n" \
1193 " divscc%%g1,%4,%%g1 \n" \
1194 " divscc%%g1,%4,%%g1 \n" \
1195 " divscc%%g1,%4,%%g1 \n" \
1196 " divscc%%g1,%4,%%g1 \n" \
1197 " divscc%%g1,%4,%%g1 \n" \
1198 " divscc%%g1,%4,%%g1 \n" \
1199 " divscc%%g1,%4,%%g1 \n" \
1200 " divscc%%g1,%4,%%g1 \n" \
1201 " divscc%%g1,%4,%%g1 \n" \
1202 " divscc%%g1,%4,%%g1 \n" \
1203 " divscc%%g1,%4,%%g1 \n" \
1204 " divscc%%g1,%4,%%g1 \n" \
1205 " divscc%%g1,%4,%%g1 \n" \
1206 " divscc%%g1,%4,%%g1 \n" \
1207 " divscc%%g1,%4,%%g1 \n" \
1208 " divscc%%g1,%4,%0 \n" \
1209 " rd%%y,%1 \n" \
1210 " bl,a 1f \n" \
1211 " add%1,%4,%1 \n" \
1212 "1:! End of inline udiv_qrnnd" \
1213 : "=r" ((USItype)(q)), \
1214 "=r" ((USItype)(r)) \
1215 : "r" ((USItype)(n1)), \
1216 "r" ((USItype)(n0)), \
1217 "rI" ((USItype)(d)) \
1218 : "%g1" __AND_CLOBBER_CC)
1219#define UDIV_TIME 37
1220#define count_leading_zeros(count, x) \
1221 __asm__ ("scan %1,0,%0" \
1222 : "=r" ((USItype)(x)) \
1223 : "r" ((USItype)(count)))
1224/* Early sparclites return 63 for an argument of 0, but they warn that future
1225 implementations might change this. Therefore, leave COUNT_LEADING_ZEROS_0
1226 undefined. */
1227#endif /* __sparclite__ */
1228#endif /* __sparc_v8__ */
1229/* Default to sparc v7 versions of umul_ppmm and udiv_qrnnd. */
1230#ifndef umul_ppmm
1231#define umul_ppmm(w1, w0, u, v) \
1232 __asm__ ("! Inlined umul_ppmm \n" \
1233 " wr %%g0,%2,%%y! SPARC has 0-3 delay insn after a wr \n" \
1234 " sra %3,31,%%g2! Don't move this insn \n" \
1235 " and %2,%%g2,%%g2! Don't move this insn \n" \
1236 " andcc %%g0,0,%%g1! Don't move this insn \n" \
1237 " mulscc%%g1,%3,%%g1 \n" \
1238 " mulscc%%g1,%3,%%g1 \n" \
1239 " mulscc%%g1,%3,%%g1 \n" \
1240 " mulscc%%g1,%3,%%g1 \n" \
1241 " mulscc%%g1,%3,%%g1 \n" \
1242 " mulscc%%g1,%3,%%g1 \n" \
1243 " mulscc%%g1,%3,%%g1 \n" \
1244 " mulscc%%g1,%3,%%g1 \n" \
1245 " mulscc%%g1,%3,%%g1 \n" \
1246 " mulscc%%g1,%3,%%g1 \n" \
1247 " mulscc%%g1,%3,%%g1 \n" \
1248 " mulscc%%g1,%3,%%g1 \n" \
1249 " mulscc%%g1,%3,%%g1 \n" \
1250 " mulscc%%g1,%3,%%g1 \n" \
1251 " mulscc%%g1,%3,%%g1 \n" \
1252 " mulscc%%g1,%3,%%g1 \n" \
1253 " mulscc%%g1,%3,%%g1 \n" \
1254 " mulscc%%g1,%3,%%g1 \n" \
1255 " mulscc%%g1,%3,%%g1 \n" \
1256 " mulscc%%g1,%3,%%g1 \n" \
1257 " mulscc%%g1,%3,%%g1 \n" \
1258 " mulscc%%g1,%3,%%g1 \n" \
1259 " mulscc%%g1,%3,%%g1 \n" \
1260 " mulscc%%g1,%3,%%g1 \n" \
1261 " mulscc%%g1,%3,%%g1 \n" \
1262 " mulscc%%g1,%3,%%g1 \n" \
1263 " mulscc%%g1,%3,%%g1 \n" \
1264 " mulscc%%g1,%3,%%g1 \n" \
1265 " mulscc%%g1,%3,%%g1 \n" \
1266 " mulscc%%g1,%3,%%g1 \n" \
1267 " mulscc%%g1,%3,%%g1 \n" \
1268 " mulscc%%g1,%3,%%g1 \n" \
1269 " mulscc%%g1,0,%%g1 \n" \
1270 " add%%g1,%%g2,%0 \n" \
1271 " rd%%y,%1" \
1272 : "=r" ((USItype)(w1)), \
1273 "=r" ((USItype)(w0)) \
1274 : "%rI" ((USItype)(u)), \
1275 "r" ((USItype)(v)) \
1276 : "%g1", "%g2" __AND_CLOBBER_CC)
1277 #define UMUL_TIME 39 /* 39 instructions */
1278#endif
1279#ifndef udiv_qrnnd
1280#ifndef LONGLONG_STANDALONE
1281#define udiv_qrnnd(q, r, n1, n0, d) \
1282 do { USItype __r; \
1283 (q) = __udiv_qrnnd (&__r, (n1), (n0), (d)); \
1284 (r) = __r; \
1285 } while (0)
1286extern USItype __udiv_qrnnd ();
1287#define UDIV_TIME 140
1288#endif /* LONGLONG_STANDALONE */
1289#endif /* udiv_qrnnd */
1290#endif /* __sparc__ */
1291
1292
1293/***************************************
1294 ************** VAX ******************
1295 ***************************************/
1296#if defined (__vax__) && W_TYPE_SIZE == 32
1297#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
1298 __asm__ ("addl2 %5,%1\n" \
1299 "adwc %3,%0" \
1300 : "=g" ((USItype)(sh)), \
1301 "=&g" ((USItype)(sl)) \
1302 : "%0" ((USItype)(ah)), \
1303 "g" ((USItype)(bh)), \
1304 "%1" ((USItype)(al)), \
1305 "g" ((USItype)(bl)))
1306#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
1307 __asm__ ("subl2 %5,%1\n" \
1308 "sbwc %3,%0" \
1309 : "=g" ((USItype)(sh)), \
1310 "=&g" ((USItype)(sl)) \
1311 : "0" ((USItype)(ah)), \
1312 "g" ((USItype)(bh)), \
1313 "1" ((USItype)(al)), \
1314 "g" ((USItype)(bl)))
1315#define umul_ppmm(xh, xl, m0, m1) \
1316 do { \
1317 union {UDItype __ll; \
1318 struct {USItype __l, __h;} __i; \
1319 } __xx; \
1320 USItype __m0 = (m0), __m1 = (m1); \
1321 __asm__ ("emul %1,%2,$0,%0" \
1322 : "=g" (__xx.__ll) \
1323 : "g" (__m0), \
1324 "g" (__m1)); \
1325 (xh) = __xx.__i.__h; (xl) = __xx.__i.__l; \
1326 (xh) += ((((SItype) __m0 >> 31) & __m1) \
1327 + (((SItype) __m1 >> 31) & __m0)); \
1328 } while (0)
1329#define sdiv_qrnnd(q, r, n1, n0, d) \
1330 do { \
1331 union {DItype __ll; \
1332 struct {SItype __l, __h;} __i; \
1333 } __xx; \
1334 __xx.__i.__h = n1; __xx.__i.__l = n0; \
1335 __asm__ ("ediv %3,%2,%0,%1" \
1336 : "=g" (q), "=g" (r) \
1337 : "g" (__xx.__ll), "g" (d)); \
1338 } while (0)
1339#endif /* __vax__ */
1340
1341
1342/***************************************
1343 ************** Z8000****************
1344 ***************************************/
1345#if defined (__z8000__) && W_TYPE_SIZE == 16
1346#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
1347 __asm__ ("add %H1,%H5\n\tadc %H0,%H3" \
1348 : "=r" ((unsigned int)(sh)), \
1349 "=&r" ((unsigned int)(sl)) \
1350 : "%0" ((unsigned int)(ah)), \
1351 "r" ((unsigned int)(bh)), \
1352 "%1" ((unsigned int)(al)), \
1353 "rQR" ((unsigned int)(bl)))
1354#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
1355 __asm__ ("sub %H1,%H5\n\tsbc %H0,%H3" \
1356 : "=r" ((unsigned int)(sh)), \
1357 "=&r" ((unsigned int)(sl)) \
1358 : "0" ((unsigned int)(ah)), \
1359 "r" ((unsigned int)(bh)), \
1360 "1" ((unsigned int)(al)), \
1361 "rQR" ((unsigned int)(bl)))
1362#define umul_ppmm(xh, xl, m0, m1) \
1363 do { \
1364 union {long int __ll; \
1365 struct {unsigned int __h, __l;} __i; \
1366 } __xx; \
1367 unsigned int __m0 = (m0), __m1 = (m1); \
1368 __asm__ ("mult %S0,%H3" \
1369 : "=r" (__xx.__i.__h), \
1370 "=r" (__xx.__i.__l) \
1371 : "%1" (__m0), \
1372 "rQR" (__m1)); \
1373 (xh) = __xx.__i.__h; (xl) = __xx.__i.__l; \
1374 (xh) += ((((signed int) __m0 >> 15) & __m1) \
1375 + (((signed int) __m1 >> 15) & __m0)); \
1376 } while (0)
1377#endif /* __z8000__ */
1378
1379#endif /* __GNUC__ */
1380#endif /* !__riscos__ */
1381
1382
1383/***************************************
1384 *********** Generic Versions********
1385 ***************************************/
1386#if !defined (umul_ppmm) && defined (__umulsidi3)
1387#define umul_ppmm(ph, pl, m0, m1) \
1388 { \
1389 UDWtype __ll = __umulsidi3 (m0, m1); \
1390 ph = (UWtype) (__ll >> W_TYPE_SIZE); \
1391 pl = (UWtype) __ll; \
1392 }
1393#endif
1394
1395#if !defined (__umulsidi3)
1396#define __umulsidi3(u, v) \
1397 ({UWtype __hi, __lo; \
1398 umul_ppmm (__hi, __lo, u, v); \
1399 ((UDWtype) __hi << W_TYPE_SIZE) | __lo; })
1400#endif
1401
1402/* If this machine has no inline assembler, use C macros. */
1403
1404#if !defined (add_ssaaaa)
1405#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
1406 do { \
1407 UWtype __x; \
1408 __x = (al) + (bl); \
1409 (sh) = (ah) + (bh) + (__x < (al)); \
1410 (sl) = __x; \
1411 } while (0)
1412#endif
1413
1414#if !defined (sub_ddmmss)
1415#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
1416 do { \
1417 UWtype __x; \
1418 __x = (al) - (bl); \
1419 (sh) = (ah) - (bh) - (__x > (al)); \
1420 (sl) = __x; \
1421 } while (0)
1422#endif
1423
1424#if !defined (umul_ppmm)
1425 #define umul_ppmm(w1, w0, u, v) \
1426 do { \
1427 UWtype __x0, __x1, __x2, __x3; \
1428 UHWtype __ul, __vl, __uh, __vh; \
1429 UWtype __u = (u), __v = (v); \
1430 \
1431 __ul = __ll_lowpart (__u); \
1432 __uh = __ll_highpart (__u); \
1433 __vl = __ll_lowpart (__v); \
1434 __vh = __ll_highpart (__v); \
1435 \
1436 __x0 = (UWtype) __ul * __vl; \
1437 __x1 = (UWtype) __ul * __vh; \
1438 __x2 = (UWtype) __uh * __vl; \
1439 __x3 = (UWtype) __uh * __vh; \
1440 \
1441 __x1 += __ll_highpart (__x0);/* this can't give carry */ \
1442 __x1 += __x2; /* but this indeed can */ \
1443 if (__x1 < __x2) /* did we get it? */ \
1444 __x3 += __ll_B; /* yes, add it in the proper pos. */\
1445 \
1446 (w1) = __x3 + __ll_highpart (__x1); \
1447 (w0) = (__ll_lowpart (__x1) << W_TYPE_SIZE/2) + __ll_lowpart (__x0);\
1448 } while (0)
1449#endif
1450
1451#if !defined (umul_ppmm)
1452 #define smul_ppmm(w1, w0, u, v) \
1453 do { \
1454 UWtype __w1; \
1455 UWtype __m0 = (u), __m1 = (v); \
1456 umul_ppmm (__w1, w0, __m0, __m1); \
1457 (w1) = __w1 - (-(__m0 >> (W_TYPE_SIZE - 1)) & __m1) \
1458 - (-(__m1 >> (W_TYPE_SIZE - 1)) & __m0); \
1459 } while (0)
1460#endif
1461
1462/* Define this unconditionally, so it can be used for debugging. */
1463#define __udiv_qrnnd_c(q, r, n1, n0, d) \
1464 do { \
1465 UWtype __d1, __d0, __q1, __q0, __r1, __r0, __m; \
1466 __d1 = __ll_highpart (d); \
1467 __d0 = __ll_lowpart (d); \
1468 \
1469 __r1 = (n1) % __d1; \
1470 __q1 = (n1) / __d1; \
1471 __m = (UWtype) __q1 * __d0; \
1472 __r1 = __r1 * __ll_B | __ll_highpart (n0); \
1473 if (__r1 < __m) \
1474 { \
1475 __q1--, __r1 += (d); \
1476 if (__r1 >= (d)) /* i.e. we didn't get carry when adding to __r1 */\
1477 if (__r1 < __m) \
1478 __q1--, __r1 += (d); \
1479 } \
1480 __r1 -= __m; \
1481 \
1482 __r0 = __r1 % __d1; \
1483 __q0 = __r1 / __d1; \
1484 __m = (UWtype) __q0 * __d0; \
1485 __r0 = __r0 * __ll_B | __ll_lowpart (n0); \
1486 if (__r0 < __m) \
1487 { \
1488 __q0--, __r0 += (d); \
1489 if (__r0 >= (d)) \
1490 if (__r0 < __m) \
1491 __q0--, __r0 += (d); \
1492 } \
1493 __r0 -= __m; \
1494 \
1495 (q) = (UWtype) __q1 * __ll_B | __q0; \
1496 (r) = __r0; \
1497 } while (0)
1498
1499/* If the processor has no udiv_qrnnd but sdiv_qrnnd, go through
1500 __udiv_w_sdiv (defined in libgcc or elsewhere). */
1501#if !defined (udiv_qrnnd) && defined (sdiv_qrnnd)
1502#define udiv_qrnnd(q, r, nh, nl, d) \
1503 do { \
1504 UWtype __r; \
1505 (q) = __MPN(udiv_w_sdiv) (&__r, nh, nl, d); \
1506 (r) = __r; \
1507 } while (0)
1508#endif
1509
1510/* If udiv_qrnnd was not defined for this processor, use __udiv_qrnnd_c. */
1511#if !defined (udiv_qrnnd)
1512#define UDIV_NEEDS_NORMALIZATION 1
1513#define udiv_qrnnd __udiv_qrnnd_c
1514#endif
1515
1516#if !defined (count_leading_zeros)
1517extern
1518#ifdef __STDC__
1519const
1520#endif
1521unsigned char _gcry_clz_tab[];
1522#define MPI_INTERNAL_NEED_CLZ_TAB 1
1523#define count_leading_zeros(count, x) \
1524 do { \
1525 UWtype __xr = (x); \
1526 UWtype __a; \
1527 \
1528 if (W_TYPE_SIZE <= 32) \
1529 { \
1530 __a = __xr < ((UWtype) 1 << 2*__BITS4) \
1531 ? (__xr < ((UWtype) 1 << __BITS4) ? 0 : __BITS4) \
1532 : (__xr < ((UWtype) 1 << 3*__BITS4) ? 2*__BITS4 : 3*__BITS4);\
1533 } \
1534 else \
1535 { \
1536 for (__a = W_TYPE_SIZE - 8; __a > 0; __a -= 8) \
1537 if (((__xr >> __a) & 0xff) != 0) \
1538 break; \
1539 } \
1540 \
1541 (count) = W_TYPE_SIZE - (_gcry_clz_tab[__xr >> __a] + __a); \
1542 } while (0)
1543/* This version gives a well-defined value for zero. */
1544#define COUNT_LEADING_ZEROS_0 W_TYPE_SIZE
1545#endif
1546
1547#if !defined (count_trailing_zeros)
1548/* Define count_trailing_zeros using count_leading_zeros. The latter might be
1549 defined in asm, but if it is not, the C version above is good enough. */
1550#define count_trailing_zeros(count, x) \
1551 do { \
1552 UWtype __ctz_x = (x); \
1553 UWtype __ctz_c; \
1554 count_leading_zeros (__ctz_c, __ctz_x & -__ctz_x); \
1555 (count) = W_TYPE_SIZE - 1 - __ctz_c; \
1556 } while (0)
1557#endif
1558
1559#ifndef UDIV_NEEDS_NORMALIZATION
1560#define UDIV_NEEDS_NORMALIZATION 0
1561#endif
diff --git a/pwmanager/libcrypt/mpi/mpi-add.c b/pwmanager/libcrypt/mpi/mpi-add.c
new file mode 100644
index 0000000..ada257a
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/mpi-add.c
@@ -0,0 +1,236 @@
1/* mpi-add.c - MPI functions
2 * Copyright (C) 1994, 1996, 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 *
20 * Note: This code is heavily based on the GNU MP Library.
21 * Actually it's the same code with only minor changes in the
22 * way the data is stored; this is to support the abstraction
23 * of an optional secure memory allocation which may be used
24 * to avoid revealing of sensitive data due to paging etc.
25 */
26
27#include <config.h>
28#include <stdio.h>
29#include <stdlib.h>
30
31#include "mpi-internal.h"
32
33
34/****************
35 * Add the unsigned integer V to the mpi-integer U and store the
36 * result in W. U and V may be the same.
37 */
38void
39gcry_mpi_add_ui(gcry_mpi_t w, gcry_mpi_t u, unsigned long v )
40{
41 mpi_ptr_t wp, up;
42 mpi_size_t usize, wsize;
43 int usign, wsign;
44
45 usize = u->nlimbs;
46 usign = u->sign;
47 wsign = 0;
48
49 /* If not space for W (and possible carry), increase space. */
50 wsize = usize + 1;
51 if( w->alloced < wsize )
52 mpi_resize(w, wsize);
53
54 /* These must be after realloc (U may be the same as W). */
55 up = u->d;
56 wp = w->d;
57
58 if( !usize ) { /* simple */
59 wp[0] = v;
60 wsize = v? 1:0;
61 }
62 else if( !usign ) { /* mpi is not negative */
63 mpi_limb_t cy;
64 cy = _gcry_mpih_add_1(wp, up, usize, v);
65 wp[usize] = cy;
66 wsize = usize + cy;
67 }
68 else { /* The signs are different. Need exact comparison to determine
69 * which operand to subtract from which. */
70 if( usize == 1 && up[0] < v ) {
71 wp[0] = v - up[0];
72 wsize = 1;
73 }
74 else {
75 _gcry_mpih_sub_1(wp, up, usize, v);
76 /* Size can decrease with at most one limb. */
77 wsize = usize - (wp[usize-1]==0);
78 wsign = 1;
79 }
80 }
81
82 w->nlimbs = wsize;
83 w->sign = wsign;
84}
85
86
87void
88gcry_mpi_add(gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v)
89{
90 mpi_ptr_t wp, up, vp;
91 mpi_size_t usize, vsize, wsize;
92 int usign, vsign, wsign;
93
94 if( u->nlimbs < v->nlimbs ) { /* Swap U and V. */
95 usize = v->nlimbs;
96 usign = v->sign;
97 vsize = u->nlimbs;
98 vsign = u->sign;
99 wsize = usize + 1;
100 RESIZE_IF_NEEDED(w, wsize);
101 /* These must be after realloc (u or v may be the same as w). */
102 up = v->d;
103 vp = u->d;
104 }
105 else {
106 usize = u->nlimbs;
107 usign = u->sign;
108 vsize = v->nlimbs;
109 vsign = v->sign;
110 wsize = usize + 1;
111 RESIZE_IF_NEEDED(w, wsize);
112 /* These must be after realloc (u or v may be the same as w). */
113 up = u->d;
114 vp = v->d;
115 }
116 wp = w->d;
117 wsign = 0;
118
119 if( !vsize ) { /* simple */
120 MPN_COPY(wp, up, usize );
121 wsize = usize;
122 wsign = usign;
123 }
124 else if( usign != vsign ) { /* different sign */
125 /* This test is right since USIZE >= VSIZE */
126 if( usize != vsize ) {
127 _gcry_mpih_sub(wp, up, usize, vp, vsize);
128 wsize = usize;
129 MPN_NORMALIZE(wp, wsize);
130 wsign = usign;
131 }
132 else if( _gcry_mpih_cmp(up, vp, usize) < 0 ) {
133 _gcry_mpih_sub_n(wp, vp, up, usize);
134 wsize = usize;
135 MPN_NORMALIZE(wp, wsize);
136 if( !usign )
137 wsign = 1;
138 }
139 else {
140 _gcry_mpih_sub_n(wp, up, vp, usize);
141 wsize = usize;
142 MPN_NORMALIZE(wp, wsize);
143 if( usign )
144 wsign = 1;
145 }
146 }
147 else { /* U and V have same sign. Add them. */
148 mpi_limb_t cy = _gcry_mpih_add(wp, up, usize, vp, vsize);
149 wp[usize] = cy;
150 wsize = usize + cy;
151 if( usign )
152 wsign = 1;
153 }
154
155 w->nlimbs = wsize;
156 w->sign = wsign;
157}
158
159
160/****************
161 * Subtract the unsigned integer V from the mpi-integer U and store the
162 * result in W.
163 */
164void
165gcry_mpi_sub_ui(gcry_mpi_t w, gcry_mpi_t u, unsigned long v )
166{
167 mpi_ptr_t wp, up;
168 mpi_size_t usize, wsize;
169 int usign, wsign;
170
171 usize = u->nlimbs;
172 usign = u->sign;
173 wsign = 0;
174
175 /* If not space for W (and possible carry), increase space. */
176 wsize = usize + 1;
177 if( w->alloced < wsize )
178 mpi_resize(w, wsize);
179
180 /* These must be after realloc (U may be the same as W). */
181 up = u->d;
182 wp = w->d;
183
184 if( !usize ) { /* simple */
185 wp[0] = v;
186 wsize = v? 1:0;
187 wsign = 1;
188 }
189 else if( usign ) {/* mpi and v are negative */
190 mpi_limb_t cy;
191 cy = _gcry_mpih_add_1(wp, up, usize, v);
192 wp[usize] = cy;
193 wsize = usize + cy;
194 }
195 else { /* The signs are different. Need exact comparison to determine
196 * which operand to subtract from which. */
197 if( usize == 1 && up[0] < v ) {
198 wp[0] = v - up[0];
199 wsize = 1;
200 wsign = 1;
201 }
202 else {
203 _gcry_mpih_sub_1(wp, up, usize, v);
204 /* Size can decrease with at most one limb. */
205 wsize = usize - (wp[usize-1]==0);
206 }
207 }
208
209 w->nlimbs = wsize;
210 w->sign = wsign;
211}
212
213void
214gcry_mpi_sub(gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v)
215{
216 gcry_mpi_t vv = mpi_copy (v);
217 vv->sign = ! vv->sign;
218 gcry_mpi_add (w, u, vv);
219 mpi_free (vv);
220}
221
222
223void
224gcry_mpi_addm( gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m)
225{
226 gcry_mpi_add(w, u, v);
227 _gcry_mpi_fdiv_r( w, w, m );
228}
229
230void
231gcry_mpi_subm( gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m)
232{
233 gcry_mpi_sub(w, u, v);
234 _gcry_mpi_fdiv_r( w, w, m );
235}
236
diff --git a/pwmanager/libcrypt/mpi/mpi-bit.c b/pwmanager/libcrypt/mpi/mpi-bit.c
new file mode 100644
index 0000000..26853ef
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/mpi-bit.c
@@ -0,0 +1,257 @@
1/* mpi-bit.c - MPI bit level fucntions
2 * Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <config.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <assert.h>
25#include "mpi-internal.h"
26#include "longlong.h"
27
28
29#ifdef MPI_INTERNAL_NEED_CLZ_TAB
30#ifdef __STDC__
31const
32#endif
33unsigned char
34_gcry_clz_tab[] =
35{
36 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
37 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
38 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
39 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
40 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
41 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
42 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
43 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
44};
45#endif
46
47
48#define A_LIMB_1 ((mpi_limb_t)1)
49
50
51/****************
52 * Sometimes we have MSL (most significant limbs) which are 0;
53 * this is for some reasons not good, so this function removes them.
54 */
55void
56_gcry_mpi_normalize( gcry_mpi_t a )
57{
58 if( mpi_is_opaque(a) )
59 return;
60
61 for( ; a->nlimbs && !a->d[a->nlimbs-1]; a->nlimbs-- )
62 ;
63}
64
65
66
67/****************
68 * Return the number of bits in A.
69 */
70unsigned int
71gcry_mpi_get_nbits( gcry_mpi_t a )
72{
73 unsigned n;
74
75 if( mpi_is_opaque(a) ) {
76 return a->sign; /* which holds the number of bits */
77 }
78
79 _gcry_mpi_normalize( a );
80 if( a->nlimbs ) {
81 mpi_limb_t alimb = a->d[a->nlimbs-1];
82 if( alimb )
83 count_leading_zeros( n, alimb );
84 else
85 n = BITS_PER_MPI_LIMB;
86 n = BITS_PER_MPI_LIMB - n + (a->nlimbs-1) * BITS_PER_MPI_LIMB;
87 }
88 else
89 n = 0;
90 return n;
91}
92
93
94/****************
95 * Test whether bit N is set.
96 */
97int
98gcry_mpi_test_bit( gcry_mpi_t a, unsigned int n )
99{
100 unsigned int limbno, bitno;
101 mpi_limb_t limb;
102
103 limbno = n / BITS_PER_MPI_LIMB;
104 bitno = n % BITS_PER_MPI_LIMB;
105
106 if( limbno >= a->nlimbs )
107 return 0; /* too far left: this is a 0 */
108 limb = a->d[limbno];
109 return (limb & (A_LIMB_1 << bitno))? 1: 0;
110}
111
112
113/****************
114 * Set bit N of A.
115 */
116void
117gcry_mpi_set_bit( gcry_mpi_t a, unsigned int n )
118{
119 unsigned int limbno, bitno;
120
121 limbno = n / BITS_PER_MPI_LIMB;
122 bitno = n % BITS_PER_MPI_LIMB;
123
124 if( limbno >= a->nlimbs ) { /* resize */
125 if( a->alloced >= limbno )
126 mpi_resize(a, limbno+1 );
127 a->nlimbs = limbno+1;
128 }
129 a->d[limbno] |= (A_LIMB_1<<bitno);
130}
131
132/****************
133 * Set bit N of A. and clear all bits above
134 */
135void
136gcry_mpi_set_highbit( gcry_mpi_t a, unsigned int n )
137{
138 unsigned int limbno, bitno;
139
140 limbno = n / BITS_PER_MPI_LIMB;
141 bitno = n % BITS_PER_MPI_LIMB;
142
143 if( limbno >= a->nlimbs ) { /* resize */
144 if( a->alloced >= limbno )
145 mpi_resize(a, limbno+1 );
146 a->nlimbs = limbno+1;
147 }
148 a->d[limbno] |= (A_LIMB_1<<bitno);
149 for( bitno++; bitno < BITS_PER_MPI_LIMB; bitno++ )
150 a->d[limbno] &= ~(A_LIMB_1 << bitno);
151 a->nlimbs = limbno+1;
152}
153
154/****************
155 * clear bit N of A and all bits above
156 */
157void
158gcry_mpi_clear_highbit( gcry_mpi_t a, unsigned int n )
159{
160 unsigned int limbno, bitno;
161
162 limbno = n / BITS_PER_MPI_LIMB;
163 bitno = n % BITS_PER_MPI_LIMB;
164
165 if( limbno >= a->nlimbs )
166 return; /* not allocated, so need to clear bits :-) */
167
168 for( ; bitno < BITS_PER_MPI_LIMB; bitno++ )
169 a->d[limbno] &= ~(A_LIMB_1 << bitno);
170 a->nlimbs = limbno+1;
171}
172
173/****************
174 * Clear bit N of A.
175 */
176void
177gcry_mpi_clear_bit( gcry_mpi_t a, unsigned int n )
178{
179 unsigned int limbno, bitno;
180
181 limbno = n / BITS_PER_MPI_LIMB;
182 bitno = n % BITS_PER_MPI_LIMB;
183
184 if( limbno >= a->nlimbs )
185 return; /* don't need to clear this bit, it's to far to left */
186 a->d[limbno] &= ~(A_LIMB_1 << bitno);
187}
188
189
190/****************
191 * Shift A by N bits to the right
192 * FIXME: should use alloc_limb if X and A are same.
193 */
194void
195gcry_mpi_rshift( gcry_mpi_t x, gcry_mpi_t a, unsigned n )
196{
197 mpi_ptr_t xp;
198 mpi_size_t xsize;
199
200 xsize = a->nlimbs;
201 x->sign = a->sign;
202 RESIZE_IF_NEEDED(x, xsize);
203 xp = x->d;
204
205 if( xsize ) {
206 _gcry_mpih_rshift( xp, a->d, xsize, n);
207 MPN_NORMALIZE( xp, xsize);
208 }
209 x->nlimbs = xsize;
210}
211
212
213/****************
214 * Shift A by COUNT limbs to the left
215 * This is used only within the MPI library
216 */
217void
218_gcry_mpi_lshift_limbs( gcry_mpi_t a, unsigned int count )
219{
220 mpi_ptr_t ap = a->d;
221 int n = a->nlimbs;
222 int i;
223
224 if( !count || !n )
225 return;
226
227 RESIZE_IF_NEEDED( a, n+count );
228
229 for( i = n-1; i >= 0; i-- )
230 ap[i+count] = ap[i];
231 for(i=0; i < count; i++ )
232 ap[i] = 0;
233 a->nlimbs += count;
234}
235
236
237/****************
238 * Shift A by COUNT limbs to the right
239 * This is used only within the MPI library
240 */
241void
242_gcry_mpi_rshift_limbs( gcry_mpi_t a, unsigned int count )
243{
244 mpi_ptr_t ap = a->d;
245 mpi_size_t n = a->nlimbs;
246 unsigned int i;
247
248 if( count >= n ) {
249 a->nlimbs = 0;
250 return;
251 }
252
253 for( i = 0; i < n - count; i++ )
254 ap[i] = ap[i+count];
255 ap[i] = 0;
256 a->nlimbs -= count;
257}
diff --git a/pwmanager/libcrypt/mpi/mpi-cmp.c b/pwmanager/libcrypt/mpi/mpi-cmp.c
new file mode 100644
index 0000000..6036255
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/mpi-cmp.c
@@ -0,0 +1,74 @@
1/* mpi-cmp.c - MPI functions
2 * Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <config.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include "mpi-internal.h"
25
26int
27gcry_mpi_cmp_ui( gcry_mpi_t u, unsigned long v )
28{
29 mpi_limb_t limb = v;
30
31 _gcry_mpi_normalize( u );
32 if( !u->nlimbs && !limb )
33 return 0;
34 if( u->sign )
35 return -1;
36 if( u->nlimbs > 1 )
37 return 1;
38
39 if( u->d[0] == limb )
40 return 0;
41 else if( u->d[0] > limb )
42 return 1;
43 else
44 return -1;
45}
46
47int
48gcry_mpi_cmp( gcry_mpi_t u, gcry_mpi_t v )
49{
50 mpi_size_t usize, vsize;
51 int cmp;
52
53 _gcry_mpi_normalize( u );
54 _gcry_mpi_normalize( v );
55 usize = u->nlimbs;
56 vsize = v->nlimbs;
57 if( !u->sign && v->sign )
58 return 1;
59 if( u->sign && !v->sign )
60 return -1;
61 if( usize != vsize && !u->sign && !v->sign )
62 return usize - vsize;
63 if( usize != vsize && u->sign && v->sign )
64 return vsize + usize;
65 if( !usize )
66 return 0;
67 if( !(cmp = _gcry_mpih_cmp( u->d, v->d, usize )) )
68 return 0;
69 if( (cmp < 0?1:0) == (u->sign?1:0))
70 return 1;
71 return -1;
72}
73
74
diff --git a/pwmanager/libcrypt/mpi/mpi-div.c b/pwmanager/libcrypt/mpi/mpi-div.c
new file mode 100644
index 0000000..6172eb4
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/mpi-div.c
@@ -0,0 +1,364 @@
1/* mpi-div.c - MPI functions
2 * Copyright (C) 1994, 1996, 1998, 2001, 2002,
3 * 2003 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 *
21 * Note: This code is heavily based on the GNU MP Library.
22 * Actually it's the same code with only minor changes in the
23 * way the data is stored; this is to support the abstraction
24 * of an optional secure memory allocation which may be used
25 * to avoid revealing of sensitive data due to paging etc.
26 */
27
28#include <config.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include "mpi-internal.h"
32#include "longlong.h"
33#include "g10lib.h"
34
35
36void
37_gcry_mpi_fdiv_r( gcry_mpi_t rem, gcry_mpi_t dividend, gcry_mpi_t divisor )
38{
39 int divisor_sign = divisor->sign;
40 gcry_mpi_t temp_divisor = NULL;
41
42 /* We need the original value of the divisor after the remainder has been
43 * preliminary calculated.We have to copy it to temporary space if it's
44 * the same variable as REM. */
45 if( rem == divisor ) {
46 temp_divisor = mpi_copy( divisor );
47 divisor = temp_divisor;
48 }
49
50 _gcry_mpi_tdiv_r( rem, dividend, divisor );
51
52 if( ((divisor_sign?1:0) ^ (dividend->sign?1:0)) && rem->nlimbs )
53 gcry_mpi_add( rem, rem, divisor);
54
55 if( temp_divisor )
56 mpi_free(temp_divisor);
57}
58
59
60
61/****************
62 * Division rounding the quotient towards -infinity.
63 * The remainder gets the same sign as the denominator.
64 * rem is optional
65 */
66
67ulong
68_gcry_mpi_fdiv_r_ui( gcry_mpi_t rem, gcry_mpi_t dividend, ulong divisor )
69{
70 mpi_limb_t rlimb;
71
72 rlimb = _gcry_mpih_mod_1( dividend->d, dividend->nlimbs, divisor );
73 if( rlimb && dividend->sign )
74 rlimb = divisor - rlimb;
75
76 if( rem ) {
77 rem->d[0] = rlimb;
78 rem->nlimbs = rlimb? 1:0;
79 }
80 return rlimb;
81}
82
83
84void
85_gcry_mpi_fdiv_q( gcry_mpi_t quot, gcry_mpi_t dividend, gcry_mpi_t divisor )
86{
87 gcry_mpi_t tmp = mpi_alloc( mpi_get_nlimbs(quot) );
88 _gcry_mpi_fdiv_qr( quot, tmp, dividend, divisor);
89 mpi_free(tmp);
90}
91
92void
93_gcry_mpi_fdiv_qr( gcry_mpi_t quot, gcry_mpi_t rem, gcry_mpi_t dividend, gcry_mpi_t divisor )
94{
95 int divisor_sign = divisor->sign;
96 gcry_mpi_t temp_divisor = NULL;
97
98 if( quot == divisor || rem == divisor ) {
99 temp_divisor = mpi_copy( divisor );
100 divisor = temp_divisor;
101 }
102
103 _gcry_mpi_tdiv_qr( quot, rem, dividend, divisor );
104
105 if( (divisor_sign ^ dividend->sign) && rem->nlimbs ) {
106 gcry_mpi_sub_ui( quot, quot, 1 );
107 gcry_mpi_add( rem, rem, divisor);
108 }
109
110 if( temp_divisor )
111 mpi_free(temp_divisor);
112}
113
114
115/* If den == quot, den needs temporary storage.
116 * If den == rem, den needs temporary storage.
117 * If num == quot, num needs temporary storage.
118 * If den has temporary storage, it can be normalized while being copied,
119 * i.e no extra storage should be allocated.
120 */
121
122void
123_gcry_mpi_tdiv_r( gcry_mpi_t rem, gcry_mpi_t num, gcry_mpi_t den)
124{
125 _gcry_mpi_tdiv_qr(NULL, rem, num, den );
126}
127
128void
129_gcry_mpi_tdiv_qr( gcry_mpi_t quot, gcry_mpi_t rem, gcry_mpi_t num, gcry_mpi_t den)
130{
131 mpi_ptr_t np, dp;
132 mpi_ptr_t qp, rp;
133 mpi_size_t nsize = num->nlimbs;
134 mpi_size_t dsize = den->nlimbs;
135 mpi_size_t qsize, rsize;
136 mpi_size_t sign_remainder = num->sign;
137 mpi_size_t sign_quotient = num->sign ^ den->sign;
138 unsigned normalization_steps;
139 mpi_limb_t q_limb;
140 mpi_ptr_t marker[5];
141 unsigned int marker_nlimbs[5];
142 int markidx=0;
143
144 /* Ensure space is enough for quotient and remainder.
145 * We need space for an extra limb in the remainder, because it's
146 * up-shifted (normalized) below. */
147 rsize = nsize + 1;
148 mpi_resize( rem, rsize);
149
150 qsize = rsize - dsize; /* qsize cannot be bigger than this.*/
151 if( qsize <= 0 ) {
152 if( num != rem ) {
153 rem->nlimbs = num->nlimbs;
154 rem->sign = num->sign;
155 MPN_COPY(rem->d, num->d, nsize);
156 }
157 if( quot ) {
158 /* This needs to follow the assignment to rem, in case the
159 * numerator and quotient are the same. */
160 quot->nlimbs = 0;
161 quot->sign = 0;
162 }
163 return;
164 }
165
166 if( quot )
167 mpi_resize( quot, qsize);
168
169 /* Read pointers here, when reallocation is finished. */
170 np = num->d;
171 dp = den->d;
172 rp = rem->d;
173
174 /* Optimize division by a single-limb divisor. */
175 if( dsize == 1 ) {
176 mpi_limb_t rlimb;
177 if( quot ) {
178 qp = quot->d;
179 rlimb = _gcry_mpih_divmod_1( qp, np, nsize, dp[0] );
180 qsize -= qp[qsize - 1] == 0;
181 quot->nlimbs = qsize;
182 quot->sign = sign_quotient;
183 }
184 else
185 rlimb = _gcry_mpih_mod_1( np, nsize, dp[0] );
186 rp[0] = rlimb;
187 rsize = rlimb != 0?1:0;
188 rem->nlimbs = rsize;
189 rem->sign = sign_remainder;
190 return;
191 }
192
193
194 if( quot ) {
195 qp = quot->d;
196 /* Make sure QP and NP point to different objects. Otherwise the
197 * numerator would be gradually overwritten by the quotient limbs. */
198 if(qp == np) { /* Copy NP object to temporary space. */
199 marker_nlimbs[markidx] = nsize;
200 np = marker[markidx++] = mpi_alloc_limb_space(nsize,
201 mpi_is_secure(quot));
202 MPN_COPY(np, qp, nsize);
203 }
204 }
205 else /* Put quotient at top of remainder. */
206 qp = rp + dsize;
207
208 count_leading_zeros( normalization_steps, dp[dsize - 1] );
209
210 /* Normalize the denominator, i.e. make its most significant bit set by
211 * shifting it NORMALIZATION_STEPS bits to the left. Also shift the
212 * numerator the same number of steps (to keep the quotient the same!).
213 */
214 if( normalization_steps ) {
215 mpi_ptr_t tp;
216 mpi_limb_t nlimb;
217
218 /* Shift up the denominator setting the most significant bit of
219 * the most significant word. Use temporary storage not to clobber
220 * the original contents of the denominator. */
221 marker_nlimbs[markidx] = dsize;
222 tp = marker[markidx++] = mpi_alloc_limb_space(dsize,mpi_is_secure(den));
223 _gcry_mpih_lshift( tp, dp, dsize, normalization_steps );
224 dp = tp;
225
226 /* Shift up the numerator, possibly introducing a new most
227 * significant word. Move the shifted numerator in the remainder
228 * meanwhile. */
229 nlimb = _gcry_mpih_lshift(rp, np, nsize, normalization_steps);
230 if( nlimb ) {
231 rp[nsize] = nlimb;
232 rsize = nsize + 1;
233 }
234 else
235 rsize = nsize;
236 }
237 else {
238 /* The denominator is already normalized, as required.Copy it to
239 * temporary space if it overlaps with the quotient or remainder. */
240 if( dp == rp || (quot && (dp == qp))) {
241 mpi_ptr_t tp;
242
243 marker_nlimbs[markidx] = dsize;
244 tp = marker[markidx++] = mpi_alloc_limb_space(dsize,
245 mpi_is_secure(den));
246 MPN_COPY( tp, dp, dsize );
247 dp = tp;
248 }
249
250 /* Move the numerator to the remainder. */
251 if( rp != np )
252 MPN_COPY(rp, np, nsize);
253
254 rsize = nsize;
255 }
256
257 q_limb = _gcry_mpih_divrem( qp, 0, rp, rsize, dp, dsize );
258
259 if( quot ) {
260 qsize = rsize - dsize;
261 if(q_limb) {
262 qp[qsize] = q_limb;
263 qsize += 1;
264 }
265
266 quot->nlimbs = qsize;
267 quot->sign = sign_quotient;
268 }
269
270 rsize = dsize;
271 MPN_NORMALIZE (rp, rsize);
272
273 if( normalization_steps && rsize ) {
274 _gcry_mpih_rshift(rp, rp, rsize, normalization_steps);
275 rsize -= rp[rsize - 1] == 0?1:0;
276 }
277
278 rem->nlimbs = rsize;
279 rem->sign= sign_remainder;
280 while( markidx )
281 {
282 markidx--;
283 _gcry_mpi_free_limb_space (marker[markidx], marker_nlimbs[markidx]);
284 }
285}
286
287void
288_gcry_mpi_tdiv_q_2exp( gcry_mpi_t w, gcry_mpi_t u, unsigned int count )
289{
290 mpi_size_t usize, wsize;
291 mpi_size_t limb_cnt;
292
293 usize = u->nlimbs;
294 limb_cnt = count / BITS_PER_MPI_LIMB;
295 wsize = usize - limb_cnt;
296 if( limb_cnt >= usize )
297 w->nlimbs = 0;
298 else {
299 mpi_ptr_t wp;
300 mpi_ptr_t up;
301
302 RESIZE_IF_NEEDED( w, wsize );
303 wp = w->d;
304 up = u->d;
305
306 count %= BITS_PER_MPI_LIMB;
307 if( count ) {
308 _gcry_mpih_rshift( wp, up + limb_cnt, wsize, count );
309 wsize -= !wp[wsize - 1];
310 }
311 else {
312 MPN_COPY_INCR( wp, up + limb_cnt, wsize);
313 }
314
315 w->nlimbs = wsize;
316 }
317}
318
319/****************
320 * Check whether dividend is divisible by divisor
321 * (note: divisor must fit into a limb)
322 */
323int
324_gcry_mpi_divisible_ui(gcry_mpi_t dividend, ulong divisor )
325{
326 return !_gcry_mpih_mod_1( dividend->d, dividend->nlimbs, divisor );
327}
328
329
330void
331gcry_mpi_div (gcry_mpi_t quot, gcry_mpi_t rem, gcry_mpi_t dividend, gcry_mpi_t divisor, int round)
332{
333 if (!round)
334 {
335 if (!rem)
336 {
337 gcry_mpi_t tmp = mpi_alloc (mpi_get_nlimbs(quot));
338 _gcry_mpi_tdiv_qr (quot, tmp, dividend, divisor);
339 mpi_free (tmp);
340 }
341 else
342 _gcry_mpi_tdiv_qr (quot, rem, dividend, divisor);
343 }
344 else if (round < 0)
345 {
346 if (!rem)
347 _gcry_mpi_fdiv_q (quot, dividend, divisor);
348 else if (!quot)
349 _gcry_mpi_fdiv_r (rem, dividend, divisor);
350 else
351 _gcry_mpi_fdiv_qr (quot, rem, dividend, divisor);
352 }
353 else
354 log_bug ("mpi rounding to ceiling not yet implemented\n");
355}
356
357
358void
359gcry_mpi_mod (gcry_mpi_t rem, gcry_mpi_t dividend, gcry_mpi_t divisor)
360{
361 _gcry_mpi_fdiv_r (rem, dividend, divisor);
362 rem->sign = 0;
363}
364
diff --git a/pwmanager/libcrypt/mpi/mpi-gcd.c b/pwmanager/libcrypt/mpi/mpi-gcd.c
new file mode 100644
index 0000000..51841e7
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/mpi-gcd.c
@@ -0,0 +1,54 @@
1/* mpi-gcd.c - MPI functions
2 *Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <config.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include "mpi-internal.h"
25
26/****************
27 * Find the greatest common divisor G of A and B.
28 * Return: true if this 1, false in all other cases
29 */
30int
31gcry_mpi_gcd( gcry_mpi_t g, gcry_mpi_t xa, gcry_mpi_t xb )
32{
33 gcry_mpi_t a, b;
34
35 a = mpi_copy(xa);
36 b = mpi_copy(xb);
37
38 /* TAOCP Vol II, 4.5.2, Algorithm A */
39 a->sign = 0;
40 b->sign = 0;
41 while( gcry_mpi_cmp_ui( b, 0 ) ) {
42 _gcry_mpi_fdiv_r( g, a, b ); /* g used as temorary variable */
43 mpi_set(a,b);
44 mpi_set(b,g);
45 }
46 mpi_set(g, a);
47
48 mpi_free(a);
49 mpi_free(b);
50 return !gcry_mpi_cmp_ui( g, 1);
51}
52
53
54
diff --git a/pwmanager/libcrypt/mpi/mpi-inline.c b/pwmanager/libcrypt/mpi/mpi-inline.c
new file mode 100644
index 0000000..a196946
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/mpi-inline.c
@@ -0,0 +1,36 @@
1/* mpi-inline.c
2 *Copyright (C) 1999, 2002 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <config.h>
22#include <stdio.h>
23#include <stdlib.h>
24
25/* put the inline functions as real functions into the lib */
26#define G10_MPI_INLINE_DECL
27
28#include "mpi-internal.h"
29
30/* always include the header becuase it is only
31 * included by mpi-internal if __GCC__ is defined but we
32 * need it here in all cases and the above definition of
33 * of the macro allows us to do so
34 */
35#include "mpi-inline.h"
36
diff --git a/pwmanager/libcrypt/mpi/mpi-inline.h b/pwmanager/libcrypt/mpi/mpi-inline.h
new file mode 100644
index 0000000..88d9f56
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/mpi-inline.h
@@ -0,0 +1,154 @@
1/* mpi-inline.h - Internal to the Multi Precision Integers
2 * Copyright (C) 1994, 1996, 1998, 1999,
3 * 2001, 2002 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 *
21 * Note: This code is heavily based on the GNU MP Library.
22 * Actually it's the same code with only minor changes in the
23 * way the data is stored; this is to support the abstraction
24 * of an optional secure memory allocation which may be used
25 * to avoid revealing of sensitive data due to paging etc.
26 */
27
28#ifndef G10_MPI_INLINE_H
29#define G10_MPI_INLINE_H
30
31#ifndef G10_MPI_INLINE_DECL
32#define G10_MPI_INLINE_DECL extern __inline__
33#endif
34
35G10_MPI_INLINE_DECL mpi_limb_t
36_gcry_mpih_add_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
37 mpi_size_t s1_size, mpi_limb_t s2_limb)
38{
39 mpi_limb_t x;
40
41 x = *s1_ptr++;
42 s2_limb += x;
43 *res_ptr++ = s2_limb;
44 if( s2_limb < x ) { /* sum is less than the left operand: handle carry */
45 while( --s1_size ) {
46 x = *s1_ptr++ + 1;/* add carry */
47 *res_ptr++ = x;/* and store */
48 if( x ) /* not 0 (no overflow): we can stop */
49 goto leave;
50 }
51 return 1; /* return carry (size of s1 to small) */
52 }
53
54 leave:
55 if( res_ptr != s1_ptr ) { /* not the same variable */
56 mpi_size_t i; /* copy the rest */
57 for( i=0; i < s1_size-1; i++ )
58 res_ptr[i] = s1_ptr[i];
59 }
60 return 0; /* no carry */
61}
62
63
64
65G10_MPI_INLINE_DECL mpi_limb_t
66_gcry_mpih_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
67 mpi_ptr_t s2_ptr, mpi_size_t s2_size)
68{
69 mpi_limb_t cy = 0;
70
71 if( s2_size )
72 cy = _gcry_mpih_add_n( res_ptr, s1_ptr, s2_ptr, s2_size );
73
74 if( s1_size - s2_size )
75 cy = _gcry_mpih_add_1( res_ptr + s2_size, s1_ptr + s2_size,
76 s1_size - s2_size, cy);
77 return cy;
78}
79
80
81G10_MPI_INLINE_DECL mpi_limb_t
82_gcry_mpih_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
83 mpi_size_t s1_size, mpi_limb_t s2_limb )
84{
85 mpi_limb_t x;
86
87 x = *s1_ptr++;
88 s2_limb = x - s2_limb;
89 *res_ptr++ = s2_limb;
90 if( s2_limb > x ) {
91 while( --s1_size ) {
92 x = *s1_ptr++;
93 *res_ptr++ = x - 1;
94 if( x )
95 goto leave;
96 }
97 return 1;
98 }
99
100 leave:
101 if( res_ptr != s1_ptr ) {
102 mpi_size_t i;
103 for( i=0; i < s1_size-1; i++ )
104 res_ptr[i] = s1_ptr[i];
105 }
106 return 0;
107}
108
109
110
111G10_MPI_INLINE_DECL mpi_limb_t
112_gcry_mpih_sub( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
113 mpi_ptr_t s2_ptr, mpi_size_t s2_size)
114{
115 mpi_limb_t cy = 0;
116
117 if( s2_size )
118 cy = _gcry_mpih_sub_n(res_ptr, s1_ptr, s2_ptr, s2_size);
119
120 if( s1_size - s2_size )
121 cy = _gcry_mpih_sub_1(res_ptr + s2_size, s1_ptr + s2_size,
122 s1_size - s2_size, cy);
123 return cy;
124}
125
126/****************
127 * Compare OP1_PTR/OP1_SIZE with OP2_PTR/OP2_SIZE.
128 * There are no restrictions on the relative sizes of
129 * the two arguments.
130 * Return 1 if OP1 > OP2, 0 if they are equal, and -1 if OP1 < OP2.
131 */
132G10_MPI_INLINE_DECL int
133_gcry_mpih_cmp( mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size )
134{
135 mpi_size_t i;
136 mpi_limb_t op1_word, op2_word;
137
138 for( i = size - 1; i >= 0 ; i--) {
139 op1_word = op1_ptr[i];
140 op2_word = op2_ptr[i];
141 if( op1_word != op2_word )
142 goto diff;
143 }
144 return 0;
145
146 diff:
147 /* This can *not* be simplified to
148 * op2_word - op2_word
149 * since that expression might give signed overflow. */
150 return (op1_word > op2_word) ? 1 : -1;
151}
152
153
154#endif /*G10_MPI_INLINE_H*/
diff --git a/pwmanager/libcrypt/mpi/mpi-internal.h b/pwmanager/libcrypt/mpi/mpi-internal.h
new file mode 100644
index 0000000..d78c180
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/mpi-internal.h
@@ -0,0 +1,275 @@
1/* mpi-internal.h - Internal to the Multi Precision Integers
2 * Copyright (C) 1994, 1996, 1998, 2000, 2002,
3 * 2003 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 *
21 * Note: This code is heavily based on the GNU MP Library.
22 * Actually it's the same code with only minor changes in the
23 * way the data is stored; this is to support the abstraction
24 * of an optional secure memory allocation which may be used
25 * to avoid revealing of sensitive data due to paging etc.
26 */
27
28#ifndef G10_MPI_INTERNAL_H
29#define G10_MPI_INTERNAL_H
30
31#include "mpi-asm-defs.h"
32
33#ifndef BITS_PER_MPI_LIMB
34#if BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_INT
35 typedef unsigned int mpi_limb_t;
36 typedef signed int mpi_limb_signed_t;
37#elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG
38 typedef unsigned long int mpi_limb_t;
39 typedef signed long int mpi_limb_signed_t;
40#elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG_LONG
41 typedef unsigned long long int mpi_limb_t;
42 typedef signed long long int mpi_limb_signed_t;
43#elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_SHORT
44 typedef unsigned short int mpi_limb_t;
45 typedef signed short int mpi_limb_signed_t;
46#else
47#error BYTES_PER_MPI_LIMB does not match any C type
48#endif
49#define BITS_PER_MPI_LIMB (8*BYTES_PER_MPI_LIMB)
50#endif /*BITS_PER_MPI_LIMB*/
51
52#include "mpi.h"
53
54/* If KARATSUBA_THRESHOLD is not already defined, define it to a
55 * value which is good on most machines. */
56
57/* tested 4, 16, 32 and 64, where 16 gave the best performance when
58 * checking a 768 and a 1024 bit ElGamal signature.
59 * (wk 22.12.97) */
60#ifndef KARATSUBA_THRESHOLD
61#define KARATSUBA_THRESHOLD 16
62#endif
63
64/* The code can't handle KARATSUBA_THRESHOLD smaller than 2. */
65#if KARATSUBA_THRESHOLD < 2
66#undef KARATSUBA_THRESHOLD
67#define KARATSUBA_THRESHOLD 2
68#endif
69
70
71typedef mpi_limb_t *mpi_ptr_t; /* pointer to a limb */
72typedef int mpi_size_t; /* (must be a signed type) */
73
74#define ABS(x) (x >= 0 ? x : -x)
75#define MIN(l,o) ((l) < (o) ? (l) : (o))
76#define MAX(h,i) ((h) > (i) ? (h) : (i))
77#define RESIZE_IF_NEEDED(a,b) \
78 do { \
79 if( (a)->alloced < (b) ) \
80 mpi_resize((a), (b)); \
81 } while(0)
82
83/* Copy N limbs from S to D. */
84#define MPN_COPY( d, s, n) \
85 do { \
86 mpi_size_t _i; \
87 for( _i = 0; _i < (n); _i++ )\
88 (d)[_i] = (s)[_i]; \
89 } while(0)
90
91 #define MPN_COPY_INCR( d, s, n) \
92 do { \
93 mpi_size_t _i; \
94 for( _i = 0; _i < (n); _i++ )\
95 (d)[_i] = (d)[_i]; \
96 } while (0)
97
98#define MPN_COPY_DECR( d, s, n ) \
99 do { \
100 mpi_size_t _i; \
101 for( _i = (n)-1; _i >= 0; _i--) \
102 (d)[_i] = (s)[_i]; \
103 } while(0)
104
105/* Zero N limbs at D */
106#define MPN_ZERO(d, n) \
107 do { \
108 int _i; \
109 for( _i = 0; _i < (n); _i++ ) \
110 (d)[_i] = 0; \
111 } while (0)
112
113#define MPN_NORMALIZE(d, n) \
114 do { \
115 while( (n) > 0 ) { \
116 if( (d)[(n)-1] ) \
117 break; \
118 (n)--; \
119 } \
120 } while(0)
121
122#define MPN_NORMALIZE_NOT_ZERO(d, n) \
123 do { \
124 for(;;) { \
125 if( (d)[(n)-1] ) \
126 break; \
127 (n)--; \
128 } \
129 } while(0)
130
131#define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
132 do { \
133 if( (size) < KARATSUBA_THRESHOLD ) \
134 mul_n_basecase (prodp, up, vp, size);\
135 else \
136 mul_n (prodp, up, vp, size, tspace);\
137 } while (0);
138
139
140/* Divide the two-limb number in (NH,,NL) by D, with DI being the largest
141 * limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB).
142 * If this would yield overflow, DI should be the largest possible number
143 * (i.e., only ones). For correct operation, the most significant bit of D
144 * has to be set. Put the quotient in Q and the remainder in R.
145 */
146#define UDIV_QRNND_PREINV(q, r, nh, nl, d, di) \
147 do { \
148 mpi_limb_t _q, _ql, _r; \
149 mpi_limb_t _xh, _xl; \
150 umul_ppmm (_q, _ql, (nh), (di)); \
151 _q += (nh);/* DI is 2**BITS_PER_MPI_LIMB too small */ \
152 umul_ppmm (_xh, _xl, _q, (d)); \
153 sub_ddmmss (_xh, _r, (nh), (nl), _xh, _xl); \
154 if( _xh ) { \
155 sub_ddmmss (_xh, _r, _xh, _r, 0, (d)); \
156 _q++; \
157 if( _xh) { \
158 sub_ddmmss (_xh, _r, _xh, _r, 0, (d)); \
159 _q++; \
160 } \
161 } \
162 if( _r >= (d) ) { \
163 _r -= (d); \
164 _q++; \
165 } \
166 (r) = _r; \
167 (q) = _q; \
168 } while (0)
169
170
171/*-- mpiutil.c --*/
172#define mpi_alloc_limb_space(n,f) _gcry_mpi_alloc_limb_space((n),(f))
173mpi_ptr_t _gcry_mpi_alloc_limb_space( unsigned nlimbs, int sec );
174void _gcry_mpi_free_limb_space( mpi_ptr_t a, unsigned int nlimbs );
175void _gcry_mpi_assign_limb_space( gcry_mpi_t a, mpi_ptr_t ap, unsigned nlimbs );
176
177/*-- mpi-bit.c --*/
178void _gcry_mpi_rshift_limbs( gcry_mpi_t a, unsigned int count );
179void _gcry_mpi_lshift_limbs( gcry_mpi_t a, unsigned int count );
180
181
182/*-- mpih-add.c --*/
183mpi_limb_t _gcry_mpih_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
184 mpi_size_t s1_size, mpi_limb_t s2_limb );
185mpi_limb_t _gcry_mpih_add_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
186 mpi_ptr_t s2_ptr, mpi_size_t size);
187mpi_limb_t _gcry_mpih_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
188 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
189
190/*-- mpih-sub.c --*/
191mpi_limb_t _gcry_mpih_sub_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
192 mpi_size_t s1_size, mpi_limb_t s2_limb );
193mpi_limb_t _gcry_mpih_sub_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
194 mpi_ptr_t s2_ptr, mpi_size_t size);
195mpi_limb_t _gcry_mpih_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
196 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
197
198/*-- mpih-cmp.c --*/
199int _gcry_mpih_cmp( mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size );
200
201/*-- mpih-mul.c --*/
202
203struct karatsuba_ctx {
204 struct karatsuba_ctx *next;
205 mpi_ptr_t tspace;
206 unsigned int tspace_nlimbs;
207 mpi_size_t tspace_size;
208 mpi_ptr_t tp;
209 unsigned int tp_nlimbs;
210 mpi_size_t tp_size;
211};
212
213void _gcry_mpih_release_karatsuba_ctx( struct karatsuba_ctx *ctx );
214
215mpi_limb_t _gcry_mpih_addmul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
216 mpi_size_t s1_size, mpi_limb_t s2_limb);
217mpi_limb_t _gcry_mpih_submul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
218 mpi_size_t s1_size, mpi_limb_t s2_limb);
219void _gcry_mpih_mul_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp,
220 mpi_size_t size);
221mpi_limb_t _gcry_mpih_mul( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
222 mpi_ptr_t vp, mpi_size_t vsize);
223void _gcry_mpih_sqr_n_basecase( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size );
224void _gcry_mpih_sqr_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size,
225 mpi_ptr_t tspace);
226
227void _gcry_mpih_mul_karatsuba_case( mpi_ptr_t prodp,
228 mpi_ptr_t up, mpi_size_t usize,
229 mpi_ptr_t vp, mpi_size_t vsize,
230 struct karatsuba_ctx *ctx );
231
232
233/*-- mpih-mul_1.c (or xxx/cpu/ *.S) --*/
234mpi_limb_t _gcry_mpih_mul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
235 mpi_size_t s1_size, mpi_limb_t s2_limb);
236
237/*-- mpih-div.c --*/
238mpi_limb_t _gcry_mpih_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
239 mpi_limb_t divisor_limb);
240mpi_limb_t _gcry_mpih_divrem( mpi_ptr_t qp, mpi_size_t qextra_limbs,
241 mpi_ptr_t np, mpi_size_t nsize,
242 mpi_ptr_t dp, mpi_size_t dsize);
243mpi_limb_t _gcry_mpih_divmod_1( mpi_ptr_t quot_ptr,
244 mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
245 mpi_limb_t divisor_limb);
246
247/*-- mpih-shift.c --*/
248mpi_limb_t _gcry_mpih_lshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
249 unsigned cnt);
250mpi_limb_t _gcry_mpih_rshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
251 unsigned cnt);
252
253
254/* Define stuff for longlong.h. */
255#define W_TYPE_SIZE BITS_PER_MPI_LIMB
256 typedef mpi_limb_t UWtype;
257 typedef unsigned int UHWtype;
258#if defined (__GNUC__)
259 typedef unsigned int UQItype __attribute__ ((mode (QI)));
260 typedef int SItype __attribute__ ((mode (SI)));
261 typedef unsigned int USItype __attribute__ ((mode (SI)));
262 typedef int DItype __attribute__ ((mode (DI)));
263 typedef unsigned int UDItype __attribute__ ((mode (DI)));
264#else
265 typedef unsigned char UQItype;
266 typedef long SItype;
267 typedef unsigned long USItype;
268#endif
269
270#ifdef __GNUC__
271#include "mpi-inline.h"
272#endif
273
274#endif /*G10_MPI_INTERNAL_H*/
275
diff --git a/pwmanager/libcrypt/mpi/mpi-inv.c b/pwmanager/libcrypt/mpi/mpi-inv.c
new file mode 100644
index 0000000..2e737b8
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/mpi-inv.c
@@ -0,0 +1,275 @@
1/* mpi-inv.c - MPI functions
2 *Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <config.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include "mpi-internal.h"
25#include "g10lib.h"
26
27/****************
28 * Calculate the multiplicative inverse X of A mod N
29 * That is: Find the solution x for
30 * 1 = (a*x) mod n
31 */
32void
33_gcry_mpi_invm( gcry_mpi_t x, gcry_mpi_t a, gcry_mpi_t n )
34{
35#if 0
36 gcry_mpi_t u, v, u1, u2, u3, v1, v2, v3, q, t1, t2, t3;
37 gcry_mpi_t ta, tb, tc;
38
39 u = mpi_copy(a);
40 v = mpi_copy(n);
41 u1 = mpi_alloc_set_ui(1);
42 u2 = mpi_alloc_set_ui(0);
43 u3 = mpi_copy(u);
44 v1 = mpi_alloc_set_ui(0);
45 v2 = mpi_alloc_set_ui(1);
46 v3 = mpi_copy(v);
47 q = mpi_alloc( mpi_get_nlimbs(u)+1 );
48 t1 = mpi_alloc( mpi_get_nlimbs(u)+1 );
49 t2 = mpi_alloc( mpi_get_nlimbs(u)+1 );
50 t3 = mpi_alloc( mpi_get_nlimbs(u)+1 );
51 while( mpi_cmp_ui( v3, 0 ) ) {
52 mpi_fdiv_q( q, u3, v3 );
53 mpi_mul(t1, v1, q); mpi_mul(t2, v2, q); mpi_mul(t3, v3, q);
54 mpi_sub(t1, u1, t1); mpi_sub(t2, u2, t2); mpi_sub(t3, u3, t3);
55 mpi_set(u1, v1); mpi_set(u2, v2); mpi_set(u3, v3);
56 mpi_set(v1, t1); mpi_set(v2, t2); mpi_set(v3, t3);
57 }
58 /*log_debug("result:\n");
59 log_mpidump("q =", q );
60 log_mpidump("u1=", u1);
61 log_mpidump("u2=", u2);
62 log_mpidump("u3=", u3);
63 log_mpidump("v1=", v1);
64 log_mpidump("v2=", v2); */
65 mpi_set(x, u1);
66
67 mpi_free(u1);
68 mpi_free(u2);
69 mpi_free(u3);
70 mpi_free(v1);
71 mpi_free(v2);
72 mpi_free(v3);
73 mpi_free(q);
74 mpi_free(t1);
75 mpi_free(t2);
76 mpi_free(t3);
77 mpi_free(u);
78 mpi_free(v);
79#elif 0
80 /* Extended Euclid's algorithm (See TAOCP Vol II, 4.5.2, Alg X)
81 * modified according to Michael Penk's solution for Exercise 35 */
82
83 /* FIXME: we can simplify this in most cases (see Knuth) */
84 gcry_mpi_t u, v, u1, u2, u3, v1, v2, v3, t1, t2, t3;
85 unsigned k;
86 int sign;
87
88 u = mpi_copy(a);
89 v = mpi_copy(n);
90 for(k=0; !mpi_test_bit(u,0) && !mpi_test_bit(v,0); k++ ) {
91 mpi_rshift(u, u, 1);
92 mpi_rshift(v, v, 1);
93 }
94
95
96 u1 = mpi_alloc_set_ui(1);
97 u2 = mpi_alloc_set_ui(0);
98 u3 = mpi_copy(u);
99 v1 = mpi_copy(v); /* !-- used as const 1 */
100 v2 = mpi_alloc( mpi_get_nlimbs(u) ); mpi_sub( v2, u1, u );
101 v3 = mpi_copy(v);
102 if( mpi_test_bit(u, 0) ) { /* u is odd */
103 t1 = mpi_alloc_set_ui(0);
104 t2 = mpi_alloc_set_ui(1); t2->sign = 1;
105 t3 = mpi_copy(v); t3->sign = !t3->sign;
106 goto Y4;
107 }
108 else {
109 t1 = mpi_alloc_set_ui(1);
110 t2 = mpi_alloc_set_ui(0);
111 t3 = mpi_copy(u);
112 }
113 do {
114 do {
115 if( mpi_test_bit(t1, 0) || mpi_test_bit(t2, 0) ) { /* one is odd */
116 mpi_add(t1, t1, v);
117 mpi_sub(t2, t2, u);
118 }
119 mpi_rshift(t1, t1, 1);
120 mpi_rshift(t2, t2, 1);
121 mpi_rshift(t3, t3, 1);
122 Y4:
123 ;
124 } while( !mpi_test_bit( t3, 0 ) ); /* while t3 is even */
125
126 if( !t3->sign ) {
127 mpi_set(u1, t1);
128 mpi_set(u2, t2);
129 mpi_set(u3, t3);
130 }
131 else {
132 mpi_sub(v1, v, t1);
133 sign = u->sign; u->sign = !u->sign;
134 mpi_sub(v2, u, t2);
135 u->sign = sign;
136 sign = t3->sign; t3->sign = !t3->sign;
137 mpi_set(v3, t3);
138 t3->sign = sign;
139 }
140 mpi_sub(t1, u1, v1);
141 mpi_sub(t2, u2, v2);
142 mpi_sub(t3, u3, v3);
143 if( t1->sign ) {
144 mpi_add(t1, t1, v);
145 mpi_sub(t2, t2, u);
146 }
147 } while( mpi_cmp_ui( t3, 0 ) ); /* while t3 != 0 */
148 /* mpi_lshift( u3, k ); */
149 mpi_set(x, u1);
150
151 mpi_free(u1);
152 mpi_free(u2);
153 mpi_free(u3);
154 mpi_free(v1);
155 mpi_free(v2);
156 mpi_free(v3);
157 mpi_free(t1);
158 mpi_free(t2);
159 mpi_free(t3);
160#else
161 /* Extended Euclid's algorithm (See TAOCP Vol II, 4.5.2, Alg X)
162 * modified according to Michael Penk's solution for Exercise 35
163 * with further enhancement */
164 gcry_mpi_t u, v, u1, u2=NULL, u3, v1, v2=NULL, v3, t1, t2=NULL, t3;
165 unsigned k;
166 int sign;
167 int odd ;
168
169 u = mpi_copy(a);
170 v = mpi_copy(n);
171
172 for(k=0; !mpi_test_bit(u,0) && !mpi_test_bit(v,0); k++ ) {
173 mpi_rshift(u, u, 1);
174 mpi_rshift(v, v, 1);
175 }
176 odd = mpi_test_bit(v,0);
177
178 u1 = mpi_alloc_set_ui(1);
179 if( !odd )
180 u2 = mpi_alloc_set_ui(0);
181 u3 = mpi_copy(u);
182 v1 = mpi_copy(v);
183 if( !odd ) {
184 v2 = mpi_alloc( mpi_get_nlimbs(u) );
185 mpi_sub( v2, u1, u ); /* U is used as const 1 */
186 }
187 v3 = mpi_copy(v);
188 if( mpi_test_bit(u, 0) ) { /* u is odd */
189 t1 = mpi_alloc_set_ui(0);
190 if( !odd ) {
191 t2 = mpi_alloc_set_ui(1); t2->sign = 1;
192 }
193 t3 = mpi_copy(v); t3->sign = !t3->sign;
194 goto Y4;
195 }
196 else {
197 t1 = mpi_alloc_set_ui(1);
198 if( !odd )
199 t2 = mpi_alloc_set_ui(0);
200 t3 = mpi_copy(u);
201 }
202 do {
203 do {
204 if( !odd ) {
205 if( mpi_test_bit(t1, 0) || mpi_test_bit(t2, 0) ) { /* one is odd */
206 mpi_add(t1, t1, v);
207 mpi_sub(t2, t2, u);
208 }
209 mpi_rshift(t1, t1, 1);
210 mpi_rshift(t2, t2, 1);
211 mpi_rshift(t3, t3, 1);
212 }
213 else {
214 if( mpi_test_bit(t1, 0) )
215 mpi_add(t1, t1, v);
216 mpi_rshift(t1, t1, 1);
217 mpi_rshift(t3, t3, 1);
218 }
219 Y4:
220 ;
221 } while( !mpi_test_bit( t3, 0 ) ); /* while t3 is even */
222
223 if( !t3->sign ) {
224 mpi_set(u1, t1);
225 if( !odd )
226 mpi_set(u2, t2);
227 mpi_set(u3, t3);
228 }
229 else {
230 mpi_sub(v1, v, t1);
231 sign = u->sign; u->sign = !u->sign;
232 if( !odd )
233 mpi_sub(v2, u, t2);
234 u->sign = sign;
235 sign = t3->sign; t3->sign = !t3->sign;
236 mpi_set(v3, t3);
237 t3->sign = sign;
238 }
239 mpi_sub(t1, u1, v1);
240 if( !odd )
241 mpi_sub(t2, u2, v2);
242 mpi_sub(t3, u3, v3);
243 if( t1->sign ) {
244 mpi_add(t1, t1, v);
245 if( !odd )
246 mpi_sub(t2, t2, u);
247 }
248 } while( mpi_cmp_ui( t3, 0 ) ); /* while t3 != 0 */
249 /* mpi_lshift( u3, k ); */
250 mpi_set(x, u1);
251
252 mpi_free(u1);
253 mpi_free(v1);
254 mpi_free(t1);
255 if( !odd ) {
256 mpi_free(u2);
257 mpi_free(v2);
258 mpi_free(t2);
259 }
260 mpi_free(u3);
261 mpi_free(v3);
262 mpi_free(t3);
263
264 mpi_free(u);
265 mpi_free(v);
266#endif
267}
268
269
270int
271gcry_mpi_invm (gcry_mpi_t x, gcry_mpi_t a, gcry_mpi_t n)
272{
273 _gcry_mpi_invm (x, a, n);
274 return 1;
275}
diff --git a/pwmanager/libcrypt/mpi/mpi-mpow.c b/pwmanager/libcrypt/mpi/mpi-mpow.c
new file mode 100644
index 0000000..f37c6f9
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/mpi-mpow.c
@@ -0,0 +1,224 @@
1/* mpi-mpow.c - MPI functions
2 *Copyright (C) 1998, 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <config.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include "mpi-internal.h"
25#include "longlong.h"
26#include "g10lib.h"
27#include <assert.h>
28
29
30/* Barrett is slower than the classical way. It can be tweaked by
31 * using partial multiplications
32 */
33/*#define USE_BARRETT*/
34
35
36
37#ifdef USE_BARRETT
38static void barrett_mulm( gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m, gcry_mpi_t y, int k, gcry_mpi_t r1, gcry_mpi_t r2 );
39static gcry_mpi_t init_barrett( gcry_mpi_t m, int *k, gcry_mpi_t *r1, gcry_mpi_t *r2 );
40static int calc_barrett( gcry_mpi_t r, gcry_mpi_t x, gcry_mpi_t m, gcry_mpi_t y, int k, gcry_mpi_t r1, gcry_mpi_t r2 );
41#else
42#define barrett_mulm( w, u, v, m, y, k, r1, r2 ) gcry_mpi_mulm( (w), (u), (v), (m) )
43#endif
44
45
46static int
47build_index( gcry_mpi_t *exparray, int k, int i, int t )
48{
49 int j, bitno;
50 int idx = 0;
51
52 bitno = t-i;
53 for(j=k-1; j >= 0; j-- ) {
54 idx <<= 1;
55 if( mpi_test_bit( exparray[j], bitno ) )
56 idx |= 1;
57 }
58 /*log_debug("t=%d i=%d idx=%d\n", t, i, idx );*/
59 return idx;
60}
61
62/****************
63 * RES = (BASE[0] ^ EXP[0]) * (BASE[1] ^ EXP[1]) * ... * mod M
64 */
65void
66_gcry_mpi_mulpowm( gcry_mpi_t res, gcry_mpi_t *basearray, gcry_mpi_t *exparray, gcry_mpi_t m)
67{
68 int k;/* number of elements */
69 int t;/* bit size of largest exponent */
70 int i, j, idx;
71 gcry_mpi_t *G;/* table with precomputed values of size 2^k */
72 gcry_mpi_t tmp;
73#ifdef USE_BARRETT
74 gcry_mpi_t barrett_y, barrett_r1, barrett_r2;
75 int barrett_k;
76#endif
77
78 for(k=0; basearray[k]; k++ )
79 ;
80 assert(k);
81 for(t=0, i=0; (tmp=exparray[i]); i++ ) {
82 /*log_mpidump("exp: ", tmp );*/
83 j = mpi_get_nbits(tmp);
84 if( j > t )
85 t = j;
86 }
87 /*log_mpidump("mod: ", m );*/
88 assert(i==k);
89 assert(t);
90 assert( k < 10 );
91
92 G = gcry_xcalloc( (1<<k) , sizeof *G );
93#ifdef USE_BARRETT
94 barrett_y = init_barrett( m, &barrett_k, &barrett_r1, &barrett_r2 );
95#endif
96 /* and calculate */
97 tmp = mpi_alloc( mpi_get_nlimbs(m)+1 );
98 mpi_set_ui( res, 1 );
99 for(i = 1; i <= t; i++ ) {
100 barrett_mulm(tmp, res, res, m, barrett_y, barrett_k,
101 barrett_r1, barrett_r2 );
102 idx = build_index( exparray, k, i, t );
103 assert( idx >= 0 && idx < (1<<k) );
104 if( !G[idx] ) {
105 if( !idx )
106 G[0] = mpi_alloc_set_ui( 1 );
107 else {
108 for(j=0; j < k; j++ ) {
109 if( (idx & (1<<j) ) ) {
110 if( !G[idx] )
111 G[idx] = mpi_copy( basearray[j] );
112 else
113 barrett_mulm( G[idx], G[idx], basearray[j],
114 m, barrett_y, barrett_k, barrett_r1, barrett_r2);
115 }
116 }
117 if( !G[idx] )
118 G[idx] = mpi_alloc(0);
119 }
120 }
121 barrett_mulm(res, tmp, G[idx], m, barrett_y, barrett_k, barrett_r1, barrett_r2);
122 }
123
124 /* cleanup */
125 mpi_free(tmp);
126#ifdef USE_BARRETT
127 mpi_free(barrett_y);
128 mpi_free(barrett_r1);
129 mpi_free(barrett_r2);
130#endif
131 for(i=0; i < (1<<k); i++ )
132 mpi_free(G[i]);
133 gcry_free(G);
134}
135
136
137
138#ifdef USE_BARRETT
139static void
140 barrett_mulm( gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m, gcry_mpi_t y, int k, gcry_mpi_t r1, gcry_mpi_t r2)
141{
142 mpi_mul(w, u, v);
143 if( calc_barrett( w, w, m, y, k, r1, r2 ) )
144 mpi_fdiv_r( w, w, m );
145}
146
147/****************
148 * Barrett precalculation: y = floor(b^(2k) / m)
149 */
150static gcry_mpi_t
151init_barrett( gcry_mpi_t m, int *k, gcry_mpi_t *r1, gcry_mpi_t *r2 )
152{
153 gcry_mpi_t tmp;
154
155 mpi_normalize( m );
156 *k = mpi_get_nlimbs( m );
157 tmp = mpi_alloc( *k + 1 );
158 mpi_set_ui( tmp, 1 );
159 mpi_lshift_limbs( tmp, 2 * *k );
160 mpi_fdiv_q( tmp, tmp, m );
161 *r1 = mpi_alloc( 2* *k + 1 );
162 *r2 = mpi_alloc( 2* *k + 1 );
163 return tmp;
164}
165
166/****************
167 * Barrett reduction: We assume that these conditions are met:
168 * Given x =(x_2k-1 ...x_0)_b
169 * m =(m_k-1 ....m_0)_b with m_k-1 != 0
170 * Output r = x mod m
171 * Before using this function init_barret must be used to calucalte y and k.
172 * Returns: false = no error
173 * true = can't perform barret reduction
174 */
175static int
176calc_barrett( gcry_mpi_t r, gcry_mpi_t x, gcry_mpi_t m, gcry_mpi_t y, int k, gcry_mpi_t r1, gcry_mpi_t r2 )
177{
178 int xx = k > 3 ? k-3:0;
179
180 mpi_normalize( x );
181 if( mpi_get_nlimbs(x) > 2*k )
182 return 1; /* can't do it */
183
184 /* 1. q1 = floor( x / b^k-1)
185 * q2 = q1 * y
186 * q3 = floor( q2 / b^k+1 )
187 * Actually, we don't need qx, we can work direct on r2
188 */
189 mpi_set( r2, x );
190 mpi_rshift_limbs( r2, k-1 );
191 mpi_mul( r2, r2, y );
192 mpi_rshift_limbs( r2, k+1 );
193
194 /* 2. r1 = x mod b^k+1
195 * r2 = q3 * m mod b^k+1
196 * r = r1 - r2
197 * 3. if r < 0 then r = r + b^k+1
198 */
199 mpi_set( r1, x );
200 if( r1->nlimbs > k+1 ) /* quick modulo operation */
201 r1->nlimbs = k+1;
202 mpi_mul( r2, r2, m );
203 if( r2->nlimbs > k+1 ) /* quick modulo operation */
204 r2->nlimbs = k+1;
205 mpi_sub( r, r1, r2 );
206
207 if( mpi_is_neg( r ) ) {
208 gcry_mpi_t tmp;
209
210 tmp = mpi_alloc( k + 2 );
211 mpi_set_ui( tmp, 1 );
212 mpi_lshift_limbs( tmp, k+1 );
213 mpi_add( r, r, tmp );
214 mpi_free(tmp);
215 }
216
217 /* 4. while r >= m do r = r - m */
218 while( mpi_cmp( r, m ) >= 0 )
219 mpi_sub( r, r, m );
220
221 return 0;
222}
223#endif /* USE_BARRETT */
224
diff --git a/pwmanager/libcrypt/mpi/mpi-mul.c b/pwmanager/libcrypt/mpi/mpi-mul.c
new file mode 100644
index 0000000..23beee0
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/mpi-mul.c
@@ -0,0 +1,220 @@
1/* mpi-mul.c - MPI functions
2 * Copyright (C) 1994, 1996, 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 *
20 * Note: This code is heavily based on the GNU MP Library.
21 * Actually it's the same code with only minor changes in the
22 * way the data is stored; this is to support the abstraction
23 * of an optional secure memory allocation which may be used
24 * to avoid revealing of sensitive data due to paging etc.
25 */
26
27#include <config.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include "mpi-internal.h"
31
32
33void
34gcry_mpi_mul_ui( gcry_mpi_t prod, gcry_mpi_t mult, unsigned long small_mult )
35{
36 mpi_size_t size, prod_size;
37 mpi_ptr_t prod_ptr;
38 mpi_limb_t cy;
39 int sign;
40
41 size = mult->nlimbs;
42 sign = mult->sign;
43
44 if( !size || !small_mult ) {
45 prod->nlimbs = 0;
46 prod->sign = 0;
47 return;
48 }
49
50 prod_size = size + 1;
51 if( prod->alloced < prod_size )
52 mpi_resize( prod, prod_size );
53 prod_ptr = prod->d;
54
55 cy = _gcry_mpih_mul_1( prod_ptr, mult->d, size, (mpi_limb_t)small_mult );
56 if( cy )
57 prod_ptr[size++] = cy;
58 prod->nlimbs = size;
59 prod->sign = sign;
60}
61
62
63void
64_gcry_mpi_mul_2exp( gcry_mpi_t w, gcry_mpi_t u, unsigned long cnt)
65{
66 mpi_size_t usize, wsize, limb_cnt;
67 mpi_ptr_t wp;
68 mpi_limb_t wlimb;
69 int usign, wsign;
70
71 usize = u->nlimbs;
72 usign = u->sign;
73
74 if( !usize ) {
75 w->nlimbs = 0;
76 w->sign = 0;
77 return;
78 }
79
80 limb_cnt = cnt / BITS_PER_MPI_LIMB;
81 wsize = usize + limb_cnt + 1;
82 if( w->alloced < wsize )
83 mpi_resize(w, wsize );
84 wp = w->d;
85 wsize = usize + limb_cnt;
86 wsign = usign;
87
88 cnt %= BITS_PER_MPI_LIMB;
89 if( cnt ) {
90 wlimb = _gcry_mpih_lshift( wp + limb_cnt, u->d, usize, cnt );
91 if( wlimb ) {
92 wp[wsize] = wlimb;
93 wsize++;
94 }
95 }
96 else {
97 MPN_COPY_DECR( wp + limb_cnt, u->d, usize );
98 }
99
100 /* Zero all whole limbs at low end. Do it here and not before calling
101 * mpn_lshift, not to lose for U == W. */
102 MPN_ZERO( wp, limb_cnt );
103
104 w->nlimbs = wsize;
105 w->sign = wsign;
106}
107
108
109void
110gcry_mpi_mul_2exp( gcry_mpi_t w, gcry_mpi_t u, unsigned long cnt)
111{
112 _gcry_mpi_mul_2exp (w, u, cnt);
113}
114
115
116void
117gcry_mpi_mul( gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v)
118{
119 mpi_size_t usize, vsize, wsize;
120 mpi_ptr_t up, vp, wp;
121 mpi_limb_t cy;
122 int usign, vsign, usecure, vsecure, sign_product;
123 int assign_wp=0;
124 mpi_ptr_t tmp_limb=NULL;
125 unsigned int tmp_limb_nlimbs = 0;
126
127 if( u->nlimbs < v->nlimbs ) { /* Swap U and V. */
128 usize = v->nlimbs;
129 usign = v->sign;
130 usecure = mpi_is_secure(v);
131 up = v->d;
132 vsize = u->nlimbs;
133 vsign = u->sign;
134 vsecure = mpi_is_secure(u);
135 vp = u->d;
136 }
137 else {
138 usize = u->nlimbs;
139 usign = u->sign;
140 usecure = mpi_is_secure(u);
141 up = u->d;
142 vsize = v->nlimbs;
143 vsign = v->sign;
144 vsecure = mpi_is_secure(v);
145 vp = v->d;
146 }
147 sign_product = usign ^ vsign;
148 wp = w->d;
149
150 /* Ensure W has space enough to store the result. */
151 wsize = usize + vsize;
152 if ( !mpi_is_secure (w) && (mpi_is_secure (u) || mpi_is_secure (v)) ) {
153 /* w is not allocated in secure space but u or v is. To make sure
154 * that no temporray results are stored in w, we temporary use
155 * a newly allocated limb space for w */
156 wp = mpi_alloc_limb_space( wsize, 1 );
157 assign_wp = 2; /* mark it as 2 so that we can later copy it back to
158 * mormal memory */
159 }
160 else if( w->alloced < wsize ) {
161 if( wp == up || wp == vp ) {
162 wp = mpi_alloc_limb_space( wsize, mpi_is_secure(w) );
163 assign_wp = 1;
164 }
165 else {
166 mpi_resize(w, wsize );
167 wp = w->d;
168 }
169 }
170 else { /* Make U and V not overlap with W.*/
171 if( wp == up ) {
172 /* W and U are identical. Allocate temporary space for U.*/
173 tmp_limb_nlimbs = usize;
174 up = tmp_limb = mpi_alloc_limb_space( usize, usecure );
175 /* Is V identical too? Keep it identical with U. */
176 if( wp == vp )
177 vp = up;
178 /* Copy to the temporary space. */
179 MPN_COPY( up, wp, usize );
180 }
181 else if( wp == vp ) {
182 /* W and V are identical. Allocate temporary space for V.*/
183 tmp_limb_nlimbs = vsize;
184 vp = tmp_limb = mpi_alloc_limb_space( vsize, vsecure );
185 /* Copy to the temporary space. */
186 MPN_COPY( vp, wp, vsize );
187 }
188 }
189
190 if( !vsize )
191 wsize = 0;
192 else {
193 cy = _gcry_mpih_mul( wp, up, usize, vp, vsize );
194 wsize -= cy? 0:1;
195 }
196
197 if( assign_wp ) {
198 if (assign_wp == 2) {
199 /* copy the temp wp from secure memory back to normal memory */
200 mpi_ptr_t tmp_wp = mpi_alloc_limb_space (wsize, 0);
201 MPN_COPY (tmp_wp, wp, wsize);
202 _gcry_mpi_free_limb_space (wp, 0);
203 wp = tmp_wp;
204 }
205 _gcry_mpi_assign_limb_space( w, wp, wsize );
206 }
207 w->nlimbs = wsize;
208 w->sign = sign_product;
209 if( tmp_limb )
210 _gcry_mpi_free_limb_space (tmp_limb, tmp_limb_nlimbs);
211}
212
213
214void
215gcry_mpi_mulm( gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m)
216{
217 gcry_mpi_mul(w, u, v);
218 _gcry_mpi_fdiv_r( w, w, m );
219}
220
diff --git a/pwmanager/libcrypt/mpi/mpi-pow.c b/pwmanager/libcrypt/mpi/mpi-pow.c
new file mode 100644
index 0000000..61a115f
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/mpi-pow.c
@@ -0,0 +1,302 @@
1/* mpi-pow.c - MPI functions
2 * Copyright (C) 1994, 1996, 1998, 2000, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 *
20 * Note: This code is heavily based on the GNU MP Library.
21 * Actually it's the same code with only minor changes in the
22 * way the data is stored; this is to support the abstraction
23 * of an optional secure memory allocation which may be used
24 * to avoid revealing of sensitive data due to paging etc.
25 */
26
27#include <config.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include "mpi-internal.h"
32#include "longlong.h"
33#include <assert.h>
34
35
36/****************
37 * RES = BASE ^ EXPO mod MOD
38 */
39void
40gcry_mpi_powm( gcry_mpi_t res, gcry_mpi_t base, gcry_mpi_t expo, gcry_mpi_t mod)
41{
42 mpi_ptr_t rp, ep, mp, bp;
43 mpi_size_t esize, msize, bsize, rsize;
44 int esign, msign, bsign, rsign;
45 int esec, msec, bsec, rsec;
46 mpi_size_t size;
47 int mod_shift_cnt;
48 int negative_result;
49 mpi_ptr_t mp_marker=NULL, bp_marker=NULL, ep_marker=NULL;
50 mpi_ptr_t xp_marker=NULL;
51 unsigned int mp_nlimbs = 0, bp_nlimbs = 0, ep_nlimbs = 0;
52 unsigned int xp_nlimbs = 0;
53 int assign_rp = 0;
54 mpi_ptr_t tspace = NULL;
55 mpi_size_t tsize=0; /* to avoid compiler warning */
56 /* fixme: we should check that the warning is void*/
57
58 esize = expo->nlimbs;
59 msize = mod->nlimbs;
60 size = 2 * msize;
61 esign = expo->sign;
62 msign = mod->sign;
63
64 esec = mpi_is_secure(expo);
65 msec = mpi_is_secure(mod);
66 bsec = mpi_is_secure(base);
67 rsec = mpi_is_secure(res);
68
69 rp = res->d;
70 ep = expo->d;
71
72 if( !msize )
73 msize = 1 / msize; /* provoke a signal */
74
75 if( !esize ) {
76 /* Exponent is zero, result is 1 mod MOD, i.e., 1 or 0
77 * depending on if MOD equals 1. */
78 rp[0] = 1;
79 res->nlimbs = (msize == 1 && mod->d[0] == 1) ? 0 : 1;
80 res->sign = 0;
81 goto leave;
82 }
83
84 /* Normalize MOD (i.e. make its most significant bit set) as required by
85 * mpn_divrem. This will make the intermediate values in the calculation
86 * slightly larger, but the correct result is obtained after a final
87 * reduction using the original MOD value.*/
88 mp_nlimbs = msec? msize:0;
89 mp = mp_marker = mpi_alloc_limb_space(msize, msec);
90 count_leading_zeros( mod_shift_cnt, mod->d[msize-1] );
91 if( mod_shift_cnt )
92 _gcry_mpih_lshift( mp, mod->d, msize, mod_shift_cnt );
93 else
94 MPN_COPY( mp, mod->d, msize );
95
96 bsize = base->nlimbs;
97 bsign = base->sign;
98 if( bsize > msize ) { /* The base is larger than the module. Reduce it. */
99 /* Allocate (BSIZE + 1) with space for remainder and quotient.
100 * (The quotient is (bsize - msize + 1) limbs.) */
101 bp_nlimbs = bsec ? (bsize + 1):0;
102 bp = bp_marker = mpi_alloc_limb_space( bsize + 1, bsec );
103 MPN_COPY( bp, base->d, bsize );
104 /* We don't care about the quotient, store it above the remainder,
105 * at BP + MSIZE. */
106 _gcry_mpih_divrem( bp + msize, 0, bp, bsize, mp, msize );
107 bsize = msize;
108 /* Canonicalize the base, since we are going to multiply with it
109 * quite a few times. */
110 MPN_NORMALIZE( bp, bsize );
111 }
112 else
113 bp = base->d;
114
115 if( !bsize ) {
116 res->nlimbs = 0;
117 res->sign = 0;
118 goto leave;
119 }
120
121 if( res->alloced < size ) {
122 /* We have to allocate more space for RES. If any of the input
123 * parameters are identical to RES, defer deallocation of the old
124 * space. */
125 if( rp == ep || rp == mp || rp == bp ) {
126 rp = mpi_alloc_limb_space( size, rsec );
127 assign_rp = 1;
128 }
129 else {
130 mpi_resize( res, size );
131 rp = res->d;
132 }
133 }
134 else { /* Make BASE, EXPO and MOD not overlap with RES. */
135 if( rp == bp ) {
136 /* RES and BASE are identical. Allocate temp. space for BASE. */
137 assert( !bp_marker );
138 bp_nlimbs = bsec? bsize:0;
139 bp = bp_marker = mpi_alloc_limb_space( bsize, bsec );
140 MPN_COPY(bp, rp, bsize);
141 }
142 if( rp == ep ) {
143 /* RES and EXPO are identical. Allocate temp. space for EXPO. */
144 ep_nlimbs = esec? esize:0;
145 ep = ep_marker = mpi_alloc_limb_space( esize, esec );
146 MPN_COPY(ep, rp, esize);
147 }
148 if( rp == mp ) {
149 /* RES and MOD are identical. Allocate temporary space for MOD.*/
150 assert( !mp_marker );
151 mp_nlimbs = msec?msize:0;
152 mp = mp_marker = mpi_alloc_limb_space( msize, msec );
153 MPN_COPY(mp, rp, msize);
154 }
155 }
156
157 MPN_COPY( rp, bp, bsize );
158 rsize = bsize;
159 rsign = bsign;
160
161 {
162 mpi_size_t i;
163 mpi_ptr_t xp;
164 int c;
165 mpi_limb_t e;
166 mpi_limb_t carry_limb;
167 struct karatsuba_ctx karactx;
168
169 xp_nlimbs = msec? (2 * (msize + 1)):0;
170 xp = xp_marker = mpi_alloc_limb_space( 2 * (msize + 1), msec );
171
172 memset( &karactx, 0, sizeof karactx );
173 negative_result = (ep[0] & 1) && base->sign;
174
175 i = esize - 1;
176 e = ep[i];
177 count_leading_zeros (c, e);
178 e = (e << c) << 1; /* shift the expo bits to the left, lose msb */
179 c = BITS_PER_MPI_LIMB - 1 - c;
180
181 /* Main loop.
182 *
183 * Make the result be pointed to alternately by XP and RP. This
184 * helps us avoid block copying, which would otherwise be necessary
185 * with the overlap restrictions of _gcry_mpih_divmod. With 50% probability
186 * the result after this loop will be in the area originally pointed
187 * by RP (==RES->d), and with 50% probability in the area originally
188 * pointed to by XP.
189 */
190
191 for(;;) {
192 while( c ) {
193 mpi_ptr_t tp;
194 mpi_size_t xsize;
195
196 /*mpih_mul_n(xp, rp, rp, rsize);*/
197 if( rsize < KARATSUBA_THRESHOLD )
198 _gcry_mpih_sqr_n_basecase( xp, rp, rsize );
199 else {
200 if( !tspace ) {
201 tsize = 2 * rsize;
202 tspace = mpi_alloc_limb_space( tsize, 0 );
203 }
204 else if( tsize < (2*rsize) ) {
205 _gcry_mpi_free_limb_space (tspace, 0);
206 tsize = 2 * rsize;
207 tspace = mpi_alloc_limb_space( tsize, 0 );
208 }
209 _gcry_mpih_sqr_n( xp, rp, rsize, tspace );
210 }
211
212 xsize = 2 * rsize;
213 if( xsize > msize ) {
214 _gcry_mpih_divrem(xp + msize, 0, xp, xsize, mp, msize);
215 xsize = msize;
216 }
217
218 tp = rp; rp = xp; xp = tp;
219 rsize = xsize;
220
221 if( (mpi_limb_signed_t)e < 0 ) {
222 /*mpih_mul( xp, rp, rsize, bp, bsize );*/
223 if( bsize < KARATSUBA_THRESHOLD ) {
224 _gcry_mpih_mul( xp, rp, rsize, bp, bsize );
225 }
226 else {
227 _gcry_mpih_mul_karatsuba_case(
228 xp, rp, rsize, bp, bsize, &karactx );
229 }
230
231 xsize = rsize + bsize;
232 if( xsize > msize ) {
233 _gcry_mpih_divrem(xp + msize, 0, xp, xsize, mp, msize);
234 xsize = msize;
235 }
236
237 tp = rp; rp = xp; xp = tp;
238 rsize = xsize;
239 }
240 e <<= 1;
241 c--;
242 }
243
244 i--;
245 if( i < 0 )
246 break;
247 e = ep[i];
248 c = BITS_PER_MPI_LIMB;
249 }
250
251 /* We shifted MOD, the modulo reduction argument, left MOD_SHIFT_CNT
252 * steps. Adjust the result by reducing it with the original MOD.
253 *
254 * Also make sure the result is put in RES->d (where it already
255 * might be, see above).
256 */
257 if( mod_shift_cnt ) {
258 carry_limb = _gcry_mpih_lshift( res->d, rp, rsize, mod_shift_cnt);
259 rp = res->d;
260 if( carry_limb ) {
261 rp[rsize] = carry_limb;
262 rsize++;
263 }
264 }
265 else {
266 MPN_COPY( res->d, rp, rsize);
267 rp = res->d;
268 }
269
270 if( rsize >= msize ) {
271 _gcry_mpih_divrem(rp + msize, 0, rp, rsize, mp, msize);
272 rsize = msize;
273 }
274
275 /* Remove any leading zero words from the result. */
276 if( mod_shift_cnt )
277 _gcry_mpih_rshift( rp, rp, rsize, mod_shift_cnt);
278 MPN_NORMALIZE (rp, rsize);
279
280 _gcry_mpih_release_karatsuba_ctx( &karactx );
281 }
282
283 if( negative_result && rsize ) {
284 if( mod_shift_cnt )
285 _gcry_mpih_rshift( mp, mp, msize, mod_shift_cnt);
286 _gcry_mpih_sub( rp, mp, msize, rp, rsize);
287 rsize = msize;
288 rsign = msign;
289 MPN_NORMALIZE(rp, rsize);
290 }
291 res->nlimbs = rsize;
292 res->sign = rsign;
293
294 leave:
295 if( assign_rp ) _gcry_mpi_assign_limb_space( res, rp, size );
296 if( mp_marker ) _gcry_mpi_free_limb_space( mp_marker, mp_nlimbs );
297 if( bp_marker ) _gcry_mpi_free_limb_space( bp_marker, bp_nlimbs );
298 if( ep_marker ) _gcry_mpi_free_limb_space( ep_marker, ep_nlimbs );
299 if( xp_marker ) _gcry_mpi_free_limb_space( xp_marker, xp_nlimbs );
300 if( tspace ) _gcry_mpi_free_limb_space( tspace, 0 );
301}
302
diff --git a/pwmanager/libcrypt/mpi/mpi-scan.c b/pwmanager/libcrypt/mpi/mpi-scan.c
new file mode 100644
index 0000000..90699cd
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/mpi-scan.c
@@ -0,0 +1,132 @@
1/* mpi-scan.c - MPI functions
2 *Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <config.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include "mpi-internal.h"
25#include "longlong.h"
26
27/****************
28 * Scan through an mpi and return byte for byte. a -1 is returned to indicate
29 * the end of the mpi. Scanning is done from the lsb to the msb, returned
30 * values are in the range of 0 .. 255.
31 *
32 * FIXME: This code is VERY ugly!
33 */
34int
35_gcry_mpi_getbyte( gcry_mpi_t a, unsigned idx )
36{
37 int i, j;
38 unsigned n;
39 mpi_ptr_t ap;
40 mpi_limb_t limb;
41
42 ap = a->d;
43 for(n=0,i=0; i < a->nlimbs; i++ ) {
44 limb = ap[i];
45 for( j=0; j < BYTES_PER_MPI_LIMB; j++, n++ )
46 if( n == idx )
47 return (limb >> j*8) & 0xff;
48 }
49 return -1;
50}
51
52
53/****************
54 * Put a value at position IDX into A. idx counts from lsb to msb
55 */
56void
57_gcry_mpi_putbyte( gcry_mpi_t a, unsigned idx, int xc )
58{
59 int i, j;
60 unsigned n;
61 mpi_ptr_t ap;
62 mpi_limb_t limb, c;
63
64 c = xc & 0xff;
65 ap = a->d;
66 for(n=0,i=0; i < a->alloced; i++ ) {
67 limb = ap[i];
68 for( j=0; j < BYTES_PER_MPI_LIMB; j++, n++ )
69 if( n == idx ) {
70 #if BYTES_PER_MPI_LIMB == 4
71 if( j == 0 )
72 limb = (limb & 0xffffff00) | c;
73 else if( j == 1 )
74 limb = (limb & 0xffff00ff) | (c<<8);
75 else if( j == 2 )
76 limb = (limb & 0xff00ffff) | (c<<16);
77 else
78 limb = (limb & 0x00ffffff) | (c<<24);
79 #elif BYTES_PER_MPI_LIMB == 8
80 if( j == 0 )
81 limb = (limb & 0xffffffffffffff00) | c;
82 else if( j == 1 )
83 limb = (limb & 0xffffffffffff00ff) | (c<<8);
84 else if( j == 2 )
85 limb = (limb & 0xffffffffff00ffff) | (c<<16);
86 else if( j == 3 )
87 limb = (limb & 0xffffffff00ffffff) | (c<<24);
88 else if( j == 4 )
89 limb = (limb & 0xffffff00ffffffff) | (c<<32);
90 else if( j == 5 )
91 limb = (limb & 0xffff00ffffffffff) | (c<<40);
92 else if( j == 6 )
93 limb = (limb & 0xff00ffffffffffff) | (c<<48);
94 else
95 limb = (limb & 0x00ffffffffffffff) | (c<<56);
96 #else
97 #error please enhance this function, its ugly - i know.
98 #endif
99 if( a->nlimbs <= i )
100 a->nlimbs = i+1;
101 ap[i] = limb;
102 return;
103 }
104 }
105 abort(); /* index out of range */
106}
107
108
109/****************
110 * Count the number of zerobits at the low end of A
111 */
112unsigned
113_gcry_mpi_trailing_zeros( gcry_mpi_t a )
114{
115 unsigned n, count = 0;
116
117 for(n=0; n < a->nlimbs; n++ ) {
118 if( a->d[n] ) {
119 unsigned nn;
120 mpi_limb_t alimb = a->d[n];
121
122 count_trailing_zeros( nn, alimb );
123 count += nn;
124 break;
125 }
126 count += BITS_PER_MPI_LIMB;
127 }
128 return count;
129
130}
131
132
diff --git a/pwmanager/libcrypt/mpi/mpi.pro b/pwmanager/libcrypt/mpi/mpi.pro
new file mode 100644
index 0000000..cf750ec
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/mpi.pro
@@ -0,0 +1,42 @@
1######################################################################
2# Automatically generated by qmake (1.07a) Tue Oct 19 15:37:52 2004
3######################################################################
4
5TEMPLATE = lib
6CONFIG += staticlib
7DEPENDPATH += generic
8INCLUDEPATH += . .. ../crypt
9OBJECTS_DIR = obj/$(PLATFORM)
10MOC_DIR = moc/$(PLATFORM)
11DESTDIR= ../$(PLATFORM)
12 TARGET = kpmicrompi
13# Input
14HEADERS += asm-syntax.h \
15 longlong.h \
16 generic/mpi-asm-defs.h \
17 mpi-inline.h \
18 mpi-internal.h \
19 sysdep.h
20SOURCES += mpi-add.c \
21 mpi-bit.c \
22 mpi-cmp.c \
23 mpi-div.c \
24 mpi-gcd.c \
25 mpi-inline.c \
26 mpi-inv.c \
27 mpi-mpow.c \
28 mpi-mul.c \
29 mpi-pow.c \
30 mpi-scan.c \
31 mpicoder.c \
32 mpih-div.c \
33 mpih-mul.c \
34 mpiutil.c \
35 generic/mpih-add1.c \
36 generic/mpih-lshift.c \
37 generic/mpih-mul1.c \
38 generic/mpih-mul2.c \
39 generic/mpih-mul3.c \
40 generic/mpih-rshift.c \
41 generic/mpih-sub1.c \
42 generic/udiv-w-sdiv.c
diff --git a/pwmanager/libcrypt/mpi/mpicoder.c b/pwmanager/libcrypt/mpi/mpicoder.c
new file mode 100644
index 0000000..d501fe4
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/mpicoder.c
@@ -0,0 +1,643 @@
1/* mpicoder.c - Coder for the external representation of MPIs
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002,
3 * 2003 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 */
21
22#include <config.h>
23#include <stdio.h>
24#include <string.h>
25#include <stdlib.h>
26#include <assert.h>
27
28#include "mpi-internal.h"
29#include "g10lib.h"
30
31#define MAX_EXTERN_MPI_BITS 16384
32
33
34static gcry_mpi_t
35mpi_read_from_buffer(byte *buffer, unsigned *ret_nread, int secure)
36{
37 int i, j;
38 unsigned int nbits, nbytes, nlimbs, nread=0;
39 mpi_limb_t a;
40 gcry_mpi_t val = MPI_NULL;
41
42 if( *ret_nread < 2 )
43 goto leave;
44 nbits = buffer[0] << 8 | buffer[1];
45 if( nbits > MAX_EXTERN_MPI_BITS ) {
46 log_error("mpi too large (%u bits)\n", nbits);
47 goto leave;
48 }
49 else if( !nbits ) {
50 log_error("an mpi of size 0 is not allowed\n");
51 goto leave;
52 }
53 buffer += 2;
54 nread = 2;
55
56 nbytes = (nbits+7) / 8;
57 nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
58 val = secure? mpi_alloc_secure( nlimbs )
59 : mpi_alloc( nlimbs );
60 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
61 i %= BYTES_PER_MPI_LIMB;
62 j= val->nlimbs = nlimbs;
63 val->sign = 0;
64 for( ; j > 0; j-- ) {
65 a = 0;
66 for(; i < BYTES_PER_MPI_LIMB; i++ ) {
67 if( ++nread > *ret_nread )
68 log_bug("mpi larger than buffer");
69 a <<= 8;
70 a |= *buffer++;
71 }
72 i = 0;
73 val->d[j-1] = a;
74 }
75
76 leave:
77 *ret_nread = nread;
78 return val;
79}
80
81
82/****************
83 * Make an mpi from a hex character string.
84 */
85static int
86mpi_fromstr(gcry_mpi_t val, const char *str)
87{
88 int sign=0, prepend_zero=0, i, j, c, c1, c2;
89 unsigned nbits, nbytes, nlimbs;
90 mpi_limb_t a;
91
92 if( *str == '-' ) {
93 sign = 1;
94 str++;
95 }
96
97 /* skip optional hex prefix */
98 if ( *str == '0' && str[1] == 'x' ) {
99 str += 2;
100 }
101
102 nbits = strlen(str)*4;
103 if( nbits % 8 )
104 prepend_zero = 1;
105 nbytes = (nbits+7) / 8;
106 nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB;
107 if( val->alloced < nlimbs )
108 mpi_resize(val, nlimbs );
109 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
110 i %= BYTES_PER_MPI_LIMB;
111 j= val->nlimbs = nlimbs;
112 val->sign = sign;
113 for( ; j > 0; j-- ) {
114 a = 0;
115 for(; i < BYTES_PER_MPI_LIMB; i++ ) {
116 if( prepend_zero ) {
117 c1 = '0';
118 prepend_zero = 0;
119 }
120 else
121 c1 = *str++;
122 assert(c1);
123 c2 = *str++;
124 assert(c2);
125 if( c1 >= '0' && c1 <= '9' )
126 c = c1 - '0';
127 else if( c1 >= 'a' && c1 <= 'f' )
128 c = c1 - 'a' + 10;
129 else if( c1 >= 'A' && c1 <= 'F' )
130 c = c1 - 'A' + 10;
131 else {
132 mpi_clear(val);
133 return 1;
134 }
135 c <<= 4;
136 if( c2 >= '0' && c2 <= '9' )
137 c |= c2 - '0';
138 else if( c2 >= 'a' && c2 <= 'f' )
139 c |= c2 - 'a' + 10;
140 else if( c2 >= 'A' && c2 <= 'F' )
141 c |= c2 - 'A' + 10;
142 else {
143 mpi_clear(val);
144 return 1;
145 }
146 a <<= 8;
147 a |= c;
148 }
149 i = 0;
150 val->d[j-1] = a;
151 }
152
153 return 0;
154}
155
156
157/* Dump the value of A in a format suitable for debugging to
158 Libgcrypt's logging stream. Note that one leading space but no
159 trailing space or linefeed will be printed. It is okay to pass
160 NULL for A. */
161void
162gcry_mpi_dump (const gcry_mpi_t a)
163{
164 int i;
165
166 log_printf (" ");
167 if (!a)
168 log_printf ("[MPI_NULL]");
169 else
170 {
171 if (a->sign)
172 log_printf ( "-");
173#if BYTES_PER_MPI_LIMB == 2
174# define X "4"
175#elif BYTES_PER_MPI_LIMB == 4
176# define X "8"
177#elif BYTES_PER_MPI_LIMB == 8
178# define X "16"
179#elif BYTES_PER_MPI_LIMB == 16
180# define X "32"
181#else
182# error please define the format here
183#endif
184 for (i=a->nlimbs; i > 0 ; i-- )
185 {
186 log_printf (i != a->nlimbs? "%0" X "lX":"%lX", (ulong)a->d[i-1]);
187 }
188#undef X
189 if (!a->nlimbs)
190 log_printf ("0");
191 }
192}
193
194/* Convience function used internally. */
195void
196_gcry_log_mpidump (const char *text, gcry_mpi_t a)
197{
198 log_printf ("%s:", text);
199 gcry_mpi_dump (a);
200 log_printf ("\n");
201}
202
203
204/****************
205 * Return an m_alloced buffer with the MPI (msb first).
206 * NBYTES receives the length of this buffer. Caller must free the
207 * return string (This function does return a 0 byte buffer with NBYTES
208 * set to zero if the value of A is zero. If sign is not NULL, it will
209 * be set to the sign of the A.
210 */
211static byte *
212do_get_buffer( gcry_mpi_t a, unsigned *nbytes, int *sign, int force_secure )
213{
214 byte *p, *buffer;
215 mpi_limb_t alimb;
216 int i;
217 size_t n;
218
219 if( sign )
220 *sign = a->sign;
221 *nbytes = a->nlimbs * BYTES_PER_MPI_LIMB;
222 n = *nbytes? *nbytes:1; /* allocate at least one byte */
223 p = buffer = force_secure || mpi_is_secure(a) ? gcry_xmalloc_secure(n)
224 : gcry_xmalloc(n);
225
226 for(i=a->nlimbs-1; i >= 0; i-- ) {
227 alimb = a->d[i];
228#if BYTES_PER_MPI_LIMB == 4
229 *p++ = alimb >> 24;
230 *p++ = alimb >> 16;
231 *p++ = alimb >> 8;
232 *p++ = alimb ;
233#elif BYTES_PER_MPI_LIMB == 8
234 *p++ = alimb >> 56;
235 *p++ = alimb >> 48;
236 *p++ = alimb >> 40;
237 *p++ = alimb >> 32;
238 *p++ = alimb >> 24;
239 *p++ = alimb >> 16;
240 *p++ = alimb >> 8;
241 *p++ = alimb ;
242#else
243 #error please implement for this limb size.
244#endif
245 }
246
247 /* this is sub-optimal but we need to do the shift oepration because
248 * the caller has to free the returned buffer */
249 for(p=buffer; !*p && *nbytes; p++, --*nbytes )
250 ;
251 if( p != buffer )
252 memmove(buffer,p, *nbytes);
253 return buffer;
254}
255
256
257byte *
258_gcry_mpi_get_buffer( gcry_mpi_t a, unsigned *nbytes, int *sign )
259{
260 return do_get_buffer( a, nbytes, sign, 0 );
261}
262
263byte *
264_gcry_mpi_get_secure_buffer( gcry_mpi_t a, unsigned *nbytes, int *sign )
265{
266 return do_get_buffer( a, nbytes, sign, 1 );
267}
268
269/****************
270 * Use BUFFER to update MPI.
271 */
272void
273_gcry_mpi_set_buffer( gcry_mpi_t a, const byte *buffer, unsigned nbytes, int sign )
274{
275 const byte *p;
276 mpi_limb_t alimb;
277 int nlimbs;
278 int i;
279
280 nlimbs = (nbytes + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB;
281 RESIZE_IF_NEEDED(a, nlimbs);
282 a->sign = sign;
283
284 for(i=0, p = buffer+nbytes-1; p >= buffer+BYTES_PER_MPI_LIMB; ) {
285#if BYTES_PER_MPI_LIMB == 4
286 alimb = *p-- ;
287 alimb |= *p-- << 8 ;
288 alimb |= *p-- << 16 ;
289 alimb |= *p-- << 24 ;
290#elif BYTES_PER_MPI_LIMB == 8
291 alimb = (mpi_limb_t)*p--;
292 alimb |= (mpi_limb_t)*p-- << 8 ;
293 alimb |= (mpi_limb_t)*p-- << 16 ;
294 alimb |= (mpi_limb_t)*p-- << 24 ;
295 alimb |= (mpi_limb_t)*p-- << 32 ;
296 alimb |= (mpi_limb_t)*p-- << 40 ;
297 alimb |= (mpi_limb_t)*p-- << 48 ;
298 alimb |= (mpi_limb_t)*p-- << 56 ;
299#else
300 #error please implement for this limb size.
301#endif
302 a->d[i++] = alimb;
303 }
304 if( p >= buffer ) {
305#if BYTES_PER_MPI_LIMB == 4
306 alimb = *p-- ;
307 if( p >= buffer ) alimb |= *p-- << 8 ;
308 if( p >= buffer ) alimb |= *p-- << 16 ;
309 if( p >= buffer ) alimb |= *p-- << 24 ;
310#elif BYTES_PER_MPI_LIMB == 8
311 alimb = (mpi_limb_t)*p-- ;
312 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- <<8 ;
313 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 16 ;
314 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 24 ;
315 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 32 ;
316 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 40 ;
317 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 48 ;
318 if( p >= buffer ) alimb |= (mpi_limb_t)*p-- << 56 ;
319#else
320 #error please implement for this limb size.
321#endif
322 a->d[i++] = alimb;
323 }
324 a->nlimbs = i;
325 assert( i == nlimbs );
326}
327
328
329
330/* Convert the external representation of an integer stored in BUFFER
331 with a length of BUFLEN into a newly create MPI returned in
332 RET_MPI. If NBYTES is not NULL, it will receive the number of
333 bytes actually scanned after a successful operation. */
334gcry_error_t
335gcry_mpi_scan( struct gcry_mpi **ret_mpi, enum gcry_mpi_format format,
336 const unsigned char *buffer, size_t buflen, size_t *nscanned )
337{
338 struct gcry_mpi *a = NULL;
339 unsigned int len;
340 int secure = (buffer && gcry_is_secure (buffer));
341
342 if (format == GCRYMPI_FMT_SSH)
343 len = 0;
344 else
345 len = buflen;
346
347 if( format == GCRYMPI_FMT_STD ) {
348 const byte *s = buffer;
349
350 a = secure? mpi_alloc_secure ((len+BYTES_PER_MPI_LIMB-1)
351 /BYTES_PER_MPI_LIMB)
352 : mpi_alloc ((len+BYTES_PER_MPI_LIMB-1)/BYTES_PER_MPI_LIMB);
353 if( len ) { /* not zero */
354 a->sign = *s & 0x80;
355 if( a->sign ) {
356 /* FIXME: we have to convert from 2compl to magnitude format */
357 mpi_free(a);
358 return gcry_error (GPG_ERR_INTERNAL);
359 }
360 else
361 _gcry_mpi_set_buffer( a, s, len, 0 );
362 }
363 if( ret_mpi ) {
364 mpi_normalize ( a );
365 *ret_mpi = a;
366 }
367 else
368 mpi_free(a);
369 return gcry_error (GPG_ERR_NO_ERROR);
370 }
371 else if( format == GCRYMPI_FMT_USG ) {
372 a = secure? mpi_alloc_secure ((len+BYTES_PER_MPI_LIMB-1)
373 /BYTES_PER_MPI_LIMB)
374 : mpi_alloc ((len+BYTES_PER_MPI_LIMB-1)/BYTES_PER_MPI_LIMB);
375
376 if( len ) /* not zero */
377 _gcry_mpi_set_buffer( a, buffer, len, 0 );
378 if( ret_mpi ) {
379 mpi_normalize ( a );
380 *ret_mpi = a;
381 }
382 else
383 mpi_free(a);
384 return gcry_error (GPG_ERR_NO_ERROR);
385 }
386 else if( format == GCRYMPI_FMT_PGP ) {
387 a = mpi_read_from_buffer( (char*)buffer, &len, secure);
388 if( nscanned )
389 *nscanned = len;
390 if( ret_mpi && a ) {
391 mpi_normalize ( a );
392 *ret_mpi = a;
393 }
394 else
395 mpi_free(a);
396 return gcry_error (a ? GPG_ERR_NO_ERROR : GPG_ERR_INV_OBJ);
397 }
398 else if( format == GCRYMPI_FMT_SSH ) {
399 const byte *s = buffer;
400 size_t n;
401
402 if( len && len < 4 )
403 return gcry_error (GPG_ERR_TOO_SHORT);
404 n = s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
405 s += 4;
406 if (len)
407 len -= 4;
408 if( len && n > len )
409 return gcry_error (GPG_ERR_TOO_LARGE); /* or should it be
410 too_short */
411
412 a = secure? mpi_alloc_secure ((n+BYTES_PER_MPI_LIMB-1)
413 /BYTES_PER_MPI_LIMB)
414 : mpi_alloc ((n+BYTES_PER_MPI_LIMB-1)/BYTES_PER_MPI_LIMB);
415 if( n ) { /* not zero */
416 a->sign = *s & 0x80;
417 if( a->sign ) {
418 /* FIXME: we have to convert from 2compl to magnitude format */
419 mpi_free(a);
420 return gcry_error (GPG_ERR_INTERNAL);
421 }
422 else
423 _gcry_mpi_set_buffer( a, s, n, 0 );
424 }
425 if( nscanned )
426 *nscanned = n+4;
427 if( ret_mpi ) {
428 mpi_normalize ( a );
429 *ret_mpi = a;
430 }
431 else
432 mpi_free(a);
433 return gcry_error (GPG_ERR_NO_ERROR);
434 }
435 else if( format == GCRYMPI_FMT_HEX ) {
436 if( buflen )
437 return gcry_error (GPG_ERR_INV_ARG); /* can only handle C
438 strings for now */
439 a = secure? mpi_alloc_secure (0) : mpi_alloc(0);
440 if( mpi_fromstr( a, buffer ) )
441 return gcry_error (GPG_ERR_INV_OBJ);
442 if( ret_mpi ) {
443 mpi_normalize ( a );
444 *ret_mpi = a;
445 }
446 else
447 mpi_free(a);
448 return gcry_error (GPG_ERR_NO_ERROR);
449 }
450 else
451 return gcry_error (GPG_ERR_INV_ARG);
452}
453
454/* Convert the big integer A into the external representation
455 described by FORMAT and store it in the provided BUFFER which has
456 been allocated by the user with a size of BUFLEN bytes. NWRITTEN
457 receives the actual length of the external representation unless it
458 has been passed as NULL. BUFFER may be NULL to query the required
459 length.*/
460gcry_error_t
461gcry_mpi_print( enum gcry_mpi_format format,
462 unsigned char *buffer, size_t buflen,
463 size_t *nwritten, struct gcry_mpi *a)
464{
465 unsigned int nbits = mpi_get_nbits(a);
466 size_t len;
467 size_t dummy_nwritten;
468
469 if (!nwritten)
470 nwritten = &dummy_nwritten;
471
472 len = buflen;
473 *nwritten = 0;
474 if( format == GCRYMPI_FMT_STD ) {
475 char *tmp;
476 int extra = 0;
477 unsigned int n;
478
479 if( a->sign )
480 return gcry_error (GPG_ERR_INTERNAL); /* can't handle it yet */
481
482 tmp = _gcry_mpi_get_buffer( a, &n, NULL );
483 if( n && (*tmp & 0x80) ) {
484 n++;
485 extra=1;
486 }
487
488 if (buffer && n > len) {
489 gcry_free(tmp);
490 return gcry_error (GPG_ERR_TOO_SHORT); /* the provided buffer is too short */
491 }
492 if( buffer ) {
493 byte *s = buffer;
494 if( extra )
495 *s++ = 0;
496
497 memcpy( s, tmp, n-extra );
498 }
499 gcry_free(tmp);
500 *nwritten = n;
501 return gcry_error (GPG_ERR_NO_ERROR);
502 }
503 else if( format == GCRYMPI_FMT_USG ) {
504 unsigned int n = (nbits + 7)/8;
505
506 /* we ignore the sign for this format */
507 /* FIXME: for performance reasons we should put this into
508 * mpi_aprint becuase we can then use the buffer directly */
509 if (buffer && n > len)
510 return gcry_error (GPG_ERR_TOO_SHORT); /* the provided buffer is too short */
511 if( buffer ) {
512 char *tmp;
513 tmp = _gcry_mpi_get_buffer( a, &n, NULL );
514 memcpy( buffer, tmp, n );
515 gcry_free(tmp);
516 }
517 *nwritten = n;
518 return gcry_error (GPG_ERR_NO_ERROR);
519 }
520 else if( format == GCRYMPI_FMT_PGP ) {
521 unsigned int n = (nbits + 7)/8;
522
523 if( a->sign )
524 return gcry_error (GPG_ERR_INV_ARG); /* pgp format can only handle unsigned */
525
526 if (buffer && n+2 > len)
527 return gcry_error (GPG_ERR_TOO_SHORT); /* the provided buffer is too short */
528 if( buffer ) {
529 char *tmp;
530 byte *s = buffer;
531 s[0] = nbits >> 8;
532 s[1] = nbits;
533
534 tmp = _gcry_mpi_get_buffer( a, &n, NULL );
535 memcpy( s+2, tmp, n );
536 gcry_free(tmp);
537 }
538 *nwritten = n+2;
539 return gcry_error (GPG_ERR_NO_ERROR);
540 }
541 else if( format == GCRYMPI_FMT_SSH ) {
542 char *tmp;
543 int extra = 0;
544 unsigned int n;
545
546 if( a->sign )
547 return gcry_error (GPG_ERR_INTERNAL); /* can't handle it yet */
548
549 tmp = _gcry_mpi_get_buffer( a, &n, NULL );
550 if( n && (*tmp & 0x80) ) {
551 n++;
552 extra=1;
553 }
554
555 if (buffer && n+4 > len) {
556 gcry_free(tmp);
557 return gcry_error (GPG_ERR_TOO_SHORT); /* the provided buffer is too short */
558 }
559 if( buffer ) {
560 byte *s = buffer;
561 *s++ = n >> 24;
562 *s++ = n >> 16;
563 *s++ = n >> 8;
564 *s++ = n;
565 if( extra )
566 *s++ = 0;
567
568 memcpy( s, tmp, n-extra );
569 }
570 gcry_free(tmp);
571 *nwritten = 4+n;
572 return gcry_error (GPG_ERR_NO_ERROR);
573 }
574 else if( format == GCRYMPI_FMT_HEX ) {
575 byte *tmp;
576 int i;
577 int extra = 0;
578 unsigned int n=0;
579
580 tmp = _gcry_mpi_get_buffer( a, &n, NULL );
581 if( !n || (*tmp & 0x80) )
582 extra=2;
583
584 if(buffer && 2*n + extra + !!a->sign + 1 > len) {
585 gcry_free(tmp);
586 return gcry_error (GPG_ERR_TOO_SHORT); /* the provided buffer is too short */
587 }
588 if( buffer ) {
589 byte *s = buffer;
590 if( a->sign )
591 *s++ = '-';
592 if( extra ) {
593 *s++ = '0';
594 *s++ = '0';
595 }
596
597 for(i=0; i < n; i++ ) {
598 unsigned int c = tmp[i];
599 *s++ = (c >> 4) < 10? '0'+(c>>4) : 'A'+(c>>4)-10 ;
600 c &= 15;
601 *s++ = c < 10? '0'+c : 'A'+c-10 ;
602 }
603 *s++ = 0;
604 *nwritten = s - buffer;
605 }
606 else {
607 *nwritten = 2*n + extra + !!a->sign + 1;
608 }
609 gcry_free(tmp);
610 return gcry_error (GPG_ERR_NO_ERROR);
611 }
612 else
613 return gcry_error (GPG_ERR_INV_ARG);
614}
615
616/****************
617 * Like gcry_mpi_print but this function allocates the buffer itself.
618 * The caller has to supply the address of a pointer. NWRITTEN may be
619 * NULL.
620 */
621gcry_error_t
622gcry_mpi_aprint( enum gcry_mpi_format format,
623 unsigned char **buffer, size_t *nwritten,
624 struct gcry_mpi *a )
625{
626 size_t n;
627 gcry_error_t rc;
628
629 *buffer = NULL;
630 rc = gcry_mpi_print( format, NULL, 0, &n, a );
631 if( rc )
632 return rc;
633 *buffer = mpi_is_secure(a) ? gcry_xmalloc_secure( n ) : gcry_xmalloc( n );
634 rc = gcry_mpi_print( format, *buffer, n, &n, a );
635 if( rc ) {
636 gcry_free(*buffer);
637 *buffer = NULL;
638 }
639 else if( nwritten )
640 *nwritten = n;
641 return rc;
642}
643
diff --git a/pwmanager/libcrypt/mpi/mpih-div.c b/pwmanager/libcrypt/mpi/mpih-div.c
new file mode 100644
index 0000000..e41e205
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/mpih-div.c
@@ -0,0 +1,535 @@
1/* mpih-div.c - MPI helper functions
2 * Copyright (C) 1994, 1996, 1998, 2000,
3 * 2001, 2002 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 *
21 * Note: This code is heavily based on the GNU MP Library.
22 * Actually it's the same code with only minor changes in the
23 * way the data is stored; this is to support the abstraction
24 * of an optional secure memory allocation which may be used
25 * to avoid revealing of sensitive data due to paging etc.
26 */
27
28#include <config.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include "mpi-internal.h"
32#include "longlong.h"
33
34#ifndef UMUL_TIME
35#define UMUL_TIME 1
36#endif
37#ifndef UDIV_TIME
38#define UDIV_TIME UMUL_TIME
39#endif
40
41/* FIXME: We should be using invert_limb (or invert_normalized_limb)
42 * here (not udiv_qrnnd).
43 */
44
45mpi_limb_t
46_gcry_mpih_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
47 mpi_limb_t divisor_limb)
48{
49 mpi_size_t i;
50 mpi_limb_t n1, n0, r;
51 int dummy;
52
53 /* Botch: Should this be handled at all? Rely on callers?*/
54 if( !dividend_size )
55 return 0;
56
57 /* If multiplication is much faster than division, and the
58 * dividend is large, pre-invert the divisor, and use
59 * only multiplications in the inner loop.
60 *
61 * This test should be read:
62 * Does it ever help to use udiv_qrnnd_preinv?
63 * && Does what we save compensate for the inversion overhead?
64 */
65 if( UDIV_TIME > (2 * UMUL_TIME + 6)
66 && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME ) {
67 int normalization_steps;
68
69 count_leading_zeros( normalization_steps, divisor_limb );
70 if( normalization_steps ) {
71 mpi_limb_t divisor_limb_inverted;
72
73 divisor_limb <<= normalization_steps;
74
75 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
76 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
77 * most significant bit (with weight 2**N) implicit.
78 *
79 * Special case for DIVISOR_LIMB == 100...000.
80 */
81 if( !(divisor_limb << 1) )
82 divisor_limb_inverted = ~(mpi_limb_t)0;
83 else
84 udiv_qrnnd(divisor_limb_inverted, dummy,
85 -divisor_limb, 0, divisor_limb);
86
87 n1 = dividend_ptr[dividend_size - 1];
88 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
89
90 /* Possible optimization:
91 * if (r == 0
92 * && divisor_limb > ((n1 << normalization_steps)
93 * | (dividend_ptr[dividend_size - 2] >> ...)))
94 * ...one division less...
95 */
96 for( i = dividend_size - 2; i >= 0; i--) {
97 n0 = dividend_ptr[i];
98 UDIV_QRNND_PREINV(dummy, r, r,
99 ((n1 << normalization_steps)
100 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
101 divisor_limb, divisor_limb_inverted);
102 n1 = n0;
103 }
104 UDIV_QRNND_PREINV(dummy, r, r,
105 n1 << normalization_steps,
106 divisor_limb, divisor_limb_inverted);
107 return r >> normalization_steps;
108 }
109 else {
110 mpi_limb_t divisor_limb_inverted;
111
112 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
113 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
114 * most significant bit (with weight 2**N) implicit.
115 *
116 * Special case for DIVISOR_LIMB == 100...000.
117 */
118 if( !(divisor_limb << 1) )
119 divisor_limb_inverted = ~(mpi_limb_t)0;
120 else
121 udiv_qrnnd(divisor_limb_inverted, dummy,
122 -divisor_limb, 0, divisor_limb);
123
124 i = dividend_size - 1;
125 r = dividend_ptr[i];
126
127 if( r >= divisor_limb )
128 r = 0;
129 else
130 i--;
131
132 for( ; i >= 0; i--) {
133 n0 = dividend_ptr[i];
134 UDIV_QRNND_PREINV(dummy, r, r,
135 n0, divisor_limb, divisor_limb_inverted);
136 }
137 return r;
138 }
139 }
140 else {
141 if( UDIV_NEEDS_NORMALIZATION ) {
142 int normalization_steps;
143
144 count_leading_zeros(normalization_steps, divisor_limb);
145 if( normalization_steps ) {
146 divisor_limb <<= normalization_steps;
147
148 n1 = dividend_ptr[dividend_size - 1];
149 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
150
151 /* Possible optimization:
152 * if (r == 0
153 * && divisor_limb > ((n1 << normalization_steps)
154 * | (dividend_ptr[dividend_size - 2] >> ...)))
155 * ...one division less...
156 */
157 for(i = dividend_size - 2; i >= 0; i--) {
158 n0 = dividend_ptr[i];
159 udiv_qrnnd (dummy, r, r,
160 ((n1 << normalization_steps)
161 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
162 divisor_limb);
163 n1 = n0;
164 }
165 udiv_qrnnd (dummy, r, r,
166 n1 << normalization_steps,
167 divisor_limb);
168 return r >> normalization_steps;
169 }
170 }
171 /* No normalization needed, either because udiv_qrnnd doesn't require
172 * it, or because DIVISOR_LIMB is already normalized. */
173 i = dividend_size - 1;
174 r = dividend_ptr[i];
175
176 if(r >= divisor_limb)
177 r = 0;
178 else
179 i--;
180
181 for(; i >= 0; i--) {
182 n0 = dividend_ptr[i];
183 udiv_qrnnd (dummy, r, r, n0, divisor_limb);
184 }
185 return r;
186 }
187}
188
189/* Divide num (NP/NSIZE) by den (DP/DSIZE) and write
190 * the NSIZE-DSIZE least significant quotient limbs at QP
191 * and the DSIZE long remainder at NP.If QEXTRA_LIMBS is
192 * non-zero, generate that many fraction bits and append them after the
193 * other quotient limbs.
194 * Return the most significant limb of the quotient, this is always 0 or 1.
195 *
196 * Preconditions:
197 * 0. NSIZE >= DSIZE.
198 * 1. The most significant bit of the divisor must be set.
199 * 2. QP must either not overlap with the input operands at all, or
200 * QP + DSIZE >= NP must hold true.(This means that it's
201 * possible to put the quotient in the high part of NUM, right after the
202 * remainder in NUM.
203 * 3. NSIZE >= DSIZE, even if QEXTRA_LIMBS is non-zero.
204 */
205
206mpi_limb_t
207_gcry_mpih_divrem( mpi_ptr_t qp, mpi_size_t qextra_limbs,
208 mpi_ptr_t np, mpi_size_t nsize,
209 mpi_ptr_t dp, mpi_size_t dsize)
210{
211 mpi_limb_t most_significant_q_limb = 0;
212
213 switch(dsize) {
214 case 0:
215 /* We are asked to divide by zero, so go ahead and do it! (To make
216 the compiler not remove this statement, return the value.) */
217 return 1 / dsize;
218
219 case 1:
220 {
221 mpi_size_t i;
222 mpi_limb_t n1;
223 mpi_limb_t d;
224
225 d = dp[0];
226 n1 = np[nsize - 1];
227
228 if( n1 >= d ) {
229 n1 -= d;
230 most_significant_q_limb = 1;
231 }
232
233 qp += qextra_limbs;
234 for( i = nsize - 2; i >= 0; i--)
235 udiv_qrnnd( qp[i], n1, n1, np[i], d );
236 qp -= qextra_limbs;
237
238 for( i = qextra_limbs - 1; i >= 0; i-- )
239 udiv_qrnnd (qp[i], n1, n1, 0, d);
240
241 np[0] = n1;
242 }
243 break;
244
245 case 2:
246 {
247 mpi_size_t i;
248 mpi_limb_t n1, n0, n2;
249 mpi_limb_t d1, d0;
250
251 np += nsize - 2;
252 d1 = dp[1];
253 d0 = dp[0];
254 n1 = np[1];
255 n0 = np[0];
256
257 if( n1 >= d1 && (n1 > d1 || n0 >= d0) ) {
258 sub_ddmmss (n1, n0, n1, n0, d1, d0);
259 most_significant_q_limb = 1;
260 }
261
262 for( i = qextra_limbs + nsize - 2 - 1; i >= 0; i-- ) {
263 mpi_limb_t q;
264 mpi_limb_t r;
265
266 if( i >= qextra_limbs )
267 np--;
268 else
269 np[0] = 0;
270
271 if( n1 == d1 ) {
272 /* Q should be either 111..111 or 111..110. Need special
273 * treatment of this rare case as normal division would
274 * give overflow. */
275 q = ~(mpi_limb_t)0;
276
277 r = n0 + d1;
278 if( r < d1 ) { /* Carry in the addition? */
279 add_ssaaaa( n1, n0, r - d0, np[0], 0, d0 );
280 qp[i] = q;
281 continue;
282 }
283 n1 = d0 - (d0 != 0?1:0);
284 n0 = -d0;
285 }
286 else {
287 udiv_qrnnd (q, r, n1, n0, d1);
288 umul_ppmm (n1, n0, d0, q);
289 }
290
291 n2 = np[0];
292 q_test:
293 if( n1 > r || (n1 == r && n0 > n2) ) {
294 /* The estimated Q was too large. */
295 q--;
296 sub_ddmmss (n1, n0, n1, n0, 0, d0);
297 r += d1;
298 if( r >= d1 ) /* If not carry, test Q again. */
299 goto q_test;
300 }
301
302 qp[i] = q;
303 sub_ddmmss (n1, n0, r, n2, n1, n0);
304 }
305 np[1] = n1;
306 np[0] = n0;
307 }
308 break;
309
310 default:
311 {
312 mpi_size_t i;
313 mpi_limb_t dX, d1, n0;
314
315 np += nsize - dsize;
316 dX = dp[dsize - 1];
317 d1 = dp[dsize - 2];
318 n0 = np[dsize - 1];
319
320 if( n0 >= dX ) {
321 if(n0 > dX || _gcry_mpih_cmp(np, dp, dsize - 1) >= 0 ) {
322 _gcry_mpih_sub_n(np, np, dp, dsize);
323 n0 = np[dsize - 1];
324 most_significant_q_limb = 1;
325 }
326 }
327
328 for( i = qextra_limbs + nsize - dsize - 1; i >= 0; i--) {
329 mpi_limb_t q;
330 mpi_limb_t n1, n2;
331 mpi_limb_t cy_limb;
332
333 if( i >= qextra_limbs ) {
334 np--;
335 n2 = np[dsize];
336 }
337 else {
338 n2 = np[dsize - 1];
339 MPN_COPY_DECR (np + 1, np, dsize - 1);
340 np[0] = 0;
341 }
342
343 if( n0 == dX ) {
344 /* This might over-estimate q, but it's probably not worth
345 * the extra code here to find out. */
346 q = ~(mpi_limb_t)0;
347 }
348 else {
349 mpi_limb_t r;
350
351 udiv_qrnnd(q, r, n0, np[dsize - 1], dX);
352 umul_ppmm(n1, n0, d1, q);
353
354 while( n1 > r || (n1 == r && n0 > np[dsize - 2])) {
355 q--;
356 r += dX;
357 if( r < dX ) /* I.e. "carry in previous addition?" */
358 break;
359 n1 -= n0 < d1;
360 n0 -= d1;
361 }
362 }
363
364 /* Possible optimization: We already have (q * n0) and (1 * n1)
365 * after the calculation of q.Taking advantage of that, we
366 * could make this loop make two iterations less. */
367 cy_limb = _gcry_mpih_submul_1(np, dp, dsize, q);
368
369 if( n2 != cy_limb ) {
370 _gcry_mpih_add_n(np, np, dp, dsize);
371 q--;
372 }
373
374 qp[i] = q;
375 n0 = np[dsize - 1];
376 }
377 }
378 }
379
380 return most_significant_q_limb;
381}
382
383
384/****************
385 * Divide (DIVIDEND_PTR,,DIVIDEND_SIZE) by DIVISOR_LIMB.
386 * Write DIVIDEND_SIZE limbs of quotient at QUOT_PTR.
387 * Return the single-limb remainder.
388 * There are no constraints on the value of the divisor.
389 *
390 * QUOT_PTR and DIVIDEND_PTR might point to the same limb.
391 */
392
393mpi_limb_t
394_gcry_mpih_divmod_1( mpi_ptr_t quot_ptr,
395 mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
396 mpi_limb_t divisor_limb)
397{
398 mpi_size_t i;
399 mpi_limb_t n1, n0, r;
400 int dummy;
401
402 if( !dividend_size )
403 return 0;
404
405 /* If multiplication is much faster than division, and the
406 * dividend is large, pre-invert the divisor, and use
407 * only multiplications in the inner loop.
408 *
409 * This test should be read:
410 * Does it ever help to use udiv_qrnnd_preinv?
411 * && Does what we save compensate for the inversion overhead?
412 */
413 if( UDIV_TIME > (2 * UMUL_TIME + 6)
414 && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME ) {
415 int normalization_steps;
416
417 count_leading_zeros( normalization_steps, divisor_limb );
418 if( normalization_steps ) {
419 mpi_limb_t divisor_limb_inverted;
420
421 divisor_limb <<= normalization_steps;
422
423 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
424 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
425 * most significant bit (with weight 2**N) implicit.
426 */
427 /* Special case for DIVISOR_LIMB == 100...000. */
428 if( !(divisor_limb << 1) )
429 divisor_limb_inverted = ~(mpi_limb_t)0;
430 else
431 udiv_qrnnd(divisor_limb_inverted, dummy,
432 -divisor_limb, 0, divisor_limb);
433
434 n1 = dividend_ptr[dividend_size - 1];
435 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
436
437 /* Possible optimization:
438 * if (r == 0
439 * && divisor_limb > ((n1 << normalization_steps)
440 * | (dividend_ptr[dividend_size - 2] >> ...)))
441 * ...one division less...
442 */
443 for( i = dividend_size - 2; i >= 0; i--) {
444 n0 = dividend_ptr[i];
445 UDIV_QRNND_PREINV( quot_ptr[i + 1], r, r,
446 ((n1 << normalization_steps)
447 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
448 divisor_limb, divisor_limb_inverted);
449 n1 = n0;
450 }
451 UDIV_QRNND_PREINV( quot_ptr[0], r, r,
452 n1 << normalization_steps,
453 divisor_limb, divisor_limb_inverted);
454 return r >> normalization_steps;
455 }
456 else {
457 mpi_limb_t divisor_limb_inverted;
458
459 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
460 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
461 * most significant bit (with weight 2**N) implicit.
462 */
463 /* Special case for DIVISOR_LIMB == 100...000. */
464 if( !(divisor_limb << 1) )
465 divisor_limb_inverted = ~(mpi_limb_t) 0;
466 else
467 udiv_qrnnd(divisor_limb_inverted, dummy,
468 -divisor_limb, 0, divisor_limb);
469
470 i = dividend_size - 1;
471 r = dividend_ptr[i];
472
473 if( r >= divisor_limb )
474 r = 0;
475 else
476 quot_ptr[i--] = 0;
477
478 for( ; i >= 0; i-- ) {
479 n0 = dividend_ptr[i];
480 UDIV_QRNND_PREINV( quot_ptr[i], r, r,
481 n0, divisor_limb, divisor_limb_inverted);
482 }
483 return r;
484 }
485 }
486 else {
487 if(UDIV_NEEDS_NORMALIZATION) {
488 int normalization_steps;
489
490 count_leading_zeros (normalization_steps, divisor_limb);
491 if( normalization_steps ) {
492 divisor_limb <<= normalization_steps;
493
494 n1 = dividend_ptr[dividend_size - 1];
495 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
496
497 /* Possible optimization:
498 * if (r == 0
499 * && divisor_limb > ((n1 << normalization_steps)
500 * | (dividend_ptr[dividend_size - 2] >> ...)))
501 * ...one division less...
502 */
503 for( i = dividend_size - 2; i >= 0; i--) {
504 n0 = dividend_ptr[i];
505 udiv_qrnnd (quot_ptr[i + 1], r, r,
506 ((n1 << normalization_steps)
507 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
508 divisor_limb);
509 n1 = n0;
510 }
511 udiv_qrnnd (quot_ptr[0], r, r,
512 n1 << normalization_steps,
513 divisor_limb);
514 return r >> normalization_steps;
515 }
516 }
517 /* No normalization needed, either because udiv_qrnnd doesn't require
518 * it, or because DIVISOR_LIMB is already normalized. */
519 i = dividend_size - 1;
520 r = dividend_ptr[i];
521
522 if(r >= divisor_limb)
523 r = 0;
524 else
525 quot_ptr[i--] = 0;
526
527 for(; i >= 0; i--) {
528 n0 = dividend_ptr[i];
529 udiv_qrnnd( quot_ptr[i], r, r, n0, divisor_limb );
530 }
531 return r;
532 }
533}
534
535
diff --git a/pwmanager/libcrypt/mpi/mpih-mul.c b/pwmanager/libcrypt/mpi/mpih-mul.c
new file mode 100644
index 0000000..e1f6f58
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/mpih-mul.c
@@ -0,0 +1,530 @@
1/* mpih-mul.c - MPI helper functions
2 * Copyright (C) 1994, 1996, 1998, 1999, 2000,
3 * 2001, 2002 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 *
21 * Note: This code is heavily based on the GNU MP Library.
22 * Actually it's the same code with only minor changes in the
23 * way the data is stored; this is to support the abstraction
24 * of an optional secure memory allocation which may be used
25 * to avoid revealing of sensitive data due to paging etc.
26 */
27
28#include <config.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include "mpi-internal.h"
33#include "longlong.h"
34#include "g10lib.h"
35
36#define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
37 do { \
38 if( (size) < KARATSUBA_THRESHOLD ) \
39 mul_n_basecase (prodp, up, vp, size);\
40 else \
41 mul_n (prodp, up, vp, size, tspace);\
42 } while (0);
43
44#define MPN_SQR_N_RECURSE(prodp, up, size, tspace) \
45 do { \
46 if ((size) < KARATSUBA_THRESHOLD) \
47 _gcry_mpih_sqr_n_basecase (prodp, up, size); \
48 else \
49 _gcry_mpih_sqr_n (prodp, up, size, tspace); \
50 } while (0);
51
52
53
54
55/* Multiply the natural numbers u (pointed to by UP) and v (pointed to by VP),
56 * both with SIZE limbs, and store the result at PRODP. 2 * SIZE limbs are
57 * always stored. Return the most significant limb.
58 *
59 * Argument constraints:
60 * 1. PRODP != UP and PRODP != VP, i.e. the destination
61 * must be distinct from the multiplier and the multiplicand.
62 *
63 *
64 * Handle simple cases with traditional multiplication.
65 *
66 * This is the most critical code of multiplication. All multiplies rely
67 * on this, both small and huge. Small ones arrive here immediately. Huge
68 * ones arrive here as this is the base case for Karatsuba's recursive
69 * algorithm below.
70 */
71
72static mpi_limb_t
73mul_n_basecase( mpi_ptr_t prodp, mpi_ptr_t up,
74 mpi_ptr_t vp, mpi_size_t size)
75{
76 mpi_size_t i;
77 mpi_limb_t cy;
78 mpi_limb_t v_limb;
79
80 /* Multiply by the first limb in V separately, as the result can be
81 * stored (not added) to PROD. We also avoid a loop for zeroing. */
82 v_limb = vp[0];
83 if( v_limb <= 1 ) {
84 if( v_limb == 1 )
85 MPN_COPY( prodp, up, size );
86 else
87 MPN_ZERO( prodp, size );
88 cy = 0;
89 }
90 else
91 cy = _gcry_mpih_mul_1( prodp, up, size, v_limb );
92
93 prodp[size] = cy;
94 prodp++;
95
96 /* For each iteration in the outer loop, multiply one limb from
97 * U with one limb from V, and add it to PROD. */
98 for( i = 1; i < size; i++ ) {
99 v_limb = vp[i];
100 if( v_limb <= 1 ) {
101 cy = 0;
102 if( v_limb == 1 )
103 cy = _gcry_mpih_add_n(prodp, prodp, up, size);
104 }
105 else
106 cy = _gcry_mpih_addmul_1(prodp, up, size, v_limb);
107
108 prodp[size] = cy;
109 prodp++;
110 }
111
112 return cy;
113}
114
115
116static void
117mul_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp,
118 mpi_size_t size, mpi_ptr_t tspace )
119{
120 if( size & 1 ) {
121 /* The size is odd, and the code below doesn't handle that.
122 * Multiply the least significant (size - 1) limbs with a recursive
123 * call, and handle the most significant limb of S1 and S2
124 * separately.
125 * A slightly faster way to do this would be to make the Karatsuba
126 * code below behave as if the size were even, and let it check for
127 * odd size in the end. I.e., in essence move this code to the end.
128 * Doing so would save us a recursive call, and potentially make the
129 * stack grow a lot less.
130 */
131 mpi_size_t esize = size - 1; /* even size */
132 mpi_limb_t cy_limb;
133
134 MPN_MUL_N_RECURSE( prodp, up, vp, esize, tspace );
135 cy_limb = _gcry_mpih_addmul_1( prodp + esize, up, esize, vp[esize] );
136 prodp[esize + esize] = cy_limb;
137 cy_limb = _gcry_mpih_addmul_1( prodp + esize, vp, size, up[esize] );
138 prodp[esize + size] = cy_limb;
139 }
140 else {
141 /* Anatolij Alekseevich Karatsuba's divide-and-conquer algorithm.
142 *
143 * Split U in two pieces, U1 and U0, such that
144 * U = U0 + U1*(B**n),
145 * and V in V1 and V0, such that
146 * V = V0 + V1*(B**n).
147 *
148 * UV is then computed recursively using the identity
149 *
150 * 2n n n n
151 * UV = (B + B )U V + B (U -U )(V -V ) + (B + 1)U V
152 * 1 1 1 0 0 1 0 0
153 *
154 * Where B = 2**BITS_PER_MP_LIMB.
155 */
156 mpi_size_t hsize = size >> 1;
157 mpi_limb_t cy;
158 int negflg;
159
160 /* Product H. ________________ ________________
161 * |_____U1 x V1____||____U0 x V0_____|
162 * Put result in upper part of PROD and pass low part of TSPACE
163 * as new TSPACE.
164 */
165 MPN_MUL_N_RECURSE(prodp + size, up + hsize, vp + hsize, hsize, tspace);
166
167 /* Product M. ________________
168 * |_(U1-U0)(V0-V1)_|
169 */
170 if( _gcry_mpih_cmp(up + hsize, up, hsize) >= 0 ) {
171 _gcry_mpih_sub_n(prodp, up + hsize, up, hsize);
172 negflg = 0;
173 }
174 else {
175 _gcry_mpih_sub_n(prodp, up, up + hsize, hsize);
176 negflg = 1;
177 }
178 if( _gcry_mpih_cmp(vp + hsize, vp, hsize) >= 0 ) {
179 _gcry_mpih_sub_n(prodp + hsize, vp + hsize, vp, hsize);
180 negflg ^= 1;
181 }
182 else {
183 _gcry_mpih_sub_n(prodp + hsize, vp, vp + hsize, hsize);
184 /* No change of NEGFLG. */
185 }
186 /* Read temporary operands from low part of PROD.
187 * Put result in low part of TSPACE using upper part of TSPACE
188 * as new TSPACE.
189 */
190 MPN_MUL_N_RECURSE(tspace, prodp, prodp + hsize, hsize, tspace + size);
191
192 /* Add/copy product H. */
193 MPN_COPY (prodp + hsize, prodp + size, hsize);
194 cy = _gcry_mpih_add_n( prodp + size, prodp + size,
195 prodp + size + hsize, hsize);
196
197 /* Add product M (if NEGFLG M is a negative number) */
198 if(negflg)
199 cy -= _gcry_mpih_sub_n(prodp + hsize, prodp + hsize, tspace, size);
200 else
201 cy += _gcry_mpih_add_n(prodp + hsize, prodp + hsize, tspace, size);
202
203 /* Product L. ________________ ________________
204 * |________________||____U0 x V0_____|
205 * Read temporary operands from low part of PROD.
206 * Put result in low part of TSPACE using upper part of TSPACE
207 * as new TSPACE.
208 */
209 MPN_MUL_N_RECURSE(tspace, up, vp, hsize, tspace + size);
210
211 /* Add/copy Product L (twice) */
212
213 cy += _gcry_mpih_add_n(prodp + hsize, prodp + hsize, tspace, size);
214 if( cy )
215 _gcry_mpih_add_1(prodp + hsize + size, prodp + hsize + size, hsize, cy);
216
217 MPN_COPY(prodp, tspace, hsize);
218 cy = _gcry_mpih_add_n(prodp + hsize, prodp + hsize, tspace + hsize, hsize);
219 if( cy )
220 _gcry_mpih_add_1(prodp + size, prodp + size, size, 1);
221 }
222}
223
224
225void
226_gcry_mpih_sqr_n_basecase( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size )
227{
228 mpi_size_t i;
229 mpi_limb_t cy_limb;
230 mpi_limb_t v_limb;
231
232 /* Multiply by the first limb in V separately, as the result can be
233 * stored (not added) to PROD. We also avoid a loop for zeroing. */
234 v_limb = up[0];
235 if( v_limb <= 1 ) {
236 if( v_limb == 1 )
237 MPN_COPY( prodp, up, size );
238 else
239 MPN_ZERO(prodp, size);
240 cy_limb = 0;
241 }
242 else
243 cy_limb = _gcry_mpih_mul_1( prodp, up, size, v_limb );
244
245 prodp[size] = cy_limb;
246 prodp++;
247
248 /* For each iteration in the outer loop, multiply one limb from
249 * U with one limb from V, and add it to PROD. */
250 for( i=1; i < size; i++) {
251 v_limb = up[i];
252 if( v_limb <= 1 ) {
253 cy_limb = 0;
254 if( v_limb == 1 )
255 cy_limb = _gcry_mpih_add_n(prodp, prodp, up, size);
256 }
257 else
258 cy_limb = _gcry_mpih_addmul_1(prodp, up, size, v_limb);
259
260 prodp[size] = cy_limb;
261 prodp++;
262 }
263}
264
265
266void
267_gcry_mpih_sqr_n( mpi_ptr_t prodp,
268 mpi_ptr_t up, mpi_size_t size, mpi_ptr_t tspace)
269{
270 if( size & 1 ) {
271 /* The size is odd, and the code below doesn't handle that.
272 * Multiply the least significant (size - 1) limbs with a recursive
273 * call, and handle the most significant limb of S1 and S2
274 * separately.
275 * A slightly faster way to do this would be to make the Karatsuba
276 * code below behave as if the size were even, and let it check for
277 * odd size in the end. I.e., in essence move this code to the end.
278 * Doing so would save us a recursive call, and potentially make the
279 * stack grow a lot less.
280 */
281 mpi_size_t esize = size - 1; /* even size */
282 mpi_limb_t cy_limb;
283
284 MPN_SQR_N_RECURSE( prodp, up, esize, tspace );
285 cy_limb = _gcry_mpih_addmul_1( prodp + esize, up, esize, up[esize] );
286 prodp[esize + esize] = cy_limb;
287 cy_limb = _gcry_mpih_addmul_1( prodp + esize, up, size, up[esize] );
288
289 prodp[esize + size] = cy_limb;
290 }
291 else {
292 mpi_size_t hsize = size >> 1;
293 mpi_limb_t cy;
294
295 /* Product H. ________________ ________________
296 * |_____U1 x U1____||____U0 x U0_____|
297 * Put result in upper part of PROD and pass low part of TSPACE
298 * as new TSPACE.
299 */
300 MPN_SQR_N_RECURSE(prodp + size, up + hsize, hsize, tspace);
301
302 /* Product M. ________________
303 * |_(U1-U0)(U0-U1)_|
304 */
305 if( _gcry_mpih_cmp( up + hsize, up, hsize) >= 0 )
306 _gcry_mpih_sub_n( prodp, up + hsize, up, hsize);
307 else
308 _gcry_mpih_sub_n (prodp, up, up + hsize, hsize);
309
310 /* Read temporary operands from low part of PROD.
311 * Put result in low part of TSPACE using upper part of TSPACE
312 * as new TSPACE. */
313 MPN_SQR_N_RECURSE(tspace, prodp, hsize, tspace + size);
314
315 /* Add/copy product H */
316 MPN_COPY(prodp + hsize, prodp + size, hsize);
317 cy = _gcry_mpih_add_n(prodp + size, prodp + size,
318 prodp + size + hsize, hsize);
319
320 /* Add product M (if NEGFLG M is a negative number). */
321 cy -= _gcry_mpih_sub_n (prodp + hsize, prodp + hsize, tspace, size);
322
323 /* Product L. ________________ ________________
324 * |________________||____U0 x U0_____|
325 * Read temporary operands from low part of PROD.
326 * Put result in low part of TSPACE using upper part of TSPACE
327 * as new TSPACE. */
328 MPN_SQR_N_RECURSE (tspace, up, hsize, tspace + size);
329
330 /* Add/copy Product L (twice).*/
331 cy += _gcry_mpih_add_n (prodp + hsize, prodp + hsize, tspace, size);
332 if( cy )
333 _gcry_mpih_add_1(prodp + hsize + size, prodp + hsize + size,
334 hsize, cy);
335
336 MPN_COPY(prodp, tspace, hsize);
337 cy = _gcry_mpih_add_n (prodp + hsize, prodp + hsize, tspace + hsize, hsize);
338 if( cy )
339 _gcry_mpih_add_1 (prodp + size, prodp + size, size, 1);
340 }
341}
342
343
344/* This should be made into an inline function in gmp.h. */
345void
346_gcry_mpih_mul_n( mpi_ptr_t prodp,
347 mpi_ptr_t up, mpi_ptr_t vp, mpi_size_t size)
348{
349 int secure;
350
351 if( up == vp ) {
352 if( size < KARATSUBA_THRESHOLD )
353 _gcry_mpih_sqr_n_basecase( prodp, up, size );
354 else {
355 mpi_ptr_t tspace;
356 secure = gcry_is_secure( up );
357 tspace = mpi_alloc_limb_space( 2 * size, secure );
358 _gcry_mpih_sqr_n( prodp, up, size, tspace );
359 _gcry_mpi_free_limb_space (tspace, 2 * size );
360 }
361 }
362 else {
363 if( size < KARATSUBA_THRESHOLD )
364 mul_n_basecase( prodp, up, vp, size );
365 else {
366 mpi_ptr_t tspace;
367 secure = gcry_is_secure( up ) || gcry_is_secure( vp );
368 tspace = mpi_alloc_limb_space( 2 * size, secure );
369 mul_n (prodp, up, vp, size, tspace);
370 _gcry_mpi_free_limb_space (tspace, 2 * size );
371 }
372 }
373}
374
375
376
377void
378_gcry_mpih_mul_karatsuba_case( mpi_ptr_t prodp,
379 mpi_ptr_t up, mpi_size_t usize,
380 mpi_ptr_t vp, mpi_size_t vsize,
381 struct karatsuba_ctx *ctx )
382{
383 mpi_limb_t cy;
384
385 if( !ctx->tspace || ctx->tspace_size < vsize ) {
386 if( ctx->tspace )
387 _gcry_mpi_free_limb_space( ctx->tspace, ctx->tspace_nlimbs );
388 ctx->tspace_nlimbs = 2 * vsize;
389 ctx->tspace = mpi_alloc_limb_space( 2 * vsize,
390 (gcry_is_secure( up )
391 || gcry_is_secure( vp )) );
392 ctx->tspace_size = vsize;
393 }
394
395 MPN_MUL_N_RECURSE( prodp, up, vp, vsize, ctx->tspace );
396
397 prodp += vsize;
398 up += vsize;
399 usize -= vsize;
400 if( usize >= vsize ) {
401 if( !ctx->tp || ctx->tp_size < vsize ) {
402 if( ctx->tp )
403 _gcry_mpi_free_limb_space( ctx->tp, ctx->tp_nlimbs );
404 ctx->tp_nlimbs = 2 * vsize;
405 ctx->tp = mpi_alloc_limb_space( 2 * vsize, gcry_is_secure( up )
406 || gcry_is_secure( vp ) );
407 ctx->tp_size = vsize;
408 }
409
410 do {
411 MPN_MUL_N_RECURSE( ctx->tp, up, vp, vsize, ctx->tspace );
412 cy = _gcry_mpih_add_n( prodp, prodp, ctx->tp, vsize );
413 _gcry_mpih_add_1( prodp + vsize, ctx->tp + vsize, vsize, cy );
414 prodp += vsize;
415 up += vsize;
416 usize -= vsize;
417 } while( usize >= vsize );
418 }
419
420 if( usize ) {
421 if( usize < KARATSUBA_THRESHOLD ) {
422 _gcry_mpih_mul( ctx->tspace, vp, vsize, up, usize );
423 }
424 else {
425 if( !ctx->next ) {
426 ctx->next = gcry_xcalloc( 1, sizeof *ctx );
427 }
428 _gcry_mpih_mul_karatsuba_case( ctx->tspace,
429 vp, vsize,
430 up, usize,
431 ctx->next );
432 }
433
434 cy = _gcry_mpih_add_n( prodp, prodp, ctx->tspace, vsize);
435 _gcry_mpih_add_1( prodp + vsize, ctx->tspace + vsize, usize, cy );
436 }
437}
438
439
440void
441_gcry_mpih_release_karatsuba_ctx( struct karatsuba_ctx *ctx )
442{
443 struct karatsuba_ctx *ctx2;
444
445 if( ctx->tp )
446 _gcry_mpi_free_limb_space( ctx->tp, ctx->tp_nlimbs );
447 if( ctx->tspace )
448 _gcry_mpi_free_limb_space( ctx->tspace, ctx->tspace_nlimbs );
449 for( ctx=ctx->next; ctx; ctx = ctx2 ) {
450 ctx2 = ctx->next;
451 if( ctx->tp )
452 _gcry_mpi_free_limb_space( ctx->tp, ctx->tp_nlimbs );
453 if( ctx->tspace )
454 _gcry_mpi_free_limb_space( ctx->tspace, ctx->tspace_nlimbs );
455 gcry_free( ctx );
456 }
457}
458
459/* Multiply the natural numbers u (pointed to by UP, with USIZE limbs)
460 * and v (pointed to by VP, with VSIZE limbs), and store the result at
461 * PRODP. USIZE + VSIZE limbs are always stored, but if the input
462 * operands are normalized. Return the most significant limb of the
463 * result.
464 *
465 * NOTE: The space pointed to by PRODP is overwritten before finished
466 * with U and V, so overlap is an error.
467 *
468 * Argument constraints:
469 * 1. USIZE >= VSIZE.
470 * 2. PRODP != UP and PRODP != VP, i.e. the destination
471 * must be distinct from the multiplier and the multiplicand.
472 */
473
474mpi_limb_t
475_gcry_mpih_mul( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
476 mpi_ptr_t vp, mpi_size_t vsize)
477{
478 mpi_ptr_t prod_endp = prodp + usize + vsize - 1;
479 mpi_limb_t cy;
480 struct karatsuba_ctx ctx;
481
482 if( vsize < KARATSUBA_THRESHOLD ) {
483 mpi_size_t i;
484 mpi_limb_t v_limb;
485
486 if( !vsize )
487 return 0;
488
489 /* Multiply by the first limb in V separately, as the result can be
490 * stored (not added) to PROD.We also avoid a loop for zeroing. */
491 v_limb = vp[0];
492 if( v_limb <= 1 ) {
493 if( v_limb == 1 )
494 MPN_COPY( prodp, up, usize );
495 else
496 MPN_ZERO( prodp, usize );
497 cy = 0;
498 }
499 else
500 cy = _gcry_mpih_mul_1( prodp, up, usize, v_limb );
501
502 prodp[usize] = cy;
503 prodp++;
504
505 /* For each iteration in the outer loop, multiply one limb from
506 * U with one limb from V, and add it to PROD.*/
507 for( i = 1; i < vsize; i++ ) {
508 v_limb = vp[i];
509 if( v_limb <= 1 ) {
510 cy = 0;
511 if( v_limb == 1 )
512 cy = _gcry_mpih_add_n(prodp, prodp, up, usize);
513 }
514 else
515 cy = _gcry_mpih_addmul_1(prodp, up, usize, v_limb);
516
517 prodp[usize] = cy;
518 prodp++;
519 }
520
521 return cy;
522 }
523
524 memset( &ctx, 0, sizeof ctx );
525 _gcry_mpih_mul_karatsuba_case( prodp, up, usize, vp, vsize, &ctx );
526 _gcry_mpih_release_karatsuba_ctx( &ctx );
527 return *prod_endp;
528}
529
530
diff --git a/pwmanager/libcrypt/mpi/mpiutil.c b/pwmanager/libcrypt/mpi/mpiutil.c
new file mode 100644
index 0000000..2a0bafa
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/mpiutil.c
@@ -0,0 +1,431 @@
1/* mpiutil.ac - Utility functions for MPI
2 * Copyright (C) 1998, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#include <config.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <assert.h>
26
27#include "g10lib.h"
28#include "mpi-internal.h"
29#include "memory.h"
30
31/****************
32 * Note: It was a bad idea to use the number of limbs to allocate
33 * because on a alpha the limbs are large but we normally need
34 * integers of n bits - So we should chnage this to bits (or bytes).
35 *
36 * But mpi_alloc is used in a lot of places :-)
37 */
38gcry_mpi_t
39_gcry_mpi_alloc( unsigned nlimbs )
40{
41 gcry_mpi_t a;
42
43 a = gcry_xmalloc( sizeof *a );
44 a->d = nlimbs? mpi_alloc_limb_space( nlimbs, 0 ) : NULL;
45 a->alloced = nlimbs;
46 a->nlimbs = 0;
47 a->sign = 0;
48 a->flags = 0;
49 return a;
50}
51
52void
53_gcry_mpi_m_check( gcry_mpi_t a )
54{
55 _gcry_check_heap(a);
56 _gcry_check_heap(a->d);
57}
58
59gcry_mpi_t
60_gcry_mpi_alloc_secure( unsigned nlimbs )
61{
62 gcry_mpi_t a;
63
64 a = gcry_xmalloc( sizeof *a );
65 a->d = nlimbs? mpi_alloc_limb_space( nlimbs, 1 ) : NULL;
66 a->alloced = nlimbs;
67 a->flags = 1;
68 a->nlimbs = 0;
69 a->sign = 0;
70 return a;
71}
72
73
74
75mpi_ptr_t
76_gcry_mpi_alloc_limb_space( unsigned int nlimbs, int secure )
77{
78 size_t len = nlimbs * sizeof(mpi_limb_t);
79 mpi_ptr_t p = NULL;
80
81 if (!nlimbs)
82 {
83 p = secure? gcry_xmalloc_secure( 1 ) : gcry_xmalloc( 1 );
84 *p = 0;
85 }
86 else
87 p = secure? gcry_xmalloc_secure( len ) : gcry_xmalloc( len );
88
89 return p;
90}
91
92void
93_gcry_mpi_free_limb_space( mpi_ptr_t a, unsigned int nlimbs)
94{
95 if (a)
96 {
97 size_t len = nlimbs * sizeof(mpi_limb_t);
98
99 /* If we have information on the number of allocated limbs, we
100 better wipe that space out. This is a failsafe feature if
101 secure memory has been disabled or was not properly
102 implemented in user provided allocation functions. */
103 if (len)
104 wipememory (a, len);
105 gcry_free(a);
106 }
107}
108
109
110void
111_gcry_mpi_assign_limb_space( gcry_mpi_t a, mpi_ptr_t ap, unsigned int nlimbs )
112{
113 _gcry_mpi_free_limb_space (a->d, a->alloced);
114 a->d = ap;
115 a->alloced = nlimbs;
116}
117
118
119
120/****************
121 * Resize the array of A to NLIMBS. the additional space is cleared
122 * (set to 0) [done by gcry_realloc()]
123 */
124void
125_gcry_mpi_resize (gcry_mpi_t a, unsigned nlimbs)
126{
127 if (nlimbs <= a->alloced)
128 return; /* no need to do it */
129
130 if (a->d)
131 a->d = gcry_xrealloc (a->d, nlimbs * sizeof (mpi_limb_t));
132 else
133 {
134 if (a->flags & 1)
135 /* Secure memory is wanted. */
136 a->d = gcry_xcalloc_secure (nlimbs , sizeof (mpi_limb_t));
137 else
138 /* Standard memory. */
139 a->d = gcry_xcalloc (nlimbs , sizeof (mpi_limb_t));
140 }
141 a->alloced = nlimbs;
142}
143
144void
145_gcry_mpi_clear( gcry_mpi_t a )
146{
147 a->nlimbs = 0;
148 a->flags = 0;
149}
150
151
152void
153_gcry_mpi_free( gcry_mpi_t a )
154{
155 if (!a )
156 return;
157 if ((a->flags & 4))
158 gcry_free( a->d );
159 else
160 {
161 _gcry_mpi_free_limb_space(a->d, a->alloced);
162 }
163 if ((a->flags & ~7))
164 log_bug("invalid flag value in mpi\n");
165 gcry_free(a);
166}
167
168static void
169mpi_set_secure( gcry_mpi_t a )
170{
171 mpi_ptr_t ap, bp;
172
173 if ( (a->flags & 1) )
174 return;
175 a->flags |= 1;
176 ap = a->d;
177 if (!a->nlimbs)
178 {
179 assert(!ap);
180 return;
181 }
182 bp = mpi_alloc_limb_space (a->nlimbs, 1);
183 MPN_COPY( bp, ap, a->nlimbs );
184 a->d = bp;
185 _gcry_mpi_free_limb_space (ap, a->alloced);
186}
187
188
189gcry_mpi_t
190gcry_mpi_set_opaque( gcry_mpi_t a, void *p, unsigned int nbits )
191{
192 if (!a)
193 a = mpi_alloc(0);
194
195 if( a->flags & 4 )
196 gcry_free( a->d );
197 else
198 _gcry_mpi_free_limb_space (a->d, a->alloced);
199
200 a->d = p;
201 a->alloced = 0;
202 a->nlimbs = 0;
203 a->sign = nbits;
204 a->flags = 4;
205 return a;
206}
207
208
209void *
210gcry_mpi_get_opaque( gcry_mpi_t a, unsigned int *nbits )
211{
212 if( !(a->flags & 4) )
213 log_bug("mpi_get_opaque on normal mpi\n");
214 if( nbits )
215 *nbits = a->sign;
216 return a->d;
217}
218
219
220/****************
221 * Note: This copy function should not interpret the MPI
222 * but copy it transparently.
223 */
224gcry_mpi_t
225_gcry_mpi_copy( gcry_mpi_t a )
226{
227 int i;
228 gcry_mpi_t b;
229
230 if( a && (a->flags & 4) ) {
231 void *p = gcry_is_secure(a->d)? gcry_xmalloc_secure( (a->sign+7)/8 )
232 : gcry_xmalloc( (a->sign+7)/8 );
233 memcpy( p, a->d, (a->sign+7)/8 );
234 b = gcry_mpi_set_opaque( NULL, p, a->sign );
235 }
236 else if( a ) {
237 b = mpi_is_secure(a)? mpi_alloc_secure( a->nlimbs )
238 : mpi_alloc( a->nlimbs );
239 b->nlimbs = a->nlimbs;
240 b->sign = a->sign;
241 b->flags = a->flags;
242 for(i=0; i < b->nlimbs; i++ )
243 b->d[i] = a->d[i];
244 }
245 else
246 b = NULL;
247 return b;
248}
249
250
251/****************
252 * This function allocates an MPI which is optimized to hold
253 * a value as large as the one given in the argument and allocates it
254 * with the same flags as A.
255 */
256gcry_mpi_t
257_gcry_mpi_alloc_like( gcry_mpi_t a )
258{
259 gcry_mpi_t b;
260
261 if( a && (a->flags & 4) ) {
262 int n = (a->sign+7)/8;
263 void *p = gcry_is_secure(a->d)? gcry_malloc_secure( n )
264 : gcry_malloc( n );
265 memcpy( p, a->d, n );
266 b = gcry_mpi_set_opaque( NULL, p, a->sign );
267 }
268 else if( a ) {
269 b = mpi_is_secure(a)? mpi_alloc_secure( a->nlimbs )
270 : mpi_alloc( a->nlimbs );
271 b->nlimbs = 0;
272 b->sign = 0;
273 b->flags = a->flags;
274 }
275 else
276 b = NULL;
277 return b;
278}
279
280
281void
282_gcry_mpi_set( gcry_mpi_t w, gcry_mpi_t u)
283{
284 mpi_ptr_t wp, up;
285 mpi_size_t usize = u->nlimbs;
286 int usign = u->sign;
287
288 RESIZE_IF_NEEDED(w, usize);
289 wp = w->d;
290 up = u->d;
291 MPN_COPY( wp, up, usize );
292 w->nlimbs = usize;
293 w->flags = u->flags;
294 w->sign = usign;
295}
296
297
298void
299_gcry_mpi_set_ui( gcry_mpi_t w, unsigned long u)
300{
301 RESIZE_IF_NEEDED(w, 1);
302 w->d[0] = u;
303 w->nlimbs = u? 1:0;
304 w->sign = 0;
305 w->flags = 0;
306}
307
308
309gcry_mpi_t
310_gcry_mpi_alloc_set_ui( unsigned long u)
311{
312 gcry_mpi_t w = mpi_alloc(1);
313 w->d[0] = u;
314 w->nlimbs = u? 1:0;
315 w->sign = 0;
316 return w;
317}
318
319
320void
321_gcry_mpi_swap( gcry_mpi_t a, gcry_mpi_t b)
322{
323 struct gcry_mpi tmp;
324
325 tmp = *a; *a = *b; *b = tmp;
326}
327
328void
329gcry_mpi_swap( gcry_mpi_t a, gcry_mpi_t b)
330{
331 _gcry_mpi_swap (a, b);
332}
333
334
335gcry_mpi_t
336gcry_mpi_new( unsigned int nbits )
337{
338 return _gcry_mpi_alloc( (nbits+BITS_PER_MPI_LIMB-1) / BITS_PER_MPI_LIMB );
339}
340
341
342gcry_mpi_t
343gcry_mpi_snew( unsigned int nbits )
344{
345 return _gcry_mpi_alloc_secure( (nbits+BITS_PER_MPI_LIMB-1) / BITS_PER_MPI_LIMB );
346}
347
348void
349gcry_mpi_release( gcry_mpi_t a )
350{
351 _gcry_mpi_free( a );
352}
353
354gcry_mpi_t
355gcry_mpi_copy( const gcry_mpi_t a )
356{
357 return _gcry_mpi_copy( (gcry_mpi_t)a );
358}
359
360gcry_mpi_t
361gcry_mpi_set( gcry_mpi_t w, const gcry_mpi_t u )
362{
363 if( !w )
364 w = _gcry_mpi_alloc( mpi_get_nlimbs(u) );
365 _gcry_mpi_set( w, (gcry_mpi_t)u );
366 return w;
367}
368
369gcry_mpi_t
370gcry_mpi_set_ui( gcry_mpi_t w, unsigned long u )
371{
372 if( !w )
373 w = _gcry_mpi_alloc(1);
374 _gcry_mpi_set_ui( w, u );
375 return w;
376}
377
378
379void
380gcry_mpi_randomize( gcry_mpi_t w,
381 unsigned int nbits, enum gcry_random_level level )
382{
383 char *p;
384 size_t nbytes = (nbits+7)/8;
385
386 if (level == GCRY_WEAK_RANDOM)
387 {
388 p = mpi_is_secure(w) ? gcry_xmalloc (nbytes)
389 : gcry_xmalloc_secure (nbytes);
390 gcry_create_nonce (p, nbytes);
391 }
392 else
393 {
394 p = mpi_is_secure(w) ? gcry_random_bytes (nbytes, level)
395 : gcry_random_bytes_secure (nbytes, level);
396 }
397 _gcry_mpi_set_buffer( w, p, nbytes, 0 );
398 gcry_free (p);
399}
400
401
402void
403gcry_mpi_set_flag( gcry_mpi_t a, enum gcry_mpi_flag flag )
404{
405 switch( flag ) {
406 case GCRYMPI_FLAG_SECURE: mpi_set_secure(a); break;
407 case GCRYMPI_FLAG_OPAQUE:
408 default: log_bug("invalid flag value\n");
409 }
410}
411
412void
413gcry_mpi_clear_flag( gcry_mpi_t a, enum gcry_mpi_flag flag )
414{
415 switch( flag ) {
416 case GCRYMPI_FLAG_SECURE:
417 case GCRYMPI_FLAG_OPAQUE:
418 default: log_bug("invalid flag value\n");
419 }
420}
421
422int
423gcry_mpi_get_flag( gcry_mpi_t a, enum gcry_mpi_flag flag )
424{
425 switch( flag ) {
426 case GCRYMPI_FLAG_SECURE: return (a->flags & 1);
427 case GCRYMPI_FLAG_OPAQUE: return (a->flags & 4);
428 default: log_bug("invalid flag value\n");
429 }
430}
431
diff --git a/pwmanager/libcrypt/mpi/sysdep.h b/pwmanager/libcrypt/mpi/sysdep.h
new file mode 100644
index 0000000..9615e06
--- a/dev/null
+++ b/pwmanager/libcrypt/mpi/sysdep.h
@@ -0,0 +1,2 @@
1/* created by config.links - do not edit */
2#define C_SYMBOL_NAME(name) name
diff --git a/pwmanager/libcrypt/zlib/ChangeLog b/pwmanager/libcrypt/zlib/ChangeLog
new file mode 100644
index 0000000..5f06bcc
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/ChangeLog
@@ -0,0 +1,505 @@
12002-03-12 Werner Koch <wk@gnupg.org>
2
3 Merged changes from zlib 1.1.4.
4
5Sat Feb 13 12:04:43 CET 1999 Werner Koch <wk@isil.d.shuttle.de>
6
7 * Makefile.am: Removed -Wall
8
9Wed Jan 20 21:40:21 CET 1999 Werner Koch <wk@isil.d.shuttle.de>
10
11 * Makefile.am: Corrected the list of sources
12
13Wed Jan 13 14:10:15 CET 1999 Werner Koch <wk@isil.d.shuttle.de>
14
15 Merged version 1.1.3 with the previousy used 1.0.4
16
17Thu Feb 12 12:20:45 1998 Werner Koch (wk@frodo)
18
19 Removed a couple of files, as this is only used if zlib is
20 missing on a system. Added automake stuff
21
22
23 ChangeLog file for zlib
24
25
26Changes in 1.1.4 (11 March 2002)
27- ZFREE was repeated on same allocation on some error conditions.
28 This creates a security problem described in
29 http://www.zlib.org/advisory-2002-03-11.txt
30- Returned incorrect error (Z_MEM_ERROR) on some invalid data
31- Avoid accesses before window for invalid distances with inflate window
32 less than 32K.
33- force windowBits > 8 to avoid a bug in the encoder for a window size
34 of 256 bytes. (A complete fix will be available in 1.1.5).
35
36Changes in 1.1.3 (9 July 1998)
37- fix "an inflate input buffer bug that shows up on rare but persistent
38 occasions" (Mark)
39- fix gzread and gztell for concatenated .gz files (Didier Le Botlan)
40- fix gzseek(..., SEEK_SET) in write mode
41- fix crc check after a gzeek (Frank Faubert)
42- fix miniunzip when the last entry in a zip file is itself a zip file
43 (J Lillge)
44- add contrib/asm586 and contrib/asm686 (Brian Raiter)
45 See http://www.muppetlabs.com/~breadbox/software/assembly.html
46- add support for Delphi 3 in contrib/delphi (Bob Dellaca)
47- add support for C++Builder 3 and Delphi 3 in contrib/delphi2 (Davide Moretti)
48- do not exit prematurely in untgz if 0 at start of block (Magnus Holmgren)
49- use macro EXTERN instead of extern to support DLL for BeOS (Sander Stoks)
50- added a FAQ file
51
52- Support gzdopen on Mac with Metrowerks (Jason Linhart)
53- Do not redefine Byte on Mac (Brad Pettit & Jason Linhart)
54- define SEEK_END too if SEEK_SET is not defined (Albert Chin-A-Young)
55- avoid some warnings with Borland C (Tom Tanner)
56- fix a problem in contrib/minizip/zip.c for 16-bit MSDOS (Gilles Vollant)
57- emulate utime() for WIN32 in contrib/untgz (Gilles Vollant)
58- allow several arguments to configure (Tim Mooney, Frodo Looijaard)
59- use libdir and includedir in Makefile.in (Tim Mooney)
60- support shared libraries on OSF1 V4 (Tim Mooney)
61- remove so_locations in "make clean" (Tim Mooney)
62- fix maketree.c compilation error (Glenn, Mark)
63- Python interface to zlib now in Python 1.5 (Jeremy Hylton)
64- new Makefile.riscos (Rich Walker)
65- initialize static descriptors in trees.c for embedded targets (Nick Smith)
66- use "foo-gz" in example.c for RISCOS and VMS (Nick Smith)
67- add the OS/2 files in Makefile.in too (Andrew Zabolotny)
68- fix fdopen and halloc macros for Microsoft C 6.0 (Tom Lane)
69- fix maketree.c to allow clean compilation of inffixed.h (Mark)
70- fix parameter check in deflateCopy (Gunther Nikl)
71- cleanup trees.c, use compressed_len only in debug mode (Christian Spieler)
72- Many portability patches by Christian Spieler:
73 . zutil.c, zutil.h: added "const" for zmem*
74 . Make_vms.com: fixed some typos
75 . Make_vms.com: msdos/Makefile.*: removed zutil.h from some dependency lists
76 . msdos/Makefile.msc: remove "default rtl link library" info from obj files
77 . msdos/Makefile.*: use model-dependent name for the built zlib library
78 . msdos/Makefile.emx, nt/Makefile.emx, nt/Makefile.gcc:
79 new makefiles, for emx (DOS/OS2), emx&rsxnt and mingw32 (Windows 9x / NT)
80- use define instead of typedef for Bytef also for MSC small/medium (Tom Lane)
81- replace __far with _far for better portability (Christian Spieler, Tom Lane)
82- fix test for errno.h in configure (Tim Newsham)
83
84Changes in 1.1.2 (19 March 98)
85- added contrib/minzip, mini zip and unzip based on zlib (Gilles Vollant)
86 See http://www.winimage.com/zLibDll/unzip.html
87- preinitialize the inflate tables for fixed codes, to make the code
88 completely thread safe (Mark)
89- some simplifications and slight speed-up to the inflate code (Mark)
90- fix gzeof on non-compressed files (Allan Schrum)
91- add -std1 option in configure for OSF1 to fix gzprintf (Martin Mokrejs)
92- use default value of 4K for Z_BUFSIZE for 16-bit MSDOS (Tim Wegner + Glenn)
93- added os2/Makefile.def and os2/zlib.def (Andrew Zabolotny)
94- add shared lib support for UNIX_SV4.2MP (MATSUURA Takanori)
95- do not wrap extern "C" around system includes (Tom Lane)
96- mention zlib binding for TCL in README (Andreas Kupries)
97- added amiga/Makefile.pup for Amiga powerUP SAS/C PPC (Andreas Kleinert)
98- allow "make install prefix=..." even after configure (Glenn Randers-Pehrson)
99- allow "configure --prefix $HOME" (Tim Mooney)
100- remove warnings in example.c and gzio.c (Glenn Randers-Pehrson)
101- move Makefile.sas to amiga/Makefile.sas
102
103Changes in 1.1.1 (27 Feb 98)
104- fix macros _tr_tally_* in deflate.h for debug mode (Glenn Randers-Pehrson)
105- remove block truncation heuristic which had very marginal effect for zlib
106 (smaller lit_bufsize than in gzip 1.2.4) and degraded a little the
107 compression ratio on some files. This also allows inlining _tr_tally for
108 matches in deflate_slow.
109- added msdos/Makefile.w32 for WIN32 Microsoft Visual C++ (Bob Frazier)
110
111Changes in 1.1.0 (24 Feb 98)
112- do not return STREAM_END prematurely in inflate (John Bowler)
113- revert to the zlib 1.0.8 inflate to avoid the gcc 2.8.0 bug (Jeremy Buhler)
114- compile with -DFASTEST to get compression code optimized for speed only
115- in minigzip, try mmap'ing the input file first (Miguel Albrecht)
116- increase size of I/O buffers in minigzip.c and gzio.c (not a big gain
117 on Sun but significant on HP)
118
119- add a pointer to experimental unzip library in README (Gilles Vollant)
120- initialize variable gcc in configure (Chris Herborth)
121
122Changes in 1.0.9 (17 Feb 1998)
123- added gzputs and gzgets functions
124- do not clear eof flag in gzseek (Mark Diekhans)
125- fix gzseek for files in transparent mode (Mark Diekhans)
126- do not assume that vsprintf returns the number of bytes written (Jens Krinke)
127- replace EXPORT with ZEXPORT to avoid conflict with other programs
128- added compress2 in zconf.h, zlib.def, zlib.dnt
129- new asm code from Gilles Vollant in contrib/asm386
130- simplify the inflate code (Mark):
131 . Replace ZALLOC's in huft_build() with single ZALLOC in inflate_blocks_new()
132 . ZALLOC the length list in inflate_trees_fixed() instead of using stack
133 . ZALLOC the value area for huft_build() instead of using stack
134 . Simplify Z_FINISH check in inflate()
135
136- Avoid gcc 2.8.0 comparison bug a little differently than zlib 1.0.8
137- in inftrees.c, avoid cc -O bug on HP (Farshid Elahi)
138- in zconf.h move the ZLIB_DLL stuff earlier to avoid problems with
139 the declaration of FAR (Gilles VOllant)
140- install libz.so* with mode 755 (executable) instead of 644 (Marc Lehmann)
141- read_buf buf parameter of type Bytef* instead of charf*
142- zmemcpy parameters are of type Bytef*, not charf* (Joseph Strout)
143- do not redeclare unlink in minigzip.c for WIN32 (John Bowler)
144- fix check for presence of directories in "make install" (Ian Willis)
145
146Changes in 1.0.8 (27 Jan 1998)
147- fixed offsets in contrib/asm386/gvmat32.asm (Gilles Vollant)
148- fix gzgetc and gzputc for big endian systems (Markus Oberhumer)
149- added compress2() to allow setting the compression level
150- include sys/types.h to get off_t on some systems (Marc Lehmann & QingLong)
151- use constant arrays for the static trees in trees.c instead of computing
152 them at run time (thanks to Ken Raeburn for this suggestion). To create
153 trees.h, compile with GEN_TREES_H and run "make test".
154- check return code of example in "make test" and display result
155- pass minigzip command line options to file_compress
156- simplifying code of inflateSync to avoid gcc 2.8 bug
157
158- support CC="gcc -Wall" in configure -s (QingLong)
159- avoid a flush caused by ftell in gzopen for write mode (Ken Raeburn)
160- fix test for shared library support to avoid compiler warnings
161- zlib.lib -> zlib.dll in msdos/zlib.rc (Gilles Vollant)
162- check for TARGET_OS_MAC in addition to MACOS (Brad Pettit)
163- do not use fdopen for Metrowerks on Mac (Brad Pettit))
164- add checks for gzputc and gzputc in example.c
165- avoid warnings in gzio.c and deflate.c (Andreas Kleinert)
166- use const for the CRC table (Ken Raeburn)
167- fixed "make uninstall" for shared libraries
168- use Tracev instead of Trace in infblock.c
169- in example.c use correct compressed length for test_sync
170- suppress +vnocompatwarnings in configure for HPUX (not always supported)
171
172Changes in 1.0.7 (20 Jan 1998)
173- fix gzseek which was broken in write mode
174- return error for gzseek to negative absolute position
175- fix configure for Linux (Chun-Chung Chen)
176- increase stack space for MSC (Tim Wegner)
177- get_crc_table and inflateSyncPoint are EXPORTed (Gilles Vollant)
178- define EXPORTVA for gzprintf (Gilles Vollant)
179- added man page zlib.3 (Rick Rodgers)
180- for contrib/untgz, fix makedir() and improve Makefile
181
182- check gzseek in write mode in example.c
183- allocate extra buffer for seeks only if gzseek is actually called
184- avoid signed/unsigned comparisons (Tim Wegner, Gilles Vollant)
185- add inflateSyncPoint in zconf.h
186- fix list of exported functions in nt/zlib.dnt and mdsos/zlib.def
187
188Changes in 1.0.6 (19 Jan 1998)
189- add functions gzprintf, gzputc, gzgetc, gztell, gzeof, gzseek, gzrewind and
190 gzsetparams (thanks to Roland Giersig and Kevin Ruland for some of this code)
191- Fix a deflate bug occuring only with compression level 0 (thanks to
192 Andy Buckler for finding this one).
193- In minigzip, pass transparently also the first byte for .Z files.
194- return Z_BUF_ERROR instead of Z_OK if output buffer full in uncompress()
195- check Z_FINISH in inflate (thanks to Marc Schluper)
196- Implement deflateCopy (thanks to Adam Costello)
197- make static libraries by default in configure, add --shared option.
198- move MSDOS or Windows specific files to directory msdos
199- suppress the notion of partial flush to simplify the interface
200 (but the symbol Z_PARTIAL_FLUSH is kept for compatibility with 1.0.4)
201- suppress history buffer provided by application to simplify the interface
202 (this feature was not implemented anyway in 1.0.4)
203- next_in and avail_in must be initialized before calling inflateInit or
204 inflateInit2
205- add EXPORT in all exported functions (for Windows DLL)
206- added Makefile.nt (thanks to Stephen Williams)
207- added the unsupported "contrib" directory:
208 contrib/asm386/ by Gilles Vollant <info@winimage.com>
209 386 asm code replacing longest_match().
210 contrib/iostream/ by Kevin Ruland <kevin@rodin.wustl.edu>
211 A C++ I/O streams interface to the zlib gz* functions
212 contrib/iostream2/ by Tyge Løvset <Tyge.Lovset@cmr.no>
213 Another C++ I/O streams interface
214 contrib/untgz/ by "Pedro A. Aranda Guti\irrez" <paag@tid.es>
215 A very simple tar.gz file extractor using zlib
216 contrib/visual-basic.txt by Carlos Rios <c_rios@sonda.cl>
217 How to use compress(), uncompress() and the gz* functions from VB.
218- pass params -f (filtered data), -h (huffman only), -1 to -9 (compression
219 level) in minigzip (thanks to Tom Lane)
220
221- use const for rommable constants in deflate
222- added test for gzseek and gztell in example.c
223- add undocumented function inflateSyncPoint() (hack for Paul Mackerras)
224- add undocumented function zError to convert error code to string
225 (for Tim Smithers)
226- Allow compilation of gzio with -DNO_DEFLATE to avoid the compression code.
227- Use default memcpy for Symantec MSDOS compiler.
228- Add EXPORT keyword for check_func (needed for Windows DLL)
229- add current directory to LD_LIBRARY_PATH for "make test"
230- create also a link for libz.so.1
231- added support for FUJITSU UXP/DS (thanks to Toshiaki Nomura)
232- use $(SHAREDLIB) instead of libz.so in Makefile.in (for HPUX)
233- added -soname for Linux in configure (Chun-Chung Chen,
234- assign numbers to the exported functions in zlib.def (for Windows DLL)
235- add advice in zlib.h for best usage of deflateSetDictionary
236- work around compiler bug on Atari (cast Z_NULL in call of s->checkfn)
237- allow compilation with ANSI keywords only enabled for TurboC in large model
238- avoid "versionString"[0] (Borland bug)
239- add NEED_DUMMY_RETURN for Borland
240- use variable z_verbose for tracing in debug mode (L. Peter Deutsch).
241- allow compilation with CC
242- defined STDC for OS/2 (David Charlap)
243- limit external names to 8 chars for MVS (Thomas Lund)
244- in minigzip.c, use static buffers only for 16-bit systems
245- fix suffix check for "minigzip -d foo.gz"
246- do not return an error for the 2nd of two consecutive gzflush() (Felix Lee)
247- use _fdopen instead of fdopen for MSC >= 6.0 (Thomas Fanslau)
248- added makelcc.bat for lcc-win32 (Tom St Denis)
249- in Makefile.dj2, use copy and del instead of install and rm (Frank Donahoe)
250- Avoid expanded $Id$. Use "rcs -kb" or "cvs admin -kb" to avoid Id expansion.
251- check for unistd.h in configure (for off_t)
252- remove useless check parameter in inflate_blocks_free
253- avoid useless assignment of s->check to itself in inflate_blocks_new
254- do not flush twice in gzclose (thanks to Ken Raeburn)
255- rename FOPEN as F_OPEN to avoid clash with /usr/include/sys/file.h
256- use NO_ERRNO_H instead of enumeration of operating systems with errno.h
257- work around buggy fclose on pipes for HP/UX
258- support zlib DLL with BORLAND C++ 5.0 (thanks to Glenn Randers-Pehrson)
259- fix configure if CC is already equal to gcc
260
261Changes in 1.0.5 (3 Jan 98)
262- Fix inflate to terminate gracefully when fed corrupted or invalid data
263- Use const for rommable constants in inflate
264- Eliminate memory leaks on error conditions in inflate
265- Removed some vestigial code in inflate
266- Update web address in README
267
268Changes in 1.0.4 (24 Jul 96)
269- In very rare conditions, deflate(s, Z_FINISH) could fail to produce an EOF
270 bit, so the decompressor could decompress all the correct data but went
271 on to attempt decompressing extra garbage data. This affected minigzip too.
272- zlibVersion and gzerror return const char* (needed for DLL)
273- port to RISCOS (no fdopen, no multiple dots, no unlink, no fileno)
274- use z_error only for DEBUG (avoid problem with DLLs)
275
276Changes in 1.0.3 (2 Jul 96)
277- use z_streamp instead of z_stream *, which is now a far pointer in MSDOS
278 small and medium models; this makes the library incompatible with previous
279 versions for these models. (No effect in large model or on other systems.)
280- return OK instead of BUF_ERROR if previous deflate call returned with
281 avail_out as zero but there is nothing to do
282- added memcmp for non STDC compilers
283- define NO_DUMMY_DECL for more Mac compilers (.h files merged incorrectly)
284- define __32BIT__ if __386__ or i386 is defined (pb. with Watcom and SCO)
285- better check for 16-bit mode MSC (avoids problem with Symantec)
286
287Changes in 1.0.2 (23 May 96)
288- added Windows DLL support
289- added a function zlibVersion (for the DLL support)
290- fixed declarations using Bytef in infutil.c (pb with MSDOS medium model)
291- Bytef is define's instead of typedef'd only for Borland C
292- avoid reading uninitialized memory in example.c
293- mention in README that the zlib format is now RFC1950
294- updated Makefile.dj2
295- added algorithm.doc
296
297Changes in 1.0.1 (20 May 96) [1.0 skipped to avoid confusion]
298- fix array overlay in deflate.c which sometimes caused bad compressed data
299- fix inflate bug with empty stored block
300- fix MSDOS medium model which was broken in 0.99
301- fix deflateParams() which could generated bad compressed data.
302- Bytef is define'd instead of typedef'ed (work around Borland bug)
303- added an INDEX file
304- new makefiles for DJGPP (Makefile.dj2), 32-bit Borland (Makefile.b32),
305 Watcom (Makefile.wat), Amiga SAS/C (Makefile.sas)
306- speed up adler32 for modern machines without auto-increment
307- added -ansi for IRIX in configure
308- static_init_done in trees.c is an int
309- define unlink as delete for VMS
310- fix configure for QNX
311- add configure branch for SCO and HPUX
312- avoid many warnings (unused variables, dead assignments, etc...)
313- no fdopen for BeOS
314- fix the Watcom fix for 32 bit mode (define FAR as empty)
315- removed redefinition of Byte for MKWERKS
316- work around an MWKERKS bug (incorrect merge of all .h files)
317
318Changes in 0.99 (27 Jan 96)
319- allow preset dictionary shared between compressor and decompressor
320- allow compression level 0 (no compression)
321- add deflateParams in zlib.h: allow dynamic change of compression level
322 and compression strategy.
323- test large buffers and deflateParams in example.c
324- add optional "configure" to build zlib as a shared library
325- suppress Makefile.qnx, use configure instead
326- fixed deflate for 64-bit systems (detected on Cray)
327- fixed inflate_blocks for 64-bit systems (detected on Alpha)
328- declare Z_DEFLATED in zlib.h (possible parameter for deflateInit2)
329- always return Z_BUF_ERROR when deflate() has nothing to do
330- deflateInit and inflateInit are now macros to allow version checking
331- prefix all global functions and types with z_ with -DZ_PREFIX
332- make falloc completely reentrant (inftrees.c)
333- fixed very unlikely race condition in ct_static_init
334- free in reverse order of allocation to help memory manager
335- use zlib-1.0/* instead of zlib/* inside the tar.gz
336- make zlib warning-free with "gcc -O3 -Wall -Wwrite-strings -Wpointer-arith
337 -Wconversion -Wstrict-prototypes -Wmissing-prototypes"
338- allow gzread on concatenated .gz files
339- deflateEnd now returns Z_DATA_ERROR if it was premature
340- deflate is finally (?) fully deterministic (no matches beyond end of input)
341- Document Z_SYNC_FLUSH
342- add uninstall in Makefile
343- Check for __cpluplus in zlib.h
344- Better test in ct_align for partial flush
345- avoid harmless warnings for Borland C++
346- initialize hash_head in deflate.c
347- avoid warning on fdopen (gzio.c) for HP cc -Aa
348- include stdlib.h for STDC compilers
349- include errno.h for Cray
350- ignore error if ranlib doesn't exist
351- call ranlib twice for NeXTSTEP
352- use exec_prefix instead of prefix for libz.a
353- renamed ct_* as _tr_* to avoid conflict with applications
354- clear z->msg in inflateInit2 before any error return
355- initialize opaque in example.c, gzio.c, deflate.c and inflate.c
356- fixed typo in zconf.h (_GNUC__ => __GNUC__)
357- check for WIN32 in zconf.h and zutil.c (avoid farmalloc in 32-bit mode)
358- fix typo in Make_vms.com (f$trnlnm -> f$getsyi)
359- in fcalloc, normalize pointer if size > 65520 bytes
360- don't use special fcalloc for 32 bit Borland C++
361- use STDC instead of __GO32__ to avoid redeclaring exit, calloc, etc...
362- use Z_BINARY instead of BINARY
363- document that gzclose after gzdopen will close the file
364- allow "a" as mode in gzopen.
365- fix error checking in gzread
366- allow skipping .gz extra-field on pipes
367- added reference to Perl interface in README
368- put the crc table in FAR data (I dislike more and more the medium model :)
369- added get_crc_table
370- added a dimension to all arrays (Borland C can't count).
371- workaround Borland C bug in declaration of inflate_codes_new & inflate_fast
372- guard against multiple inclusion of *.h (for precompiled header on Mac)
373- Watcom C pretends to be Microsoft C small model even in 32 bit mode.
374- don't use unsized arrays to avoid silly warnings by Visual C++:
375 warning C4746: 'inflate_mask' : unsized array treated as '__far'
376 (what's wrong with far data in far model?).
377- define enum out of inflate_blocks_state to allow compilation with C++
378
379Changes in 0.95 (16 Aug 95)
380- fix MSDOS small and medium model (now easier to adapt to any compiler)
381- inlined send_bits
382- fix the final (:-) bug for deflate with flush (output was correct but
383 not completely flushed in rare occasions).
384- default window size is same for compression and decompression
385 (it's now sufficient to set MAX_WBITS in zconf.h).
386- voidp -> voidpf and voidnp -> voidp (for consistency with other
387 typedefs and because voidnp was not near in large model).
388
389Changes in 0.94 (13 Aug 95)
390- support MSDOS medium model
391- fix deflate with flush (could sometimes generate bad output)
392- fix deflateReset (zlib header was incorrectly suppressed)
393- added support for VMS
394- allow a compression level in gzopen()
395- gzflush now calls fflush
396- For deflate with flush, flush even if no more input is provided.
397- rename libgz.a as libz.a
398- avoid complex expression in infcodes.c triggering Turbo C bug
399- work around a problem with gcc on Alpha (in INSERT_STRING)
400- don't use inline functions (problem with some gcc versions)
401- allow renaming of Byte, uInt, etc... with #define.
402- avoid warning about (unused) pointer before start of array in deflate.c
403- avoid various warnings in gzio.c, example.c, infblock.c, adler32.c, zutil.c
404- avoid reserved word 'new' in trees.c
405
406Changes in 0.93 (25 June 95)
407- temporarily disable inline functions
408- make deflate deterministic
409- give enough lookahead for PARTIAL_FLUSH
410- Set binary mode for stdin/stdout in minigzip.c for OS/2
411- don't even use signed char in inflate (not portable enough)
412- fix inflate memory leak for segmented architectures
413
414Changes in 0.92 (3 May 95)
415- don't assume that char is signed (problem on SGI)
416- Clear bit buffer when starting a stored block
417- no memcpy on Pyramid
418- suppressed inftest.c
419- optimized fill_window, put longest_match inline for gcc
420- optimized inflate on stored blocks.
421- untabify all sources to simplify patches
422
423Changes in 0.91 (2 May 95)
424- Default MEM_LEVEL is 8 (not 9 for Unix) as documented in zlib.h
425- Document the memory requirements in zconf.h
426- added "make install"
427- fix sync search logic in inflateSync
428- deflate(Z_FULL_FLUSH) now works even if output buffer too short
429- after inflateSync, don't scare people with just "lo world"
430- added support for DJGPP
431
432Changes in 0.9 (1 May 95)
433- don't assume that zalloc clears the allocated memory (the TurboC bug
434 was Mark's bug after all :)
435- let again gzread copy uncompressed data unchanged (was working in 0.71)
436- deflate(Z_FULL_FLUSH), inflateReset and inflateSync are now fully implemented
437- added a test of inflateSync in example.c
438- moved MAX_WBITS to zconf.h because users might want to change that.
439- document explicitly that zalloc(64K) on MSDOS must return a normalized
440 pointer (zero offset)
441- added Makefiles for Microsoft C, Turbo C, Borland C++
442- faster crc32()
443
444Changes in 0.8 (29 April 95)
445- added fast inflate (inffast.c)
446- deflate(Z_FINISH) now returns Z_STREAM_END when done. Warning: this
447 is incompatible with previous versions of zlib which returned Z_OK.
448- work around a TurboC compiler bug (bad code for b << 0, see infutil.h)
449 (actually that was not a compiler bug, see 0.81 above)
450- gzread no longer reads one extra byte in certain cases
451- In gzio destroy(), don't reference a freed structure
452- avoid many warnings for MSDOS
453- avoid the ERROR symbol which is used by MS Windows
454
455Changes in 0.71 (14 April 95)
456- Fixed more MSDOS compilation problems :( There is still a bug with
457 TurboC large model.
458
459Changes in 0.7 (14 April 95)
460- Added full inflate support.
461- Simplified the crc32() interface. The pre- and post-conditioning
462 (one's complement) is now done inside crc32(). WARNING: this is
463 incompatible with previous versions; see zlib.h for the new usage.
464
465Changes in 0.61 (12 April 95)
466- workaround for a bug in TurboC. example and minigzip now work on MSDOS.
467
468Changes in 0.6 (11 April 95)
469- added minigzip.c
470- added gzdopen to reopen a file descriptor as gzFile
471- added transparent reading of non-gziped files in gzread.
472- fixed bug in gzread (don't read crc as data)
473- fixed bug in destroy (gzio.c) (don't return Z_STREAM_END for gzclose).
474- don't allocate big arrays in the stack (for MSDOS)
475- fix some MSDOS compilation problems
476
477Changes in 0.5:
478- do real compression in deflate.c. Z_PARTIAL_FLUSH is supported but
479 not yet Z_FULL_FLUSH.
480- support decompression but only in a single step (forced Z_FINISH)
481- added opaque object for zalloc and zfree.
482- added deflateReset and inflateReset
483- added a variable zlib_version for consistency checking.
484- renamed the 'filter' parameter of deflateInit2 as 'strategy'.
485 Added Z_FILTERED and Z_HUFFMAN_ONLY constants.
486
487Changes in 0.4:
488- avoid "zip" everywhere, use zlib instead of ziplib.
489- suppress Z_BLOCK_FLUSH, interpret Z_PARTIAL_FLUSH as block flush
490 if compression method == 8.
491- added adler32 and crc32
492- renamed deflateOptions as deflateInit2, call one or the other but not both
493- added the method parameter for deflateInit2.
494- added inflateInit2
495- simplied considerably deflateInit and inflateInit by not supporting
496 user-provided history buffer. This is supported only in deflateInit2
497 and inflateInit2.
498
499Changes in 0.3:
500- prefix all macro names with Z_
501- use Z_FINISH instead of deflateEnd to finish compression.
502- added Z_HUFFMAN_ONLY
503- added gzerror()
504
505
diff --git a/pwmanager/libcrypt/zlib/README b/pwmanager/libcrypt/zlib/README
new file mode 100644
index 0000000..8ff4587
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/README
@@ -0,0 +1,148 @@
1zlib 1.1.3 is a general purpose data compression library. All the code
2is thread safe. The data format used by the zlib library
3is described by RFCs (Request for Comments) 1950 to 1952 in the files
4ftp://ds.internic.net/rfc/rfc1950.txt (zlib format), rfc1951.txt (deflate
5format) and rfc1952.txt (gzip format). These documents are also available in
6other formats from ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html
7
8All functions of the compression library are documented in the file zlib.h
9(volunteer to write man pages welcome, contact jloup@gzip.org). A usage
10example of the library is given in the file example.c which also tests that
11the library is working correctly. Another example is given in the file
12minigzip.c. The compression library itself is composed of all source files
13except example.c and minigzip.c.
14
15To compile all files and run the test program, follow the instructions
16given at the top of Makefile. In short "make test; make install"
17should work for most machines. For Unix: "configure; make test; make install"
18For MSDOS, use one of the special makefiles such as Makefile.msc.
19For VMS, use Make_vms.com or descrip.mms.
20
21Questions about zlib should be sent to <zlib@quest.jpl.nasa.gov>, or to
22Gilles Vollant <info@winimage.com> for the Windows DLL version.
23The zlib home page is http://www.cdrom.com/pub/infozip/zlib/
24The official zlib ftp site is ftp://ftp.cdrom.com/pub/infozip/zlib/
25Before reporting a problem, please check those sites to verify that
26you have the latest version of zlib; otherwise get the latest version and
27check whether the problem still exists or not.
28
29Mark Nelson <markn@tiny.com> wrote an article about zlib for the Jan. 1997
30issue of Dr. Dobb's Journal; a copy of the article is available in
31http://web2.airmail.net/markn/articles/zlibtool/zlibtool.htm
32
33The changes made in version 1.1.3 are documented in the file ChangeLog.
34The main changes since 1.1.2 are:
35
36- fix "an inflate input buffer bug that shows up on rare but persistent
37 occasions" (Mark)
38- fix gzread and gztell for concatenated .gz files (Didier Le Botlan)
39- fix gzseek(..., SEEK_SET) in write mode
40- fix crc check after a gzeek (Frank Faubert)
41- fix miniunzip when the last entry in a zip file is itself a zip file
42 (J Lillge)
43- add contrib/asm586 and contrib/asm686 (Brian Raiter)
44 See http://www.muppetlabs.com/~breadbox/software/assembly.html
45- add support for Delphi 3 in contrib/delphi (Bob Dellaca)
46- add support for C++Builder 3 and Delphi 3 in contrib/delphi2 (Davide Moretti)
47- do not exit prematurely in untgz if 0 at start of block (Magnus Holmgren)
48- use macro EXTERN instead of extern to support DLL for BeOS (Sander Stoks)
49- added a FAQ file
50
51plus many changes for portability.
52
53Unsupported third party contributions are provided in directory "contrib".
54
55A Java implementation of zlib is available in the Java Development Kit 1.1
56http://www.javasoft.com/products/JDK/1.1/docs/api/Package-java.util.zip.html
57See the zlib home page http://www.cdrom.com/pub/infozip/zlib/ for details.
58
59A Perl interface to zlib written by Paul Marquess <pmarquess@bfsec.bt.co.uk>
60is in the CPAN (Comprehensive Perl Archive Network) sites, such as:
61ftp://ftp.cis.ufl.edu/pub/perl/CPAN/modules/by-module/Compress/Compress-Zlib*
62
63A Python interface to zlib written by A.M. Kuchling <amk@magnet.com>
64is available in Python 1.5 and later versions, see
65http://www.python.org/doc/lib/module-zlib.html
66
67A zlib binding for TCL written by Andreas Kupries <a.kupries@westend.com>
68is availlable at http://www.westend.com/~kupries/doc/trf/man/man.html
69
70An experimental package to read and write files in .zip format,
71written on top of zlib by Gilles Vollant <info@winimage.com>, is
72available at http://www.winimage.com/zLibDll/unzip.html
73and also in the contrib/minizip directory of zlib.
74
75
76Notes for some targets:
77
78- To build a Windows DLL version, include in a DLL project zlib.def, zlib.rc
79 and all .c files except example.c and minigzip.c; compile with -DZLIB_DLL
80 The zlib DLL support was initially done by Alessandro Iacopetti and is
81 now maintained by Gilles Vollant <info@winimage.com>. Check the zlib DLL
82 home page at http://www.winimage.com/zLibDll
83
84 From Visual Basic, you can call the DLL functions which do not take
85 a structure as argument: compress, uncompress and all gz* functions.
86 See contrib/visual-basic.txt for more information, or get
87 http://www.tcfb.com/dowseware/cmp-z-it.zip
88
89- For 64-bit Irix, deflate.c must be compiled without any optimization.
90 With -O, one libpng test fails. The test works in 32 bit mode (with
91 the -n32 compiler flag). The compiler bug has been reported to SGI.
92
93- zlib doesn't work with gcc 2.6.3 on a DEC 3000/300LX under OSF/1 2.1
94 it works when compiled with cc.
95
96- on Digital Unix 4.0D (formely OSF/1) on AlphaServer, the cc option -std1
97 is necessary to get gzprintf working correctly. This is done by configure.
98
99- zlib doesn't work on HP-UX 9.05 with some versions of /bin/cc. It works
100 with other compilers. Use "make test" to check your compiler.
101
102- gzdopen is not supported on RISCOS, BEOS and by some Mac compilers.
103
104- For Turbo C the small model is supported only with reduced performance to
105 avoid any far allocation; it was tested with -DMAX_WBITS=11 -DMAX_MEM_LEVEL=3
106
107- For PalmOs, see http://www.cs.uit.no/~perm/PASTA/pilot/software.html
108 Per Harald Myrvang <perm@stud.cs.uit.no>
109
110
111Acknowledgments:
112
113 The deflate format used by zlib was defined by Phil Katz. The deflate
114 and zlib specifications were written by L. Peter Deutsch. Thanks to all the
115 people who reported problems and suggested various improvements in zlib;
116 they are too numerous to cite here.
117
118Copyright notice:
119
120 (C) 1995-1998 Jean-loup Gailly and Mark Adler
121
122 This software is provided 'as-is', without any express or implied
123 warranty. In no event will the authors be held liable for any damages
124 arising from the use of this software.
125
126 Permission is granted to anyone to use this software for any purpose,
127 including commercial applications, and to alter it and redistribute it
128 freely, subject to the following restrictions:
129
130 1. The origin of this software must not be misrepresented; you must not
131 claim that you wrote the original software. If you use this software
132 in a product, an acknowledgment in the product documentation would be
133 appreciated but is not required.
134 2. Altered source versions must be plainly marked as such, and must not be
135 misrepresented as being the original software.
136 3. This notice may not be removed or altered from any source distribution.
137
138 Jean-loup Gailly Mark Adler
139 jloup@gzip.org madler@alumni.caltech.edu
140
141If you use the zlib library in a product, we would appreciate *not*
142receiving lengthy legal documents to sign. The sources are provided
143for free but without warranty of any kind. The library has been
144entirely written by Jean-loup Gailly and Mark Adler; it does not
145include third-party code.
146
147If you redistribute modified sources, we would appreciate that you include
148in the file ChangeLog history information documenting your changes.
diff --git a/pwmanager/libcrypt/zlib/adler32.c b/pwmanager/libcrypt/zlib/adler32.c
new file mode 100644
index 0000000..fae88b6
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/adler32.c
@@ -0,0 +1,48 @@
1/* adler32.c -- compute the Adler-32 checksum of a data stream
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* @(#) $Id$ */
7
8#include "zlib.h"
9
10#define BASE 65521L /* largest prime smaller than 65536 */
11#define NMAX 5552
12/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
13
14#define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
15#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
16#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
17#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
18#define DO16(buf) DO8(buf,0); DO8(buf,8);
19
20/* ========================================================================= */
21uLong ZEXPORT adler32(adler, buf, len)
22 uLong adler;
23 const Bytef *buf;
24 uInt len;
25{
26 unsigned long s1 = adler & 0xffff;
27 unsigned long s2 = (adler >> 16) & 0xffff;
28 int k;
29
30 if (buf == Z_NULL) return 1L;
31
32 while (len > 0) {
33 k = len < NMAX ? len : NMAX;
34 len -= k;
35 while (k >= 16) {
36 DO16(buf);
37 buf += 16;
38 k -= 16;
39 }
40 if (k != 0) do {
41 s1 += *buf++;
42 s2 += s1;
43 } while (--k);
44 s1 %= BASE;
45 s2 %= BASE;
46 }
47 return (s2 << 16) | s1;
48}
diff --git a/pwmanager/libcrypt/zlib/algorithm.doc b/pwmanager/libcrypt/zlib/algorithm.doc
new file mode 100644
index 0000000..01902af
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/algorithm.doc
@@ -0,0 +1,105 @@
11. Compression algorithm (deflate)
2
3The deflation algorithm used by zlib (also zip and gzip) is a variation of
4LZ77 (Lempel-Ziv 1977, see reference below). It finds duplicated strings in
5the input data. The second occurrence of a string is replaced by a
6pointer to the previous string, in the form of a pair (distance,
7length). Distances are limited to 32K bytes, and lengths are limited
8to 258 bytes. When a string does not occur anywhere in the previous
932K bytes, it is emitted as a sequence of literal bytes. (In this
10description, `string' must be taken as an arbitrary sequence of bytes,
11and is not restricted to printable characters.)
12
13Literals or match lengths are compressed with one Huffman tree, and
14match distances are compressed with another tree. The trees are stored
15in a compact form at the start of each block. The blocks can have any
16size (except that the compressed data for one block must fit in
17available memory). A block is terminated when deflate() determines that
18it would be useful to start another block with fresh trees. (This is
19somewhat similar to the behavior of LZW-based _compress_.)
20
21Duplicated strings are found using a hash table. All input strings of
22length 3 are inserted in the hash table. A hash index is computed for
23the next 3 bytes. If the hash chain for this index is not empty, all
24strings in the chain are compared with the current input string, and
25the longest match is selected.
26
27The hash chains are searched starting with the most recent strings, to
28favor small distances and thus take advantage of the Huffman encoding.
29The hash chains are singly linked. There are no deletions from the
30hash chains, the algorithm simply discards matches that are too old.
31
32To avoid a worst-case situation, very long hash chains are arbitrarily
33truncated at a certain length, determined by a runtime option (level
34parameter of deflateInit). So deflate() does not always find the longest
35possible match but generally finds a match which is long enough.
36
37deflate() also defers the selection of matches with a lazy evaluation
38mechanism. After a match of length N has been found, deflate() searches for a
39longer match at the next input byte. If a longer match is found, the
40previous match is truncated to a length of one (thus producing a single
41literal byte) and the longer match is emitted afterwards. Otherwise,
42the original match is kept, and the next match search is attempted only
43N steps later.
44
45The lazy match evaluation is also subject to a runtime parameter. If
46the current match is long enough, deflate() reduces the search for a longer
47match, thus speeding up the whole process. If compression ratio is more
48important than speed, deflate() attempts a complete second search even if
49the first match is already long enough.
50
51The lazy match evaluation is not performed for the fastest compression
52modes (level parameter 1 to 3). For these fast modes, new strings
53are inserted in the hash table only when no match was found, or
54when the match is not too long. This degrades the compression ratio
55but saves time since there are both fewer insertions and fewer searches.
56
57
582. Decompression algorithm (inflate)
59
60The real question is, given a Huffman tree, how to decode fast. The most
61important realization is that shorter codes are much more common than
62longer codes, so pay attention to decoding the short codes fast, and let
63the long codes take longer to decode.
64
65inflate() sets up a first level table that covers some number of bits of
66input less than the length of longest code. It gets that many bits from the
67stream, and looks it up in the table. The table will tell if the next
68code is that many bits or less and how many, and if it is, it will tell
69the value, else it will point to the next level table for which inflate()
70grabs more bits and tries to decode a longer code.
71
72How many bits to make the first lookup is a tradeoff between the time it
73takes to decode and the time it takes to build the table. If building the
74table took no time (and if you had infinite memory), then there would only
75be a first level table to cover all the way to the longest code. However,
76building the table ends up taking a lot longer for more bits since short
77codes are replicated many times in such a table. What inflate() does is
78simply to make the number of bits in the first table a variable, and set it
79for the maximum speed.
80
81inflate() sends new trees relatively often, so it is possibly set for a
82smaller first level table than an application that has only one tree for
83all the data. For inflate, which has 286 possible codes for the
84literal/length tree, the size of the first table is nine bits. Also the
85distance trees have 30 possible values, and the size of the first table is
86six bits. Note that for each of those cases, the table ended up one bit
87longer than the ``average'' code length, i.e. the code length of an
88approximately flat code which would be a little more than eight bits for
89286 symbols and a little less than five bits for 30 symbols. It would be
90interesting to see if optimizing the first level table for other
91applications gave values within a bit or two of the flat code size.
92
93
94Jean-loup Gailly Mark Adler
95gzip@prep.ai.mit.edu madler@alumni.caltech.edu
96
97
98References:
99
100[LZ77] Ziv J., Lempel A., ``A Universal Algorithm for Sequential Data
101Compression,'' IEEE Transactions on Information Theory, Vol. 23, No. 3,
102pp. 337-343.
103
104``DEFLATE Compressed Data Format Specification'' available in
105ftp://ds.internic.net/rfc/rfc1951.txt
diff --git a/pwmanager/libcrypt/zlib/compress.c b/pwmanager/libcrypt/zlib/compress.c
new file mode 100644
index 0000000..814bd9d
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/compress.c
@@ -0,0 +1,68 @@
1/* compress.c -- compress a memory buffer
2 * Copyright (C) 1995-2002 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* @(#) $Id$ */
7
8#include "zlib.h"
9
10/* ===========================================================================
11 Compresses the source buffer into the destination buffer. The level
12 parameter has the same meaning as in deflateInit. sourceLen is the byte
13 length of the source buffer. Upon entry, destLen is the total size of the
14 destination buffer, which must be at least 0.1% larger than sourceLen plus
15 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
16
17 compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
18 memory, Z_BUF_ERROR if there was not enough room in the output buffer,
19 Z_STREAM_ERROR if the level parameter is invalid.
20*/
21int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
22 Bytef *dest;
23 uLongf *destLen;
24 const Bytef *source;
25 uLong sourceLen;
26 int level;
27{
28 z_stream stream;
29 int err;
30
31 stream.next_in = (Bytef*)source;
32 stream.avail_in = (uInt)sourceLen;
33#ifdef MAXSEG_64K
34 /* Check for source > 64K on 16-bit machine: */
35 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
36#endif
37 stream.next_out = dest;
38 stream.avail_out = (uInt)*destLen;
39 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
40
41 stream.zalloc = (alloc_func)0;
42 stream.zfree = (free_func)0;
43 stream.opaque = (voidpf)0;
44
45 err = deflateInit(&stream, level);
46 if (err != Z_OK) return err;
47
48 err = deflate(&stream, Z_FINISH);
49 if (err != Z_STREAM_END) {
50 deflateEnd(&stream);
51 return err == Z_OK ? Z_BUF_ERROR : err;
52 }
53 *destLen = stream.total_out;
54
55 err = deflateEnd(&stream);
56 return err;
57}
58
59/* ===========================================================================
60 */
61int ZEXPORT compress (dest, destLen, source, sourceLen)
62 Bytef *dest;
63 uLongf *destLen;
64 const Bytef *source;
65 uLong sourceLen;
66{
67 return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
68}
diff --git a/pwmanager/libcrypt/zlib/crc32.c b/pwmanager/libcrypt/zlib/crc32.c
new file mode 100644
index 0000000..81edc19
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/crc32.c
@@ -0,0 +1,163 @@
1/* crc32.c -- compute the CRC-32 of a data stream
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* @(#) $Id$ */
7
8#include "zlib.h"
9
10#define local static
11
12#ifdef DYNAMIC_CRC_TABLE
13
14local int crc_table_empty = 1;
15local uLongf crc_table[256];
16local void make_crc_table OF((void));
17
18/*
19 Generate a table for a byte-wise 32-bit CRC calculation on the polynomial:
20 x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
21
22 Polynomials over GF(2) are represented in binary, one bit per coefficient,
23 with the lowest powers in the most significant bit. Then adding polynomials
24 is just exclusive-or, and multiplying a polynomial by x is a right shift by
25 one. If we call the above polynomial p, and represent a byte as the
26 polynomial q, also with the lowest power in the most significant bit (so the
27 byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
28 where a mod b means the remainder after dividing a by b.
29
30 This calculation is done using the shift-register method of multiplying and
31 taking the remainder. The register is initialized to zero, and for each
32 incoming bit, x^32 is added mod p to the register if the bit is a one (where
33 x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
34 x (which is shifting right by one and adding x^32 mod p if the bit shifted
35 out is a one). We start with the highest power (least significant bit) of
36 q and repeat for all eight bits of q.
37
38 The table is simply the CRC of all possible eight bit values. This is all
39 the information needed to generate CRC's on data a byte at a time for all
40 combinations of CRC register values and incoming bytes.
41*/
42local void make_crc_table()
43{
44 uLong c;
45 int n, k;
46 uLong poly; /* polynomial exclusive-or pattern */
47 /* terms of polynomial defining this crc (except x^32): */
48 static const Byte p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
49
50 /* make exclusive-or pattern from polynomial (0xedb88320L) */
51 poly = 0L;
52 for (n = 0; n < sizeof(p)/sizeof(Byte); n++)
53 poly |= 1L << (31 - p[n]);
54
55 for (n = 0; n < 256; n++)
56 {
57 c = (uLong)n;
58 for (k = 0; k < 8; k++)
59 c = c & 1 ? poly ^ (c >> 1) : c >> 1;
60 crc_table[n] = c;
61 }
62 crc_table_empty = 0;
63}
64#else
65/* ========================================================================
66 * Table of CRC-32's of all single-byte values (made by make_crc_table)
67 */
68local const uLongf crc_table[256] = {
69 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
70 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
71 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
72 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
73 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
74 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
75 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
76 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
77 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
78 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
79 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
80 0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
81 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
82 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
83 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
84 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
85 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
86 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
87 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
88 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
89 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
90 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
91 0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
92 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
93 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
94 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
95 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
96 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
97 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
98 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
99 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
100 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
101 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
102 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
103 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
104 0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
105 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
106 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
107 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
108 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
109 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
110 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
111 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
112 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
113 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
114 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
115 0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
116 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
117 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
118 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
119 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
120 0x2d02ef8dL
121};
122#endif
123
124/* =========================================================================
125 * This function can be used by asm versions of crc32()
126 */
127const uLongf * ZEXPORT get_crc_table()
128{
129#ifdef DYNAMIC_CRC_TABLE
130 if (crc_table_empty) make_crc_table();
131#endif
132 return (const uLongf *)crc_table;
133}
134
135/* ========================================================================= */
136#define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
137#define DO2(buf) DO1(buf); DO1(buf);
138#define DO4(buf) DO2(buf); DO2(buf);
139#define DO8(buf) DO4(buf); DO4(buf);
140
141/* ========================================================================= */
142uLong ZEXPORT crc32(crc, buf, len)
143 uLong crc;
144 const Bytef *buf;
145 uInt len;
146{
147 if (buf == Z_NULL) return 0L;
148#ifdef DYNAMIC_CRC_TABLE
149 if (crc_table_empty)
150 make_crc_table();
151#endif
152 crc = crc ^ 0xffffffffL;
153 while (len >= 8)
154 {
155 DO8(buf);
156 len -= 8;
157 }
158 if (len) do {
159 DO1(buf);
160 } while (--len);
161 return crc ^ 0xffffffffL;
162}
163
diff --git a/pwmanager/libcrypt/zlib/deflate.c b/pwmanager/libcrypt/zlib/deflate.c
new file mode 100644
index 0000000..16ebdad
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/deflate.c
@@ -0,0 +1,1350 @@
1/* deflate.c -- compress data using the deflation algorithm
2 * Copyright (C) 1995-2002 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/*
7 * ALGORITHM
8 *
9 * The "deflation" process depends on being able to identify portions
10 * of the input text which are identical to earlier input (within a
11 * sliding window trailing behind the input currently being processed).
12 *
13 * The most straightforward technique turns out to be the fastest for
14 * most input files: try all possible matches and select the longest.
15 * The key feature of this algorithm is that insertions into the string
16 * dictionary are very simple and thus fast, and deletions are avoided
17 * completely. Insertions are performed at each input character, whereas
18 * string matches are performed only when the previous match ends. So it
19 * is preferable to spend more time in matches to allow very fast string
20 * insertions and avoid deletions. The matching algorithm for small
21 * strings is inspired from that of Rabin & Karp. A brute force approach
22 * is used to find longer strings when a small match has been found.
23 * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
24 * (by Leonid Broukhis).
25 * A previous version of this file used a more sophisticated algorithm
26 * (by Fiala and Greene) which is guaranteed to run in linear amortized
27 * time, but has a larger average cost, uses more memory and is patented.
28 * However the F&G algorithm may be faster for some highly redundant
29 * files if the parameter max_chain_length (described below) is too large.
30 *
31 * ACKNOWLEDGEMENTS
32 *
33 * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
34 * I found it in 'freeze' written by Leonid Broukhis.
35 * Thanks to many people for bug reports and testing.
36 *
37 * REFERENCES
38 *
39 * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
40 * Available in ftp://ds.internic.net/rfc/rfc1951.txt
41 *
42 * A description of the Rabin and Karp algorithm is given in the book
43 * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
44 *
45 * Fiala,E.R., and Greene,D.H.
46 * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
47 *
48 */
49
50/* @(#) $Id$ */
51
52#include "deflate.h"
53
54const char deflate_copyright[] =
55 " deflate 1.1.4 Copyright 1995-2002 Jean-loup Gailly ";
56/*
57 If you use the zlib library in a product, an acknowledgment is welcome
58 in the documentation of your product. If for some reason you cannot
59 include such an acknowledgment, I would appreciate that you keep this
60 copyright string in the executable of your product.
61 */
62
63/* ===========================================================================
64 * Function prototypes.
65 */
66typedef enum {
67 need_more, /* block not completed, need more input or more output */
68 block_done, /* block flush performed */
69 finish_started, /* finish started, need only more output at next deflate */
70 finish_done /* finish done, accept no more input or output */
71} block_state;
72
73typedef block_state (*compress_func) OF((deflate_state *s, int flush));
74/* Compression function. Returns the block state after the call. */
75
76local void fill_window OF((deflate_state *s));
77local block_state deflate_stored OF((deflate_state *s, int flush));
78local block_state deflate_fast OF((deflate_state *s, int flush));
79local block_state deflate_slow OF((deflate_state *s, int flush));
80local void lm_init OF((deflate_state *s));
81local void putShortMSB OF((deflate_state *s, uInt b));
82local void flush_pending OF((z_streamp strm));
83local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
84#ifdef ASMV
85 void match_init OF((void)); /* asm code initialization */
86 uInt longest_match OF((deflate_state *s, IPos cur_match));
87#else
88local uInt longest_match OF((deflate_state *s, IPos cur_match));
89#endif
90
91#ifdef DEBUG
92local void check_match OF((deflate_state *s, IPos start, IPos match,
93 int length));
94#endif
95
96/* ===========================================================================
97 * Local data
98 */
99
100#define NIL 0
101/* Tail of hash chains */
102
103#ifndef TOO_FAR
104# define TOO_FAR 4096
105#endif
106/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
107
108#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
109/* Minimum amount of lookahead, except at the end of the input file.
110 * See deflate.c for comments about the MIN_MATCH+1.
111 */
112
113/* Values for max_lazy_match, good_match and max_chain_length, depending on
114 * the desired pack level (0..9). The values given below have been tuned to
115 * exclude worst case performance for pathological files. Better values may be
116 * found for specific files.
117 */
118typedef struct config_s {
119 ush good_length; /* reduce lazy search above this match length */
120 ush max_lazy; /* do not perform lazy search above this match length */
121 ush nice_length; /* quit search above this match length */
122 ush max_chain;
123 compress_func func;
124} config;
125
126local const config configuration_table[10] = {
127/* good lazy nice chain */
128/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
129/* 1 */ {4, 4, 8, 4, deflate_fast}, /* maximum speed, no lazy matches */
130/* 2 */ {4, 5, 16, 8, deflate_fast},
131/* 3 */ {4, 6, 32, 32, deflate_fast},
132
133/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
134/* 5 */ {8, 16, 32, 32, deflate_slow},
135/* 6 */ {8, 16, 128, 128, deflate_slow},
136/* 7 */ {8, 32, 128, 256, deflate_slow},
137/* 8 */ {32, 128, 258, 1024, deflate_slow},
138/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* maximum compression */
139
140/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
141 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
142 * meaning.
143 */
144
145#define EQUAL 0
146/* result of memcmp for equal strings */
147
148struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
149
150/* ===========================================================================
151 * Update a hash value with the given input byte
152 * IN assertion: all calls to to UPDATE_HASH are made with consecutive
153 * input characters, so that a running hash key can be computed from the
154 * previous key instead of complete recalculation each time.
155 */
156#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
157
158
159/* ===========================================================================
160 * Insert string str in the dictionary and set match_head to the previous head
161 * of the hash chain (the most recent string with same hash key). Return
162 * the previous length of the hash chain.
163 * If this file is compiled with -DFASTEST, the compression level is forced
164 * to 1, and no hash chains are maintained.
165 * IN assertion: all calls to to INSERT_STRING are made with consecutive
166 * input characters and the first MIN_MATCH bytes of str are valid
167 * (except for the last MIN_MATCH-1 bytes of the input file).
168 */
169#ifdef FASTEST
170#define INSERT_STRING(s, str, match_head) \
171 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
172 match_head = s->head[s->ins_h], \
173 s->head[s->ins_h] = (Pos)(str))
174#else
175#define INSERT_STRING(s, str, match_head) \
176 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
177 s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
178 s->head[s->ins_h] = (Pos)(str))
179#endif
180
181/* ===========================================================================
182 * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
183 * prev[] will be initialized on the fly.
184 */
185#define CLEAR_HASH(s) \
186 s->head[s->hash_size-1] = NIL; \
187 zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
188
189/* ========================================================================= */
190int ZEXPORT deflateInit_(strm, level, version, stream_size)
191 z_streamp strm;
192 int level;
193 const char *version;
194 int stream_size;
195{
196 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
197 Z_DEFAULT_STRATEGY, version, stream_size);
198 /* To do: ignore strm->next_in if we use it as window */
199}
200
201/* ========================================================================= */
202int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
203 version, stream_size)
204 z_streamp strm;
205 int level;
206 int method;
207 int windowBits;
208 int memLevel;
209 int strategy;
210 const char *version;
211 int stream_size;
212{
213 deflate_state *s;
214 int noheader = 0;
215 static const char* my_version = ZLIB_VERSION;
216
217 ushf *overlay;
218 /* We overlay pending_buf and d_buf+l_buf. This works since the average
219 * output size for (length,distance) codes is <= 24 bits.
220 */
221
222 if (version == Z_NULL || version[0] != my_version[0] ||
223 stream_size != sizeof(z_stream)) {
224 return Z_VERSION_ERROR;
225 }
226 if (strm == Z_NULL) return Z_STREAM_ERROR;
227
228 strm->msg = Z_NULL;
229 if (strm->zalloc == Z_NULL) {
230 strm->zalloc = zcalloc;
231 strm->opaque = (voidpf)0;
232 }
233 if (strm->zfree == Z_NULL) strm->zfree = zcfree;
234
235 if (level == Z_DEFAULT_COMPRESSION) level = 6;
236#ifdef FASTEST
237 level = 1;
238#endif
239
240 if (windowBits < 0) { /* undocumented feature: suppress zlib header */
241 noheader = 1;
242 windowBits = -windowBits;
243 }
244 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
245 windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
246 strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
247 return Z_STREAM_ERROR;
248 }
249 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
250 if (s == Z_NULL) return Z_MEM_ERROR;
251 strm->state = (struct internal_state FAR *)s;
252 s->strm = strm;
253
254 s->noheader = noheader;
255 s->w_bits = windowBits;
256 s->w_size = 1 << s->w_bits;
257 s->w_mask = s->w_size - 1;
258
259 s->hash_bits = memLevel + 7;
260 s->hash_size = 1 << s->hash_bits;
261 s->hash_mask = s->hash_size - 1;
262 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
263
264 s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
265 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
266 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
267
268 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
269
270 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
271 s->pending_buf = (uchf *) overlay;
272 s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
273
274 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
275 s->pending_buf == Z_NULL) {
276 strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
277 deflateEnd (strm);
278 return Z_MEM_ERROR;
279 }
280 s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
281 s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
282
283 s->level = level;
284 s->strategy = strategy;
285 s->method = (Byte)method;
286
287 return deflateReset(strm);
288}
289
290/* ========================================================================= */
291int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
292 z_streamp strm;
293 const Bytef *dictionary;
294 uInt dictLength;
295{
296 deflate_state *s;
297 uInt length = dictLength;
298 uInt n;
299 IPos hash_head = 0;
300
301 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
302 strm->state->status != INIT_STATE) return Z_STREAM_ERROR;
303
304 s = strm->state;
305 strm->adler = adler32(strm->adler, dictionary, dictLength);
306
307 if (length < MIN_MATCH) return Z_OK;
308 if (length > MAX_DIST(s)) {
309 length = MAX_DIST(s);
310#ifndef USE_DICT_HEAD
311 dictionary += dictLength - length; /* use the tail of the dictionary */
312#endif
313 }
314 zmemcpy(s->window, dictionary, length);
315 s->strstart = length;
316 s->block_start = (long)length;
317
318 /* Insert all strings in the hash table (except for the last two bytes).
319 * s->lookahead stays null, so s->ins_h will be recomputed at the next
320 * call of fill_window.
321 */
322 s->ins_h = s->window[0];
323 UPDATE_HASH(s, s->ins_h, s->window[1]);
324 for (n = 0; n <= length - MIN_MATCH; n++) {
325 INSERT_STRING(s, n, hash_head);
326 }
327 if (hash_head) hash_head = 0; /* to make compiler happy */
328 return Z_OK;
329}
330
331/* ========================================================================= */
332int ZEXPORT deflateReset (strm)
333 z_streamp strm;
334{
335 deflate_state *s;
336
337 if (strm == Z_NULL || strm->state == Z_NULL ||
338 strm->zalloc == Z_NULL || strm->zfree == Z_NULL) return Z_STREAM_ERROR;
339
340 strm->total_in = strm->total_out = 0;
341 strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
342 strm->data_type = Z_UNKNOWN;
343
344 s = (deflate_state *)strm->state;
345 s->pending = 0;
346 s->pending_out = s->pending_buf;
347
348 if (s->noheader < 0) {
349 s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
350 }
351 s->status = s->noheader ? BUSY_STATE : INIT_STATE;
352 strm->adler = 1;
353 s->last_flush = Z_NO_FLUSH;
354
355 _tr_init(s);
356 lm_init(s);
357
358 return Z_OK;
359}
360
361/* ========================================================================= */
362int ZEXPORT deflateParams(strm, level, strategy)
363 z_streamp strm;
364 int level;
365 int strategy;
366{
367 deflate_state *s;
368 compress_func func;
369 int err = Z_OK;
370
371 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
372 s = strm->state;
373
374 if (level == Z_DEFAULT_COMPRESSION) {
375 level = 6;
376 }
377 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
378 return Z_STREAM_ERROR;
379 }
380 func = configuration_table[s->level].func;
381
382 if (func != configuration_table[level].func && strm->total_in != 0) {
383 /* Flush the last buffer: */
384 err = deflate(strm, Z_PARTIAL_FLUSH);
385 }
386 if (s->level != level) {
387 s->level = level;
388 s->max_lazy_match = configuration_table[level].max_lazy;
389 s->good_match = configuration_table[level].good_length;
390 s->nice_match = configuration_table[level].nice_length;
391 s->max_chain_length = configuration_table[level].max_chain;
392 }
393 s->strategy = strategy;
394 return err;
395}
396
397/* =========================================================================
398 * Put a short in the pending buffer. The 16-bit value is put in MSB order.
399 * IN assertion: the stream state is correct and there is enough room in
400 * pending_buf.
401 */
402local void putShortMSB (s, b)
403 deflate_state *s;
404 uInt b;
405{
406 put_byte(s, (Byte)(b >> 8));
407 put_byte(s, (Byte)(b & 0xff));
408}
409
410/* =========================================================================
411 * Flush as much pending output as possible. All deflate() output goes
412 * through this function so some applications may wish to modify it
413 * to avoid allocating a large strm->next_out buffer and copying into it.
414 * (See also read_buf()).
415 */
416local void flush_pending(strm)
417 z_streamp strm;
418{
419 unsigned len = strm->state->pending;
420
421 if (len > strm->avail_out) len = strm->avail_out;
422 if (len == 0) return;
423
424 zmemcpy(strm->next_out, strm->state->pending_out, len);
425 strm->next_out += len;
426 strm->state->pending_out += len;
427 strm->total_out += len;
428 strm->avail_out -= len;
429 strm->state->pending -= len;
430 if (strm->state->pending == 0) {
431 strm->state->pending_out = strm->state->pending_buf;
432 }
433}
434
435/* ========================================================================= */
436int ZEXPORT deflate (strm, flush)
437 z_streamp strm;
438 int flush;
439{
440 int old_flush; /* value of flush param for previous deflate call */
441 deflate_state *s;
442
443 if (strm == Z_NULL || strm->state == Z_NULL ||
444 flush > Z_FINISH || flush < 0) {
445 return Z_STREAM_ERROR;
446 }
447 s = strm->state;
448
449 if (strm->next_out == Z_NULL ||
450 (strm->next_in == Z_NULL && strm->avail_in != 0) ||
451 (s->status == FINISH_STATE && flush != Z_FINISH)) {
452 ERR_RETURN(strm, Z_STREAM_ERROR);
453 }
454 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
455
456 s->strm = strm; /* just in case */
457 old_flush = s->last_flush;
458 s->last_flush = flush;
459
460 /* Write the zlib header */
461 if (s->status == INIT_STATE) {
462
463 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
464 uInt level_flags = (s->level-1) >> 1;
465
466 if (level_flags > 3) level_flags = 3;
467 header |= (level_flags << 6);
468 if (s->strstart != 0) header |= PRESET_DICT;
469 header += 31 - (header % 31);
470
471 s->status = BUSY_STATE;
472 putShortMSB(s, header);
473
474 /* Save the adler32 of the preset dictionary: */
475 if (s->strstart != 0) {
476 putShortMSB(s, (uInt)(strm->adler >> 16));
477 putShortMSB(s, (uInt)(strm->adler & 0xffff));
478 }
479 strm->adler = 1L;
480 }
481
482 /* Flush as much pending output as possible */
483 if (s->pending != 0) {
484 flush_pending(strm);
485 if (strm->avail_out == 0) {
486 /* Since avail_out is 0, deflate will be called again with
487 * more output space, but possibly with both pending and
488 * avail_in equal to zero. There won't be anything to do,
489 * but this is not an error situation so make sure we
490 * return OK instead of BUF_ERROR at next call of deflate:
491 */
492 s->last_flush = -1;
493 return Z_OK;
494 }
495
496 /* Make sure there is something to do and avoid duplicate consecutive
497 * flushes. For repeated and useless calls with Z_FINISH, we keep
498 * returning Z_STREAM_END instead of Z_BUFF_ERROR.
499 */
500 } else if (strm->avail_in == 0 && flush <= old_flush &&
501 flush != Z_FINISH) {
502 ERR_RETURN(strm, Z_BUF_ERROR);
503 }
504
505 /* User must not provide more input after the first FINISH: */
506 if (s->status == FINISH_STATE && strm->avail_in != 0) {
507 ERR_RETURN(strm, Z_BUF_ERROR);
508 }
509
510 /* Start a new block or continue the current one.
511 */
512 if (strm->avail_in != 0 || s->lookahead != 0 ||
513 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
514 block_state bstate;
515
516 bstate = (*(configuration_table[s->level].func))(s, flush);
517
518 if (bstate == finish_started || bstate == finish_done) {
519 s->status = FINISH_STATE;
520 }
521 if (bstate == need_more || bstate == finish_started) {
522 if (strm->avail_out == 0) {
523 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
524 }
525 return Z_OK;
526 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
527 * of deflate should use the same flush parameter to make sure
528 * that the flush is complete. So we don't have to output an
529 * empty block here, this will be done at next call. This also
530 * ensures that for a very small output buffer, we emit at most
531 * one empty block.
532 */
533 }
534 if (bstate == block_done) {
535 if (flush == Z_PARTIAL_FLUSH) {
536 _tr_align(s);
537 } else { /* FULL_FLUSH or SYNC_FLUSH */
538 _tr_stored_block(s, (char*)0, 0L, 0);
539 /* For a full flush, this empty block will be recognized
540 * as a special marker by inflate_sync().
541 */
542 if (flush == Z_FULL_FLUSH) {
543 CLEAR_HASH(s); /* forget history */
544 }
545 }
546 flush_pending(strm);
547 if (strm->avail_out == 0) {
548 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
549 return Z_OK;
550 }
551 }
552 }
553 Assert(strm->avail_out > 0, "bug2");
554
555 if (flush != Z_FINISH) return Z_OK;
556 if (s->noheader) return Z_STREAM_END;
557
558 /* Write the zlib trailer (adler32) */
559 putShortMSB(s, (uInt)(strm->adler >> 16));
560 putShortMSB(s, (uInt)(strm->adler & 0xffff));
561 flush_pending(strm);
562 /* If avail_out is zero, the application will call deflate again
563 * to flush the rest.
564 */
565 s->noheader = -1; /* write the trailer only once! */
566 return s->pending != 0 ? Z_OK : Z_STREAM_END;
567}
568
569/* ========================================================================= */
570int ZEXPORT deflateEnd (strm)
571 z_streamp strm;
572{
573 int status;
574
575 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
576
577 status = strm->state->status;
578 if (status != INIT_STATE && status != BUSY_STATE &&
579 status != FINISH_STATE) {
580 return Z_STREAM_ERROR;
581 }
582
583 /* Deallocate in reverse order of allocations: */
584 TRY_FREE(strm, strm->state->pending_buf);
585 TRY_FREE(strm, strm->state->head);
586 TRY_FREE(strm, strm->state->prev);
587 TRY_FREE(strm, strm->state->window);
588
589 ZFREE(strm, strm->state);
590 strm->state = Z_NULL;
591
592 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
593}
594
595/* =========================================================================
596 * Copy the source state to the destination state.
597 * To simplify the source, this is not supported for 16-bit MSDOS (which
598 * doesn't have enough memory anyway to duplicate compression states).
599 */
600int ZEXPORT deflateCopy (dest, source)
601 z_streamp dest;
602 z_streamp source;
603{
604#ifdef MAXSEG_64K
605 return Z_STREAM_ERROR;
606#else
607 deflate_state *ds;
608 deflate_state *ss;
609 ushf *overlay;
610
611
612 if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
613 return Z_STREAM_ERROR;
614 }
615
616 ss = source->state;
617
618 *dest = *source;
619
620 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
621 if (ds == Z_NULL) return Z_MEM_ERROR;
622 dest->state = (struct internal_state FAR *) ds;
623 *ds = *ss;
624 ds->strm = dest;
625
626 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
627 ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
628 ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
629 overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
630 ds->pending_buf = (uchf *) overlay;
631
632 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
633 ds->pending_buf == Z_NULL) {
634 deflateEnd (dest);
635 return Z_MEM_ERROR;
636 }
637 /* following zmemcpy do not work for 16-bit MSDOS */
638 zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
639 zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
640 zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
641 zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
642
643 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
644 ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
645 ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
646
647 ds->l_desc.dyn_tree = ds->dyn_ltree;
648 ds->d_desc.dyn_tree = ds->dyn_dtree;
649 ds->bl_desc.dyn_tree = ds->bl_tree;
650
651 return Z_OK;
652#endif
653}
654
655/* ===========================================================================
656 * Read a new buffer from the current input stream, update the adler32
657 * and total number of bytes read. All deflate() input goes through
658 * this function so some applications may wish to modify it to avoid
659 * allocating a large strm->next_in buffer and copying from it.
660 * (See also flush_pending()).
661 */
662local int read_buf(strm, buf, size)
663 z_streamp strm;
664 Bytef *buf;
665 unsigned size;
666{
667 unsigned len = strm->avail_in;
668
669 if (len > size) len = size;
670 if (len == 0) return 0;
671
672 strm->avail_in -= len;
673
674 if (!strm->state->noheader) {
675 strm->adler = adler32(strm->adler, strm->next_in, len);
676 }
677 zmemcpy(buf, strm->next_in, len);
678 strm->next_in += len;
679 strm->total_in += len;
680
681 return (int)len;
682}
683
684/* ===========================================================================
685 * Initialize the "longest match" routines for a new zlib stream
686 */
687local void lm_init (s)
688 deflate_state *s;
689{
690 s->window_size = (ulg)2L*s->w_size;
691
692 CLEAR_HASH(s);
693
694 /* Set the default configuration parameters:
695 */
696 s->max_lazy_match = configuration_table[s->level].max_lazy;
697 s->good_match = configuration_table[s->level].good_length;
698 s->nice_match = configuration_table[s->level].nice_length;
699 s->max_chain_length = configuration_table[s->level].max_chain;
700
701 s->strstart = 0;
702 s->block_start = 0L;
703 s->lookahead = 0;
704 s->match_length = s->prev_length = MIN_MATCH-1;
705 s->match_available = 0;
706 s->ins_h = 0;
707#ifdef ASMV
708 match_init(); /* initialize the asm code */
709#endif
710}
711
712/* ===========================================================================
713 * Set match_start to the longest match starting at the given string and
714 * return its length. Matches shorter or equal to prev_length are discarded,
715 * in which case the result is equal to prev_length and match_start is
716 * garbage.
717 * IN assertions: cur_match is the head of the hash chain for the current
718 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
719 * OUT assertion: the match length is not greater than s->lookahead.
720 */
721#ifndef ASMV
722/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
723 * match.S. The code will be functionally equivalent.
724 */
725#ifndef FASTEST
726local uInt longest_match(s, cur_match)
727 deflate_state *s;
728 IPos cur_match; /* current match */
729{
730 unsigned chain_length = s->max_chain_length;/* max hash chain length */
731 register Bytef *scan = s->window + s->strstart; /* current string */
732 register Bytef *match; /* matched string */
733 register int len; /* length of current match */
734 int best_len = s->prev_length; /* best match length so far */
735 int nice_match = s->nice_match; /* stop if match long enough */
736 IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
737 s->strstart - (IPos)MAX_DIST(s) : NIL;
738 /* Stop when cur_match becomes <= limit. To simplify the code,
739 * we prevent matches with the string of window index 0.
740 */
741 Posf *prev = s->prev;
742 uInt wmask = s->w_mask;
743
744#ifdef UNALIGNED_OK
745 /* Compare two bytes at a time. Note: this is not always beneficial.
746 * Try with and without -DUNALIGNED_OK to check.
747 */
748 register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
749 register ush scan_start = *(ushf*)scan;
750 register ush scan_end = *(ushf*)(scan+best_len-1);
751#else
752 register Bytef *strend = s->window + s->strstart + MAX_MATCH;
753 register Byte scan_end1 = scan[best_len-1];
754 register Byte scan_end = scan[best_len];
755#endif
756
757 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
758 * It is easy to get rid of this optimization if necessary.
759 */
760 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
761
762 /* Do not waste too much time if we already have a good match: */
763 if (s->prev_length >= s->good_match) {
764 chain_length >>= 2;
765 }
766 /* Do not look for matches beyond the end of the input. This is necessary
767 * to make deflate deterministic.
768 */
769 if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
770
771 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
772
773 do {
774 Assert(cur_match < s->strstart, "no future");
775 match = s->window + cur_match;
776
777 /* Skip to next match if the match length cannot increase
778 * or if the match length is less than 2:
779 */
780#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
781 /* This code assumes sizeof(unsigned short) == 2. Do not use
782 * UNALIGNED_OK if your compiler uses a different size.
783 */
784 if (*(ushf*)(match+best_len-1) != scan_end ||
785 *(ushf*)match != scan_start) continue;
786
787 /* It is not necessary to compare scan[2] and match[2] since they are
788 * always equal when the other bytes match, given that the hash keys
789 * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
790 * strstart+3, +5, ... up to strstart+257. We check for insufficient
791 * lookahead only every 4th comparison; the 128th check will be made
792 * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
793 * necessary to put more guard bytes at the end of the window, or
794 * to check more often for insufficient lookahead.
795 */
796 Assert(scan[2] == match[2], "scan[2]?");
797 scan++, match++;
798 do {
799 } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
800 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
801 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
802 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
803 scan < strend);
804 /* The funny "do {}" generates better code on most compilers */
805
806 /* Here, scan <= window+strstart+257 */
807 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
808 if (*scan == *match) scan++;
809
810 len = (MAX_MATCH - 1) - (int)(strend-scan);
811 scan = strend - (MAX_MATCH-1);
812
813#else /* UNALIGNED_OK */
814
815 if (match[best_len] != scan_end ||
816 match[best_len-1] != scan_end1 ||
817 *match != *scan ||
818 *++match != scan[1]) continue;
819
820 /* The check at best_len-1 can be removed because it will be made
821 * again later. (This heuristic is not always a win.)
822 * It is not necessary to compare scan[2] and match[2] since they
823 * are always equal when the other bytes match, given that
824 * the hash keys are equal and that HASH_BITS >= 8.
825 */
826 scan += 2, match++;
827 Assert(*scan == *match, "match[2]?");
828
829 /* We check for insufficient lookahead only every 8th comparison;
830 * the 256th check will be made at strstart+258.
831 */
832 do {
833 } while (*++scan == *++match && *++scan == *++match &&
834 *++scan == *++match && *++scan == *++match &&
835 *++scan == *++match && *++scan == *++match &&
836 *++scan == *++match && *++scan == *++match &&
837 scan < strend);
838
839 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
840
841 len = MAX_MATCH - (int)(strend - scan);
842 scan = strend - MAX_MATCH;
843
844#endif /* UNALIGNED_OK */
845
846 if (len > best_len) {
847 s->match_start = cur_match;
848 best_len = len;
849 if (len >= nice_match) break;
850#ifdef UNALIGNED_OK
851 scan_end = *(ushf*)(scan+best_len-1);
852#else
853 scan_end1 = scan[best_len-1];
854 scan_end = scan[best_len];
855#endif
856 }
857 } while ((cur_match = prev[cur_match & wmask]) > limit
858 && --chain_length != 0);
859
860 if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
861 return s->lookahead;
862}
863
864#else /* FASTEST */
865/* ---------------------------------------------------------------------------
866 * Optimized version for level == 1 only
867 */
868local uInt longest_match(s, cur_match)
869 deflate_state *s;
870 IPos cur_match; /* current match */
871{
872 register Bytef *scan = s->window + s->strstart; /* current string */
873 register Bytef *match; /* matched string */
874 register int len; /* length of current match */
875 register Bytef *strend = s->window + s->strstart + MAX_MATCH;
876
877 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
878 * It is easy to get rid of this optimization if necessary.
879 */
880 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
881
882 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
883
884 Assert(cur_match < s->strstart, "no future");
885
886 match = s->window + cur_match;
887
888 /* Return failure if the match length is less than 2:
889 */
890 if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
891
892 /* The check at best_len-1 can be removed because it will be made
893 * again later. (This heuristic is not always a win.)
894 * It is not necessary to compare scan[2] and match[2] since they
895 * are always equal when the other bytes match, given that
896 * the hash keys are equal and that HASH_BITS >= 8.
897 */
898 scan += 2, match += 2;
899 Assert(*scan == *match, "match[2]?");
900
901 /* We check for insufficient lookahead only every 8th comparison;
902 * the 256th check will be made at strstart+258.
903 */
904 do {
905 } while (*++scan == *++match && *++scan == *++match &&
906 *++scan == *++match && *++scan == *++match &&
907 *++scan == *++match && *++scan == *++match &&
908 *++scan == *++match && *++scan == *++match &&
909 scan < strend);
910
911 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
912
913 len = MAX_MATCH - (int)(strend - scan);
914
915 if (len < MIN_MATCH) return MIN_MATCH - 1;
916
917 s->match_start = cur_match;
918 return len <= s->lookahead ? len : s->lookahead;
919}
920#endif /* FASTEST */
921#endif /* ASMV */
922
923#ifdef DEBUG
924/* ===========================================================================
925 * Check that the match at match_start is indeed a match.
926 */
927local void check_match(s, start, match, length)
928 deflate_state *s;
929 IPos start, match;
930 int length;
931{
932 /* check that the match is indeed a match */
933 if (zmemcmp(s->window + match,
934 s->window + start, length) != EQUAL) {
935 fprintf(stderr, " start %u, match %u, length %d\n",
936 start, match, length);
937 do {
938 fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
939 } while (--length != 0);
940 z_error("invalid match");
941 }
942 if (z_verbose > 1) {
943 fprintf(stderr,"\\[%d,%d]", start-match, length);
944 do { putc(s->window[start++], stderr); } while (--length != 0);
945 }
946}
947#else
948# define check_match(s, start, match, length)
949#endif
950
951/* ===========================================================================
952 * Fill the window when the lookahead becomes insufficient.
953 * Updates strstart and lookahead.
954 *
955 * IN assertion: lookahead < MIN_LOOKAHEAD
956 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
957 * At least one byte has been read, or avail_in == 0; reads are
958 * performed for at least two bytes (required for the zip translate_eol
959 * option -- not supported here).
960 */
961local void fill_window(s)
962 deflate_state *s;
963{
964 register unsigned n, m;
965 register Posf *p;
966 unsigned more; /* Amount of free space at the end of the window. */
967 uInt wsize = s->w_size;
968
969 do {
970 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
971
972 /* Deal with !@#$% 64K limit: */
973 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
974 more = wsize;
975
976 } else if (more == (unsigned)(-1)) {
977 /* Very unlikely, but possible on 16 bit machine if strstart == 0
978 * and lookahead == 1 (input done one byte at time)
979 */
980 more--;
981
982 /* If the window is almost full and there is insufficient lookahead,
983 * move the upper half to the lower one to make room in the upper half.
984 */
985 } else if (s->strstart >= wsize+MAX_DIST(s)) {
986
987 zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
988 s->match_start -= wsize;
989 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
990 s->block_start -= (long) wsize;
991
992 /* Slide the hash table (could be avoided with 32 bit values
993 at the expense of memory usage). We slide even when level == 0
994 to keep the hash table consistent if we switch back to level > 0
995 later. (Using level 0 permanently is not an optimal usage of
996 zlib, so we don't care about this pathological case.)
997 */
998 n = s->hash_size;
999 p = &s->head[n];
1000 do {
1001 m = *--p;
1002 *p = (Pos)(m >= wsize ? m-wsize : NIL);
1003 } while (--n);
1004
1005 n = wsize;
1006#ifndef FASTEST
1007 p = &s->prev[n];
1008 do {
1009 m = *--p;
1010 *p = (Pos)(m >= wsize ? m-wsize : NIL);
1011 /* If n is not on any hash chain, prev[n] is garbage but
1012 * its value will never be used.
1013 */
1014 } while (--n);
1015#endif
1016 more += wsize;
1017 }
1018 if (s->strm->avail_in == 0) return;
1019
1020 /* If there was no sliding:
1021 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
1022 * more == window_size - lookahead - strstart
1023 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
1024 * => more >= window_size - 2*WSIZE + 2
1025 * In the BIG_MEM or MMAP case (not yet supported),
1026 * window_size == input_size + MIN_LOOKAHEAD &&
1027 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
1028 * Otherwise, window_size == 2*WSIZE so more >= 2.
1029 * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
1030 */
1031 Assert(more >= 2, "more < 2");
1032
1033 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1034 s->lookahead += n;
1035
1036 /* Initialize the hash value now that we have some input: */
1037 if (s->lookahead >= MIN_MATCH) {
1038 s->ins_h = s->window[s->strstart];
1039 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1040#if MIN_MATCH != 3
1041 Call UPDATE_HASH() MIN_MATCH-3 more times
1042#endif
1043 }
1044 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
1045 * but this is not important since only literal bytes will be emitted.
1046 */
1047
1048 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1049}
1050
1051/* ===========================================================================
1052 * Flush the current block, with given end-of-file flag.
1053 * IN assertion: strstart is set to the end of the current match.
1054 */
1055#define FLUSH_BLOCK_ONLY(s, eof) { \
1056 _tr_flush_block(s, (s->block_start >= 0L ? \
1057 (charf *)&s->window[(unsigned)s->block_start] : \
1058 (charf *)Z_NULL), \
1059 (ulg)((long)s->strstart - s->block_start), \
1060 (eof)); \
1061 s->block_start = s->strstart; \
1062 flush_pending(s->strm); \
1063 Tracev((stderr,"[FLUSH]")); \
1064}
1065
1066/* Same but force premature exit if necessary. */
1067#define FLUSH_BLOCK(s, eof) { \
1068 FLUSH_BLOCK_ONLY(s, eof); \
1069 if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
1070}
1071
1072/* ===========================================================================
1073 * Copy without compression as much as possible from the input stream, return
1074 * the current block state.
1075 * This function does not insert new strings in the dictionary since
1076 * uncompressible data is probably not useful. This function is used
1077 * only for the level=0 compression option.
1078 * NOTE: this function should be optimized to avoid extra copying from
1079 * window to pending_buf.
1080 */
1081local block_state deflate_stored(s, flush)
1082 deflate_state *s;
1083 int flush;
1084{
1085 /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
1086 * to pending_buf_size, and each stored block has a 5 byte header:
1087 */
1088 ulg max_block_size = 0xffff;
1089 ulg max_start;
1090
1091 if (max_block_size > s->pending_buf_size - 5) {
1092 max_block_size = s->pending_buf_size - 5;
1093 }
1094
1095 /* Copy as much as possible from input to output: */
1096 for (;;) {
1097 /* Fill the window as much as possible: */
1098 if (s->lookahead <= 1) {
1099
1100 Assert(s->strstart < s->w_size+MAX_DIST(s) ||
1101 s->block_start >= (long)s->w_size, "slide too late");
1102
1103 fill_window(s);
1104 if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
1105
1106 if (s->lookahead == 0) break; /* flush the current block */
1107 }
1108 Assert(s->block_start >= 0L, "block gone");
1109
1110 s->strstart += s->lookahead;
1111 s->lookahead = 0;
1112
1113 /* Emit a stored block if pending_buf will be full: */
1114 max_start = s->block_start + max_block_size;
1115 if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
1116 /* strstart == 0 is possible when wraparound on 16-bit machine */
1117 s->lookahead = (uInt)(s->strstart - max_start);
1118 s->strstart = (uInt)max_start;
1119 FLUSH_BLOCK(s, 0);
1120 }
1121 /* Flush if we may have to slide, otherwise block_start may become
1122 * negative and the data will be gone:
1123 */
1124 if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
1125 FLUSH_BLOCK(s, 0);
1126 }
1127 }
1128 FLUSH_BLOCK(s, flush == Z_FINISH);
1129 return flush == Z_FINISH ? finish_done : block_done;
1130}
1131
1132/* ===========================================================================
1133 * Compress as much as possible from the input stream, return the current
1134 * block state.
1135 * This function does not perform lazy evaluation of matches and inserts
1136 * new strings in the dictionary only for unmatched strings or for short
1137 * matches. It is used only for the fast compression options.
1138 */
1139local block_state deflate_fast(s, flush)
1140 deflate_state *s;
1141 int flush;
1142{
1143 IPos hash_head = NIL; /* head of the hash chain */
1144 int bflush; /* set if current block must be flushed */
1145
1146 for (;;) {
1147 /* Make sure that we always have enough lookahead, except
1148 * at the end of the input file. We need MAX_MATCH bytes
1149 * for the next match, plus MIN_MATCH bytes to insert the
1150 * string following the next match.
1151 */
1152 if (s->lookahead < MIN_LOOKAHEAD) {
1153 fill_window(s);
1154 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1155 return need_more;
1156 }
1157 if (s->lookahead == 0) break; /* flush the current block */
1158 }
1159
1160 /* Insert the string window[strstart .. strstart+2] in the
1161 * dictionary, and set hash_head to the head of the hash chain:
1162 */
1163 if (s->lookahead >= MIN_MATCH) {
1164 INSERT_STRING(s, s->strstart, hash_head);
1165 }
1166
1167 /* Find the longest match, discarding those <= prev_length.
1168 * At this point we have always match_length < MIN_MATCH
1169 */
1170 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1171 /* To simplify the code, we prevent matches with the string
1172 * of window index 0 (in particular we have to avoid a match
1173 * of the string with itself at the start of the input file).
1174 */
1175 if (s->strategy != Z_HUFFMAN_ONLY) {
1176 s->match_length = longest_match (s, hash_head);
1177 }
1178 /* longest_match() sets match_start */
1179 }
1180 if (s->match_length >= MIN_MATCH) {
1181 check_match(s, s->strstart, s->match_start, s->match_length);
1182
1183 _tr_tally_dist(s, s->strstart - s->match_start,
1184 s->match_length - MIN_MATCH, bflush);
1185
1186 s->lookahead -= s->match_length;
1187
1188 /* Insert new strings in the hash table only if the match length
1189 * is not too large. This saves time but degrades compression.
1190 */
1191#ifndef FASTEST
1192 if (s->match_length <= s->max_insert_length &&
1193 s->lookahead >= MIN_MATCH) {
1194 s->match_length--; /* string at strstart already in hash table */
1195 do {
1196 s->strstart++;
1197 INSERT_STRING(s, s->strstart, hash_head);
1198 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
1199 * always MIN_MATCH bytes ahead.
1200 */
1201 } while (--s->match_length != 0);
1202 s->strstart++;
1203 } else
1204#endif
1205 {
1206 s->strstart += s->match_length;
1207 s->match_length = 0;
1208 s->ins_h = s->window[s->strstart];
1209 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1210#if MIN_MATCH != 3
1211 Call UPDATE_HASH() MIN_MATCH-3 more times
1212#endif
1213 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
1214 * matter since it will be recomputed at next deflate call.
1215 */
1216 }
1217 } else {
1218 /* No match, output a literal byte */
1219 Tracevv((stderr,"%c", s->window[s->strstart]));
1220 _tr_tally_lit (s, s->window[s->strstart], bflush);
1221 s->lookahead--;
1222 s->strstart++;
1223 }
1224 if (bflush) FLUSH_BLOCK(s, 0);
1225 }
1226 FLUSH_BLOCK(s, flush == Z_FINISH);
1227 return flush == Z_FINISH ? finish_done : block_done;
1228}
1229
1230/* ===========================================================================
1231 * Same as above, but achieves better compression. We use a lazy
1232 * evaluation for matches: a match is finally adopted only if there is
1233 * no better match at the next window position.
1234 */
1235local block_state deflate_slow(s, flush)
1236 deflate_state *s;
1237 int flush;
1238{
1239 IPos hash_head = NIL; /* head of hash chain */
1240 int bflush; /* set if current block must be flushed */
1241
1242 /* Process the input block. */
1243 for (;;) {
1244 /* Make sure that we always have enough lookahead, except
1245 * at the end of the input file. We need MAX_MATCH bytes
1246 * for the next match, plus MIN_MATCH bytes to insert the
1247 * string following the next match.
1248 */
1249 if (s->lookahead < MIN_LOOKAHEAD) {
1250 fill_window(s);
1251 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1252 return need_more;
1253 }
1254 if (s->lookahead == 0) break; /* flush the current block */
1255 }
1256
1257 /* Insert the string window[strstart .. strstart+2] in the
1258 * dictionary, and set hash_head to the head of the hash chain:
1259 */
1260 if (s->lookahead >= MIN_MATCH) {
1261 INSERT_STRING(s, s->strstart, hash_head);
1262 }
1263
1264 /* Find the longest match, discarding those <= prev_length.
1265 */
1266 s->prev_length = s->match_length, s->prev_match = s->match_start;
1267 s->match_length = MIN_MATCH-1;
1268
1269 if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
1270 s->strstart - hash_head <= MAX_DIST(s)) {
1271 /* To simplify the code, we prevent matches with the string
1272 * of window index 0 (in particular we have to avoid a match
1273 * of the string with itself at the start of the input file).
1274 */
1275 if (s->strategy != Z_HUFFMAN_ONLY) {
1276 s->match_length = longest_match (s, hash_head);
1277 }
1278 /* longest_match() sets match_start */
1279
1280 if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
1281 (s->match_length == MIN_MATCH &&
1282 s->strstart - s->match_start > TOO_FAR))) {
1283
1284 /* If prev_match is also MIN_MATCH, match_start is garbage
1285 * but we will ignore the current match anyway.
1286 */
1287 s->match_length = MIN_MATCH-1;
1288 }
1289 }
1290 /* If there was a match at the previous step and the current
1291 * match is not better, output the previous match:
1292 */
1293 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1294 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1295 /* Do not insert strings in hash table beyond this. */
1296
1297 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1298
1299 _tr_tally_dist(s, s->strstart -1 - s->prev_match,
1300 s->prev_length - MIN_MATCH, bflush);
1301
1302 /* Insert in hash table all strings up to the end of the match.
1303 * strstart-1 and strstart are already inserted. If there is not
1304 * enough lookahead, the last two strings are not inserted in
1305 * the hash table.
1306 */
1307 s->lookahead -= s->prev_length-1;
1308 s->prev_length -= 2;
1309 do {
1310 if (++s->strstart <= max_insert) {
1311 INSERT_STRING(s, s->strstart, hash_head);
1312 }
1313 } while (--s->prev_length != 0);
1314 s->match_available = 0;
1315 s->match_length = MIN_MATCH-1;
1316 s->strstart++;
1317
1318 if (bflush) FLUSH_BLOCK(s, 0);
1319
1320 } else if (s->match_available) {
1321 /* If there was no match at the previous position, output a
1322 * single literal. If there was a match but the current match
1323 * is longer, truncate the previous match to a single literal.
1324 */
1325 Tracevv((stderr,"%c", s->window[s->strstart-1]));
1326 _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1327 if (bflush) {
1328 FLUSH_BLOCK_ONLY(s, 0);
1329 }
1330 s->strstart++;
1331 s->lookahead--;
1332 if (s->strm->avail_out == 0) return need_more;
1333 } else {
1334 /* There is no previous match to compare with, wait for
1335 * the next step to decide.
1336 */
1337 s->match_available = 1;
1338 s->strstart++;
1339 s->lookahead--;
1340 }
1341 }
1342 Assert (flush != Z_NO_FLUSH, "no flush?");
1343 if (s->match_available) {
1344 Tracevv((stderr,"%c", s->window[s->strstart-1]));
1345 _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1346 s->match_available = 0;
1347 }
1348 FLUSH_BLOCK(s, flush == Z_FINISH);
1349 return flush == Z_FINISH ? finish_done : block_done;
1350}
diff --git a/pwmanager/libcrypt/zlib/deflate.h b/pwmanager/libcrypt/zlib/deflate.h
new file mode 100644
index 0000000..b99a48a
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/deflate.h
@@ -0,0 +1,318 @@
1/* deflate.h -- internal compression state
2 * Copyright (C) 1995-2002 Jean-loup Gailly
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* WARNING: this file should *not* be used by applications. It is
7 part of the implementation of the compression library and is
8 subject to change. Applications should only use zlib.h.
9 */
10
11/* @(#) $Id$ */
12
13#ifndef _DEFLATE_H
14#define _DEFLATE_H
15
16#include "zutil.h"
17
18/* ===========================================================================
19 * Internal compression state.
20 */
21
22#define LENGTH_CODES 29
23/* number of length codes, not counting the special END_BLOCK code */
24
25#define LITERALS 256
26/* number of literal bytes 0..255 */
27
28#define L_CODES (LITERALS+1+LENGTH_CODES)
29/* number of Literal or Length codes, including the END_BLOCK code */
30
31#define D_CODES 30
32/* number of distance codes */
33
34#define BL_CODES 19
35/* number of codes used to transfer the bit lengths */
36
37#define HEAP_SIZE (2*L_CODES+1)
38/* maximum heap size */
39
40#define MAX_BITS 15
41/* All codes must not exceed MAX_BITS bits */
42
43#define INIT_STATE 42
44#define BUSY_STATE 113
45#define FINISH_STATE 666
46/* Stream status */
47
48
49/* Data structure describing a single value and its code string. */
50typedef struct ct_data_s {
51 union {
52 ush freq; /* frequency count */
53 ush code; /* bit string */
54 } fc;
55 union {
56 ush dad; /* father node in Huffman tree */
57 ush len; /* length of bit string */
58 } dl;
59} FAR ct_data;
60
61#define Freq fc.freq
62#define Code fc.code
63#define Dad dl.dad
64#define Len dl.len
65
66typedef struct static_tree_desc_s static_tree_desc;
67
68typedef struct tree_desc_s {
69 ct_data *dyn_tree; /* the dynamic tree */
70 int max_code; /* largest code with non zero frequency */
71 static_tree_desc *stat_desc; /* the corresponding static tree */
72} FAR tree_desc;
73
74typedef ush Pos;
75typedef Pos FAR Posf;
76typedef unsigned IPos;
77
78/* A Pos is an index in the character window. We use short instead of int to
79 * save space in the various tables. IPos is used only for parameter passing.
80 */
81
82typedef struct internal_state {
83 z_streamp strm; /* pointer back to this zlib stream */
84 int status; /* as the name implies */
85 Bytef *pending_buf; /* output still pending */
86 ulg pending_buf_size; /* size of pending_buf */
87 Bytef *pending_out; /* next pending byte to output to the stream */
88 int pending; /* nb of bytes in the pending buffer */
89 int noheader; /* suppress zlib header and adler32 */
90 Byte data_type; /* UNKNOWN, BINARY or ASCII */
91 Byte method; /* STORED (for zip only) or DEFLATED */
92 int last_flush; /* value of flush param for previous deflate call */
93
94 /* used by deflate.c: */
95
96 uInt w_size; /* LZ77 window size (32K by default) */
97 uInt w_bits; /* log2(w_size) (8..16) */
98 uInt w_mask; /* w_size - 1 */
99
100 Bytef *window;
101 /* Sliding window. Input bytes are read into the second half of the window,
102 * and move to the first half later to keep a dictionary of at least wSize
103 * bytes. With this organization, matches are limited to a distance of
104 * wSize-MAX_MATCH bytes, but this ensures that IO is always
105 * performed with a length multiple of the block size. Also, it limits
106 * the window size to 64K, which is quite useful on MSDOS.
107 * To do: use the user input buffer as sliding window.
108 */
109
110 ulg window_size;
111 /* Actual size of window: 2*wSize, except when the user input buffer
112 * is directly used as sliding window.
113 */
114
115 Posf *prev;
116 /* Link to older string with same hash index. To limit the size of this
117 * array to 64K, this link is maintained only for the last 32K strings.
118 * An index in this array is thus a window index modulo 32K.
119 */
120
121 Posf *head; /* Heads of the hash chains or NIL. */
122
123 uInt ins_h; /* hash index of string to be inserted */
124 uInt hash_size; /* number of elements in hash table */
125 uInt hash_bits; /* log2(hash_size) */
126 uInt hash_mask; /* hash_size-1 */
127
128 uInt hash_shift;
129 /* Number of bits by which ins_h must be shifted at each input
130 * step. It must be such that after MIN_MATCH steps, the oldest
131 * byte no longer takes part in the hash key, that is:
132 * hash_shift * MIN_MATCH >= hash_bits
133 */
134
135 long block_start;
136 /* Window position at the beginning of the current output block. Gets
137 * negative when the window is moved backwards.
138 */
139
140 uInt match_length; /* length of best match */
141 IPos prev_match; /* previous match */
142 int match_available; /* set if previous match exists */
143 uInt strstart; /* start of string to insert */
144 uInt match_start; /* start of matching string */
145 uInt lookahead; /* number of valid bytes ahead in window */
146
147 uInt prev_length;
148 /* Length of the best match at previous step. Matches not greater than this
149 * are discarded. This is used in the lazy match evaluation.
150 */
151
152 uInt max_chain_length;
153 /* To speed up deflation, hash chains are never searched beyond this
154 * length. A higher limit improves compression ratio but degrades the
155 * speed.
156 */
157
158 uInt max_lazy_match;
159 /* Attempt to find a better match only when the current match is strictly
160 * smaller than this value. This mechanism is used only for compression
161 * levels >= 4.
162 */
163# define max_insert_length max_lazy_match
164 /* Insert new strings in the hash table only if the match length is not
165 * greater than this length. This saves time but degrades compression.
166 * max_insert_length is used only for compression levels <= 3.
167 */
168
169 int level; /* compression level (1..9) */
170 int strategy; /* favor or force Huffman coding*/
171
172 uInt good_match;
173 /* Use a faster search when the previous match is longer than this */
174
175 int nice_match; /* Stop searching when current match exceeds this */
176
177 /* used by trees.c: */
178 /* Didn't use ct_data typedef below to supress compiler warning */
179 struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
180 struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
181 struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
182
183 struct tree_desc_s l_desc; /* desc. for literal tree */
184 struct tree_desc_s d_desc; /* desc. for distance tree */
185 struct tree_desc_s bl_desc; /* desc. for bit length tree */
186
187 ush bl_count[MAX_BITS+1];
188 /* number of codes at each bit length for an optimal tree */
189
190 int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
191 int heap_len; /* number of elements in the heap */
192 int heap_max; /* element of largest frequency */
193 /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
194 * The same heap array is used to build all trees.
195 */
196
197 uch depth[2*L_CODES+1];
198 /* Depth of each subtree used as tie breaker for trees of equal frequency
199 */
200
201 uchf *l_buf; /* buffer for literals or lengths */
202
203 uInt lit_bufsize;
204 /* Size of match buffer for literals/lengths. There are 4 reasons for
205 * limiting lit_bufsize to 64K:
206 * - frequencies can be kept in 16 bit counters
207 * - if compression is not successful for the first block, all input
208 * data is still in the window so we can still emit a stored block even
209 * when input comes from standard input. (This can also be done for
210 * all blocks if lit_bufsize is not greater than 32K.)
211 * - if compression is not successful for a file smaller than 64K, we can
212 * even emit a stored file instead of a stored block (saving 5 bytes).
213 * This is applicable only for zip (not gzip or zlib).
214 * - creating new Huffman trees less frequently may not provide fast
215 * adaptation to changes in the input data statistics. (Take for
216 * example a binary file with poorly compressible code followed by
217 * a highly compressible string table.) Smaller buffer sizes give
218 * fast adaptation but have of course the overhead of transmitting
219 * trees more frequently.
220 * - I can't count above 4
221 */
222
223 uInt last_lit; /* running index in l_buf */
224
225 ushf *d_buf;
226 /* Buffer for distances. To simplify the code, d_buf and l_buf have
227 * the same number of elements. To use different lengths, an extra flag
228 * array would be necessary.
229 */
230
231 ulg opt_len; /* bit length of current block with optimal trees */
232 ulg static_len; /* bit length of current block with static trees */
233 uInt matches; /* number of string matches in current block */
234 int last_eob_len; /* bit length of EOB code for last block */
235
236#ifdef DEBUG
237 ulg compressed_len; /* total bit length of compressed file mod 2^32 */
238 ulg bits_sent; /* bit length of compressed data sent mod 2^32 */
239#endif
240
241 ush bi_buf;
242 /* Output buffer. bits are inserted starting at the bottom (least
243 * significant bits).
244 */
245 int bi_valid;
246 /* Number of valid bits in bi_buf. All bits above the last valid bit
247 * are always zero.
248 */
249
250} FAR deflate_state;
251
252/* Output a byte on the stream.
253 * IN assertion: there is enough room in pending_buf.
254 */
255#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
256
257
258#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
259/* Minimum amount of lookahead, except at the end of the input file.
260 * See deflate.c for comments about the MIN_MATCH+1.
261 */
262
263#define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
264/* In order to simplify the code, particularly on 16 bit machines, match
265 * distances are limited to MAX_DIST instead of WSIZE.
266 */
267
268 /* in trees.c */
269void _tr_init OF((deflate_state *s));
270int _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
271void _tr_flush_block OF((deflate_state *s, charf *buf, ulg stored_len,
272 int eof));
273void _tr_align OF((deflate_state *s));
274void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
275 int eof));
276
277#define d_code(dist) \
278 ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
279/* Mapping from a distance to a distance code. dist is the distance - 1 and
280 * must not have side effects. _dist_code[256] and _dist_code[257] are never
281 * used.
282 */
283
284#ifndef DEBUG
285/* Inline versions of _tr_tally for speed: */
286
287#if defined(GEN_TREES_H) || !defined(STDC)
288 extern uch _length_code[];
289 extern uch _dist_code[];
290#else
291 extern const uch _length_code[];
292 extern const uch _dist_code[];
293#endif
294
295# define _tr_tally_lit(s, c, flush) \
296 { uch cc = (c); \
297 s->d_buf[s->last_lit] = 0; \
298 s->l_buf[s->last_lit++] = cc; \
299 s->dyn_ltree[cc].Freq++; \
300 flush = (s->last_lit == s->lit_bufsize-1); \
301 }
302# define _tr_tally_dist(s, distance, length, flush) \
303 { uch len = (length); \
304 ush dist = (distance); \
305 s->d_buf[s->last_lit] = dist; \
306 s->l_buf[s->last_lit++] = len; \
307 dist--; \
308 s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
309 s->dyn_dtree[d_code(dist)].Freq++; \
310 flush = (s->last_lit == s->lit_bufsize-1); \
311 }
312#else
313# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
314# define _tr_tally_dist(s, distance, length, flush) \
315 flush = _tr_tally(s, distance, length)
316#endif
317
318#endif
diff --git a/pwmanager/libcrypt/zlib/infblock.c b/pwmanager/libcrypt/zlib/infblock.c
new file mode 100644
index 0000000..dd7a6d4
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/infblock.c
@@ -0,0 +1,403 @@
1/* infblock.c -- interpret and process block types to last block
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6#include "zutil.h"
7#include "infblock.h"
8#include "inftrees.h"
9#include "infcodes.h"
10#include "infutil.h"
11
12struct inflate_codes_state {int dummy;}; /* for buggy compilers */
13
14/* simplify the use of the inflate_huft type with some defines */
15#define exop word.what.Exop
16#define bits word.what.Bits
17
18/* Table for deflate from PKZIP's appnote.txt. */
19local const uInt border[] = { /* Order of the bit length code lengths */
20 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
21
22/*
23 Notes beyond the 1.93a appnote.txt:
24
25 1. Distance pointers never point before the beginning of the output
26 stream.
27 2. Distance pointers can point back across blocks, up to 32k away.
28 3. There is an implied maximum of 7 bits for the bit length table and
29 15 bits for the actual data.
30 4. If only one code exists, then it is encoded using one bit. (Zero
31 would be more efficient, but perhaps a little confusing.) If two
32 codes exist, they are coded using one bit each (0 and 1).
33 5. There is no way of sending zero distance codes--a dummy must be
34 sent if there are none. (History: a pre 2.0 version of PKZIP would
35 store blocks with no distance codes, but this was discovered to be
36 too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
37 zero distance codes, which is sent as one code of zero bits in
38 length.
39 6. There are up to 286 literal/length codes. Code 256 represents the
40 end-of-block. Note however that the static length tree defines
41 288 codes just to fill out the Huffman codes. Codes 286 and 287
42 cannot be used though, since there is no length base or extra bits
43 defined for them. Similarily, there are up to 30 distance codes.
44 However, static trees define 32 codes (all 5 bits) to fill out the
45 Huffman codes, but the last two had better not show up in the data.
46 7. Unzip can check dynamic Huffman blocks for complete code sets.
47 The exception is that a single code would not be complete (see #4).
48 8. The five bits following the block type is really the number of
49 literal codes sent minus 257.
50 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
51 (1+6+6). Therefore, to output three times the length, you output
52 three codes (1+1+1), whereas to output four times the same length,
53 you only need two codes (1+3). Hmm.
54 10. In the tree reconstruction algorithm, Code = Code + Increment
55 only if BitLength(i) is not zero. (Pretty obvious.)
56 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
57 12. Note: length code 284 can represent 227-258, but length code 285
58 really is 258. The last length deserves its own, short code
59 since it gets used a lot in very redundant files. The length
60 258 is special since 258 - 3 (the min match length) is 255.
61 13. The literal/length and distance code bit lengths are read as a
62 single stream of lengths. It is possible (and advantageous) for
63 a repeat code (16, 17, or 18) to go across the boundary between
64 the two sets of lengths.
65 */
66
67
68void inflate_blocks_reset(s, z, c)
69inflate_blocks_statef *s;
70z_streamp z;
71uLongf *c;
72{
73 if (c != Z_NULL)
74 *c = s->check;
75 if (s->mode == BTREE || s->mode == DTREE)
76 ZFREE(z, s->sub.trees.blens);
77 if (s->mode == CODES)
78 inflate_codes_free(s->sub.decode.codes, z);
79 s->mode = TYPE;
80 s->bitk = 0;
81 s->bitb = 0;
82 s->read = s->write = s->window;
83 if (s->checkfn != Z_NULL)
84 z->adler = s->check = (*s->checkfn)(0L, (const Bytef *)Z_NULL, 0);
85 Tracev((stderr, "inflate: blocks reset\n"));
86}
87
88
89inflate_blocks_statef *inflate_blocks_new(z, c, w)
90z_streamp z;
91check_func c;
92uInt w;
93{
94 inflate_blocks_statef *s;
95
96 if ((s = (inflate_blocks_statef *)ZALLOC
97 (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
98 return s;
99 if ((s->hufts =
100 (inflate_huft *)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)
101 {
102 ZFREE(z, s);
103 return Z_NULL;
104 }
105 if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
106 {
107 ZFREE(z, s->hufts);
108 ZFREE(z, s);
109 return Z_NULL;
110 }
111 s->end = s->window + w;
112 s->checkfn = c;
113 s->mode = TYPE;
114 Tracev((stderr, "inflate: blocks allocated\n"));
115 inflate_blocks_reset(s, z, Z_NULL);
116 return s;
117}
118
119
120int inflate_blocks(s, z, r)
121inflate_blocks_statef *s;
122z_streamp z;
123int r;
124{
125 uInt t; /* temporary storage */
126 uLong b; /* bit buffer */
127 uInt k; /* bits in bit buffer */
128 Bytef *p; /* input data pointer */
129 uInt n; /* bytes available there */
130 Bytef *q; /* output window write pointer */
131 uInt m; /* bytes to end of window or read pointer */
132
133 /* copy input/output information to locals (UPDATE macro restores) */
134 LOAD
135
136 /* process input based on current state */
137 while (1) switch (s->mode)
138 {
139 case TYPE:
140 NEEDBITS(3)
141 t = (uInt)b & 7;
142 s->last = t & 1;
143 switch (t >> 1)
144 {
145 case 0: /* stored */
146 Tracev((stderr, "inflate: stored block%s\n",
147 s->last ? " (last)" : ""));
148 DUMPBITS(3)
149 t = k & 7; /* go to byte boundary */
150 DUMPBITS(t)
151 s->mode = LENS; /* get length of stored block */
152 break;
153 case 1: /* fixed */
154 Tracev((stderr, "inflate: fixed codes block%s\n",
155 s->last ? " (last)" : ""));
156 {
157 uInt bl, bd;
158 inflate_huft *tl, *td;
159
160 inflate_trees_fixed(&bl, &bd, &tl, &td, z);
161 s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
162 if (s->sub.decode.codes == Z_NULL)
163 {
164 r = Z_MEM_ERROR;
165 LEAVE
166 }
167 }
168 DUMPBITS(3)
169 s->mode = CODES;
170 break;
171 case 2: /* dynamic */
172 Tracev((stderr, "inflate: dynamic codes block%s\n",
173 s->last ? " (last)" : ""));
174 DUMPBITS(3)
175 s->mode = TABLE;
176 break;
177 case 3: /* illegal */
178 DUMPBITS(3)
179 s->mode = BAD;
180 z->msg = (char*)"invalid block type";
181 r = Z_DATA_ERROR;
182 LEAVE
183 }
184 break;
185 case LENS:
186 NEEDBITS(32)
187 if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
188 {
189 s->mode = BAD;
190 z->msg = (char*)"invalid stored block lengths";
191 r = Z_DATA_ERROR;
192 LEAVE
193 }
194 s->sub.left = (uInt)b & 0xffff;
195 b = k = 0; /* dump bits */
196 Tracev((stderr, "inflate: stored length %u\n", s->sub.left));
197 s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
198 break;
199 case STORED:
200 if (n == 0)
201 LEAVE
202 NEEDOUT
203 t = s->sub.left;
204 if (t > n) t = n;
205 if (t > m) t = m;
206 zmemcpy(q, p, t);
207 p += t; n -= t;
208 q += t; m -= t;
209 if ((s->sub.left -= t) != 0)
210 break;
211 Tracev((stderr, "inflate: stored end, %lu total out\n",
212 z->total_out + (q >= s->read ? q - s->read :
213 (s->end - s->read) + (q - s->window))));
214 s->mode = s->last ? DRY : TYPE;
215 break;
216 case TABLE:
217 NEEDBITS(14)
218 s->sub.trees.table = t = (uInt)b & 0x3fff;
219#ifndef PKZIP_BUG_WORKAROUND
220 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
221 {
222 s->mode = BAD;
223 z->msg = (char*)"too many length or distance symbols";
224 r = Z_DATA_ERROR;
225 LEAVE
226 }
227#endif
228 t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
229 if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
230 {
231 r = Z_MEM_ERROR;
232 LEAVE
233 }
234 DUMPBITS(14)
235 s->sub.trees.index = 0;
236 Tracev((stderr, "inflate: table sizes ok\n"));
237 s->mode = BTREE;
238 case BTREE:
239 while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
240 {
241 NEEDBITS(3)
242 s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
243 DUMPBITS(3)
244 }
245 while (s->sub.trees.index < 19)
246 s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
247 s->sub.trees.bb = 7;
248 t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
249 &s->sub.trees.tb, s->hufts, z);
250 if (t != Z_OK)
251 {
252 r = t;
253 if (r == Z_DATA_ERROR)
254 {
255 ZFREE(z, s->sub.trees.blens);
256 s->mode = BAD;
257 }
258 LEAVE
259 }
260 s->sub.trees.index = 0;
261 Tracev((stderr, "inflate: bits tree ok\n"));
262 s->mode = DTREE;
263 case DTREE:
264 while (t = s->sub.trees.table,
265 s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
266 {
267 inflate_huft *h;
268 uInt i, j, c;
269
270 t = s->sub.trees.bb;
271 NEEDBITS(t)
272 h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
273 t = h->bits;
274 c = h->base;
275 if (c < 16)
276 {
277 DUMPBITS(t)
278 s->sub.trees.blens[s->sub.trees.index++] = c;
279 }
280 else /* c == 16..18 */
281 {
282 i = c == 18 ? 7 : c - 14;
283 j = c == 18 ? 11 : 3;
284 NEEDBITS(t + i)
285 DUMPBITS(t)
286 j += (uInt)b & inflate_mask[i];
287 DUMPBITS(i)
288 i = s->sub.trees.index;
289 t = s->sub.trees.table;
290 if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
291 (c == 16 && i < 1))
292 {
293 ZFREE(z, s->sub.trees.blens);
294 s->mode = BAD;
295 z->msg = (char*)"invalid bit length repeat";
296 r = Z_DATA_ERROR;
297 LEAVE
298 }
299 c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
300 do {
301 s->sub.trees.blens[i++] = c;
302 } while (--j);
303 s->sub.trees.index = i;
304 }
305 }
306 s->sub.trees.tb = Z_NULL;
307 {
308 uInt bl, bd;
309 inflate_huft *tl, *td;
310 inflate_codes_statef *c;
311
312 bl = 9; /* must be <= 9 for lookahead assumptions */
313 bd = 6; /* must be <= 9 for lookahead assumptions */
314 t = s->sub.trees.table;
315 t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
316 s->sub.trees.blens, &bl, &bd, &tl, &td,
317 s->hufts, z);
318 if (t != Z_OK)
319 {
320 if (t == (uInt)Z_DATA_ERROR)
321 {
322 ZFREE(z, s->sub.trees.blens);
323 s->mode = BAD;
324 }
325 r = t;
326 LEAVE
327 }
328 Tracev((stderr, "inflate: trees ok\n"));
329 if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
330 {
331 r = Z_MEM_ERROR;
332 LEAVE
333 }
334 s->sub.decode.codes = c;
335 }
336 ZFREE(z, s->sub.trees.blens);
337 s->mode = CODES;
338 case CODES:
339 UPDATE
340 if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
341 return inflate_flush(s, z, r);
342 r = Z_OK;
343 inflate_codes_free(s->sub.decode.codes, z);
344 LOAD
345 Tracev((stderr, "inflate: codes end, %lu total out\n",
346 z->total_out + (q >= s->read ? q - s->read :
347 (s->end - s->read) + (q - s->window))));
348 if (!s->last)
349 {
350 s->mode = TYPE;
351 break;
352 }
353 s->mode = DRY;
354 case DRY:
355 FLUSH
356 if (s->read != s->write)
357 LEAVE
358 s->mode = DONE;
359 case DONE:
360 r = Z_STREAM_END;
361 LEAVE
362 case BAD:
363 r = Z_DATA_ERROR;
364 LEAVE
365 default:
366 r = Z_STREAM_ERROR;
367 LEAVE
368 }
369}
370
371
372int inflate_blocks_free(s, z)
373inflate_blocks_statef *s;
374z_streamp z;
375{
376 inflate_blocks_reset(s, z, Z_NULL);
377 ZFREE(z, s->window);
378 ZFREE(z, s->hufts);
379 ZFREE(z, s);
380 Tracev((stderr, "inflate: blocks freed\n"));
381 return Z_OK;
382}
383
384
385void inflate_set_dictionary(s, d, n)
386inflate_blocks_statef *s;
387const Bytef *d;
388uInt n;
389{
390 zmemcpy(s->window, d, n);
391 s->read = s->write = s->window + n;
392}
393
394
395/* Returns true if inflate is currently at the end of a block generated
396 * by Z_SYNC_FLUSH or Z_FULL_FLUSH.
397 * IN assertion: s != Z_NULL
398 */
399int inflate_blocks_sync_point(s)
400inflate_blocks_statef *s;
401{
402 return s->mode == LENS;
403}
diff --git a/pwmanager/libcrypt/zlib/infblock.h b/pwmanager/libcrypt/zlib/infblock.h
new file mode 100644
index 0000000..173b226
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/infblock.h
@@ -0,0 +1,39 @@
1/* infblock.h -- header to use infblock.c
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* WARNING: this file should *not* be used by applications. It is
7 part of the implementation of the compression library and is
8 subject to change. Applications should only use zlib.h.
9 */
10
11struct inflate_blocks_state;
12typedef struct inflate_blocks_state FAR inflate_blocks_statef;
13
14extern inflate_blocks_statef * inflate_blocks_new OF((
15 z_streamp z,
16 check_func c, /* check function */
17 uInt w)); /* window size */
18
19extern int inflate_blocks OF((
20 inflate_blocks_statef *,
21 z_streamp ,
22 int)); /* initial return code */
23
24extern void inflate_blocks_reset OF((
25 inflate_blocks_statef *,
26 z_streamp ,
27 uLongf *)); /* check value on output */
28
29extern int inflate_blocks_free OF((
30 inflate_blocks_statef *,
31 z_streamp));
32
33extern void inflate_set_dictionary OF((
34 inflate_blocks_statef *s,
35 const Bytef *d, /* dictionary */
36 uInt n)); /* dictionary length */
37
38extern int inflate_blocks_sync_point OF((
39 inflate_blocks_statef *s));
diff --git a/pwmanager/libcrypt/zlib/infcodes.c b/pwmanager/libcrypt/zlib/infcodes.c
new file mode 100644
index 0000000..9abe541
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/infcodes.c
@@ -0,0 +1,251 @@
1/* infcodes.c -- process literals and length/distance pairs
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6#include "zutil.h"
7#include "inftrees.h"
8#include "infblock.h"
9#include "infcodes.h"
10#include "infutil.h"
11#include "inffast.h"
12
13/* simplify the use of the inflate_huft type with some defines */
14#define exop word.what.Exop
15#define bits word.what.Bits
16
17typedef enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
18 START, /* x: set up for LEN */
19 LEN, /* i: get length/literal/eob next */
20 LENEXT, /* i: getting length extra (have base) */
21 DIST, /* i: get distance next */
22 DISTEXT, /* i: getting distance extra */
23 COPY, /* o: copying bytes in window, waiting for space */
24 LIT, /* o: got literal, waiting for output space */
25 WASH, /* o: got eob, possibly still output waiting */
26 END, /* x: got eob and all data flushed */
27 BADCODE} /* x: got error */
28inflate_codes_mode;
29
30/* inflate codes private state */
31struct inflate_codes_state {
32
33 /* mode */
34 inflate_codes_mode mode; /* current inflate_codes mode */
35
36 /* mode dependent information */
37 uInt len;
38 union {
39 struct {
40 inflate_huft *tree; /* pointer into tree */
41 uInt need; /* bits needed */
42 } code; /* if LEN or DIST, where in tree */
43 uInt lit; /* if LIT, literal */
44 struct {
45 uInt get; /* bits to get for extra */
46 uInt dist; /* distance back to copy from */
47 } copy; /* if EXT or COPY, where and how much */
48 } sub; /* submode */
49
50 /* mode independent information */
51 Byte lbits; /* ltree bits decoded per branch */
52 Byte dbits; /* dtree bits decoder per branch */
53 inflate_huft *ltree; /* literal/length/eob tree */
54 inflate_huft *dtree; /* distance tree */
55
56};
57
58
59inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
60uInt bl, bd;
61inflate_huft *tl;
62inflate_huft *td; /* need separate declaration for Borland C++ */
63z_streamp z;
64{
65 inflate_codes_statef *c;
66
67 if ((c = (inflate_codes_statef *)
68 ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
69 {
70 c->mode = START;
71 c->lbits = (Byte)bl;
72 c->dbits = (Byte)bd;
73 c->ltree = tl;
74 c->dtree = td;
75 Tracev((stderr, "inflate: codes new\n"));
76 }
77 return c;
78}
79
80
81int inflate_codes(s, z, r)
82inflate_blocks_statef *s;
83z_streamp z;
84int r;
85{
86 uInt j; /* temporary storage */
87 inflate_huft *t; /* temporary pointer */
88 uInt e; /* extra bits or operation */
89 uLong b; /* bit buffer */
90 uInt k; /* bits in bit buffer */
91 Bytef *p; /* input data pointer */
92 uInt n; /* bytes available there */
93 Bytef *q; /* output window write pointer */
94 uInt m; /* bytes to end of window or read pointer */
95 Bytef *f; /* pointer to copy strings from */
96 inflate_codes_statef *c = s->sub.decode.codes; /* codes state */
97
98 /* copy input/output information to locals (UPDATE macro restores) */
99 LOAD
100
101 /* process input and output based on current state */
102 while (1) switch (c->mode)
103 { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
104 case START: /* x: set up for LEN */
105#ifndef SLOW
106 if (m >= 258 && n >= 10)
107 {
108 UPDATE
109 r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
110 LOAD
111 if (r != Z_OK)
112 {
113 c->mode = r == Z_STREAM_END ? WASH : BADCODE;
114 break;
115 }
116 }
117#endif /* !SLOW */
118 c->sub.code.need = c->lbits;
119 c->sub.code.tree = c->ltree;
120 c->mode = LEN;
121 case LEN: /* i: get length/literal/eob next */
122 j = c->sub.code.need;
123 NEEDBITS(j)
124 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
125 DUMPBITS(t->bits)
126 e = (uInt)(t->exop);
127 if (e == 0) /* literal */
128 {
129 c->sub.lit = t->base;
130 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
131 "inflate: literal '%c'\n" :
132 "inflate: literal 0x%02x\n", t->base));
133 c->mode = LIT;
134 break;
135 }
136 if (e & 16) /* length */
137 {
138 c->sub.copy.get = e & 15;
139 c->len = t->base;
140 c->mode = LENEXT;
141 break;
142 }
143 if ((e & 64) == 0) /* next table */
144 {
145 c->sub.code.need = e;
146 c->sub.code.tree = t + t->base;
147 break;
148 }
149 if (e & 32) /* end of block */
150 {
151 Tracevv((stderr, "inflate: end of block\n"));
152 c->mode = WASH;
153 break;
154 }
155 c->mode = BADCODE; /* invalid code */
156 z->msg = (char*)"invalid literal/length code";
157 r = Z_DATA_ERROR;
158 LEAVE
159 case LENEXT: /* i: getting length extra (have base) */
160 j = c->sub.copy.get;
161 NEEDBITS(j)
162 c->len += (uInt)b & inflate_mask[j];
163 DUMPBITS(j)
164 c->sub.code.need = c->dbits;
165 c->sub.code.tree = c->dtree;
166 Tracevv((stderr, "inflate: length %u\n", c->len));
167 c->mode = DIST;
168 case DIST: /* i: get distance next */
169 j = c->sub.code.need;
170 NEEDBITS(j)
171 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
172 DUMPBITS(t->bits)
173 e = (uInt)(t->exop);
174 if (e & 16) /* distance */
175 {
176 c->sub.copy.get = e & 15;
177 c->sub.copy.dist = t->base;
178 c->mode = DISTEXT;
179 break;
180 }
181 if ((e & 64) == 0) /* next table */
182 {
183 c->sub.code.need = e;
184 c->sub.code.tree = t + t->base;
185 break;
186 }
187 c->mode = BADCODE; /* invalid code */
188 z->msg = (char*)"invalid distance code";
189 r = Z_DATA_ERROR;
190 LEAVE
191 case DISTEXT: /* i: getting distance extra */
192 j = c->sub.copy.get;
193 NEEDBITS(j)
194 c->sub.copy.dist += (uInt)b & inflate_mask[j];
195 DUMPBITS(j)
196 Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
197 c->mode = COPY;
198 case COPY: /* o: copying bytes in window, waiting for space */
199 f = q - c->sub.copy.dist;
200 while (f < s->window) /* modulo window size-"while" instead */
201 f += s->end - s->window; /* of "if" handles invalid distances */
202 while (c->len)
203 {
204 NEEDOUT
205 OUTBYTE(*f++)
206 if (f == s->end)
207 f = s->window;
208 c->len--;
209 }
210 c->mode = START;
211 break;
212 case LIT: /* o: got literal, waiting for output space */
213 NEEDOUT
214 OUTBYTE(c->sub.lit)
215 c->mode = START;
216 break;
217 case WASH: /* o: got eob, possibly more output */
218 if (k > 7) /* return unused byte, if any */
219 {
220 Assert(k < 16, "inflate_codes grabbed too many bytes")
221 k -= 8;
222 n++;
223 p--; /* can always return one */
224 }
225 FLUSH
226 if (s->read != s->write)
227 LEAVE
228 c->mode = END;
229 case END:
230 r = Z_STREAM_END;
231 LEAVE
232 case BADCODE: /* x: got error */
233 r = Z_DATA_ERROR;
234 LEAVE
235 default:
236 r = Z_STREAM_ERROR;
237 LEAVE
238 }
239#ifdef NEED_DUMMY_RETURN
240 return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
241#endif
242}
243
244
245void inflate_codes_free(c, z)
246inflate_codes_statef *c;
247z_streamp z;
248{
249 ZFREE(z, c);
250 Tracev((stderr, "inflate: codes free\n"));
251}
diff --git a/pwmanager/libcrypt/zlib/infcodes.h b/pwmanager/libcrypt/zlib/infcodes.h
new file mode 100644
index 0000000..46821a0
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/infcodes.h
@@ -0,0 +1,27 @@
1/* infcodes.h -- header to use infcodes.c
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* WARNING: this file should *not* be used by applications. It is
7 part of the implementation of the compression library and is
8 subject to change. Applications should only use zlib.h.
9 */
10
11struct inflate_codes_state;
12typedef struct inflate_codes_state FAR inflate_codes_statef;
13
14extern inflate_codes_statef *inflate_codes_new OF((
15 uInt, uInt,
16 inflate_huft *, inflate_huft *,
17 z_streamp ));
18
19extern int inflate_codes OF((
20 inflate_blocks_statef *,
21 z_streamp ,
22 int));
23
24extern void inflate_codes_free OF((
25 inflate_codes_statef *,
26 z_streamp ));
27
diff --git a/pwmanager/libcrypt/zlib/inffast.c b/pwmanager/libcrypt/zlib/inffast.c
new file mode 100644
index 0000000..aa7f1d4
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/inffast.c
@@ -0,0 +1,183 @@
1/* inffast.c -- process literals and length/distance pairs fast
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6#include "zutil.h"
7#include "inftrees.h"
8#include "infblock.h"
9#include "infcodes.h"
10#include "infutil.h"
11#include "inffast.h"
12
13struct inflate_codes_state {int dummy;}; /* for buggy compilers */
14
15/* simplify the use of the inflate_huft type with some defines */
16#define exop word.what.Exop
17#define bits word.what.Bits
18
19/* macros for bit input with no checking and for returning unused bytes */
20#define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
21#define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}
22
23/* Called with number of bytes left to write in window at least 258
24 (the maximum string length) and number of input bytes available
25 at least ten. The ten bytes are six bytes for the longest length/
26 distance pair plus four bytes for overloading the bit buffer. */
27
28int inflate_fast(bl, bd, tl, td, s, z)
29uInt bl, bd;
30inflate_huft *tl;
31inflate_huft *td; /* need separate declaration for Borland C++ */
32inflate_blocks_statef *s;
33z_streamp z;
34{
35 inflate_huft *t; /* temporary pointer */
36 uInt e; /* extra bits or operation */
37 uLong b; /* bit buffer */
38 uInt k; /* bits in bit buffer */
39 Bytef *p; /* input data pointer */
40 uInt n; /* bytes available there */
41 Bytef *q; /* output window write pointer */
42 uInt m; /* bytes to end of window or read pointer */
43 uInt ml; /* mask for literal/length tree */
44 uInt md; /* mask for distance tree */
45 uInt c; /* bytes to copy */
46 uInt d; /* distance back to copy from */
47 Bytef *r; /* copy source pointer */
48
49 /* load input, output, bit values */
50 LOAD
51
52 /* initialize masks */
53 ml = inflate_mask[bl];
54 md = inflate_mask[bd];
55
56 /* do until not enough input or output space for fast loop */
57 do { /* assume called with m >= 258 && n >= 10 */
58 /* get literal/length code */
59 GRABBITS(20) /* max bits for literal/length code */
60 if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
61 {
62 DUMPBITS(t->bits)
63 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
64 "inflate: * literal '%c'\n" :
65 "inflate: * literal 0x%02x\n", t->base));
66 *q++ = (Byte)t->base;
67 m--;
68 continue;
69 }
70 do {
71 DUMPBITS(t->bits)
72 if (e & 16)
73 {
74 /* get extra bits for length */
75 e &= 15;
76 c = t->base + ((uInt)b & inflate_mask[e]);
77 DUMPBITS(e)
78 Tracevv((stderr, "inflate: * length %u\n", c));
79
80 /* decode distance base of block to copy */
81 GRABBITS(15); /* max bits for distance code */
82 e = (t = td + ((uInt)b & md))->exop;
83 do {
84 DUMPBITS(t->bits)
85 if (e & 16)
86 {
87 /* get extra bits to add to distance base */
88 e &= 15;
89 GRABBITS(e) /* get extra bits (up to 13) */
90 d = t->base + ((uInt)b & inflate_mask[e]);
91 DUMPBITS(e)
92 Tracevv((stderr, "inflate: * distance %u\n", d));
93
94 /* do the copy */
95 m -= c;
96 r = q - d;
97 if (r < s->window) /* wrap if needed */
98 {
99 do {
100 r += s->end - s->window; /* force pointer in window */
101 } while (r < s->window); /* covers invalid distances */
102 e = s->end - r;
103 if (c > e)
104 {
105 c -= e; /* wrapped copy */
106 do {
107 *q++ = *r++;
108 } while (--e);
109 r = s->window;
110 do {
111 *q++ = *r++;
112 } while (--c);
113 }
114 else /* normal copy */
115 {
116 *q++ = *r++; c--;
117 *q++ = *r++; c--;
118 do {
119 *q++ = *r++;
120 } while (--c);
121 }
122 }
123 else /* normal copy */
124 {
125 *q++ = *r++; c--;
126 *q++ = *r++; c--;
127 do {
128 *q++ = *r++;
129 } while (--c);
130 }
131 break;
132 }
133 else if ((e & 64) == 0)
134 {
135 t += t->base;
136 e = (t += ((uInt)b & inflate_mask[e]))->exop;
137 }
138 else
139 {
140 z->msg = (char*)"invalid distance code";
141 UNGRAB
142 UPDATE
143 return Z_DATA_ERROR;
144 }
145 } while (1);
146 break;
147 }
148 if ((e & 64) == 0)
149 {
150 t += t->base;
151 if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0)
152 {
153 DUMPBITS(t->bits)
154 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
155 "inflate: * literal '%c'\n" :
156 "inflate: * literal 0x%02x\n", t->base));
157 *q++ = (Byte)t->base;
158 m--;
159 break;
160 }
161 }
162 else if (e & 32)
163 {
164 Tracevv((stderr, "inflate: * end of block\n"));
165 UNGRAB
166 UPDATE
167 return Z_STREAM_END;
168 }
169 else
170 {
171 z->msg = (char*)"invalid literal/length code";
172 UNGRAB
173 UPDATE
174 return Z_DATA_ERROR;
175 }
176 } while (1);
177 } while (m >= 258 && n >= 10);
178
179 /* not enough input or output--restore pointers and return */
180 UNGRAB
181 UPDATE
182 return Z_OK;
183}
diff --git a/pwmanager/libcrypt/zlib/inffast.h b/pwmanager/libcrypt/zlib/inffast.h
new file mode 100644
index 0000000..a31a4bb
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/inffast.h
@@ -0,0 +1,17 @@
1/* inffast.h -- header to use inffast.c
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* WARNING: this file should *not* be used by applications. It is
7 part of the implementation of the compression library and is
8 subject to change. Applications should only use zlib.h.
9 */
10
11extern int inflate_fast OF((
12 uInt,
13 uInt,
14 inflate_huft *,
15 inflate_huft *,
16 inflate_blocks_statef *,
17 z_streamp ));
diff --git a/pwmanager/libcrypt/zlib/inffixed.h b/pwmanager/libcrypt/zlib/inffixed.h
new file mode 100644
index 0000000..77f7e76
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/inffixed.h
@@ -0,0 +1,151 @@
1/* inffixed.h -- table for decoding fixed codes
2 * Generated automatically by the maketree.c program
3 */
4
5/* WARNING: this file should *not* be used by applications. It is
6 part of the implementation of the compression library and is
7 subject to change. Applications should only use zlib.h.
8 */
9
10local uInt fixed_bl = 9;
11local uInt fixed_bd = 5;
12local inflate_huft fixed_tl[] = {
13 {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
14 {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},192},
15 {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},160},
16 {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},224},
17 {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},144},
18 {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},208},
19 {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},176},
20 {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},240},
21 {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
22 {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},200},
23 {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},168},
24 {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},232},
25 {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},152},
26 {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},216},
27 {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},184},
28 {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},248},
29 {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
30 {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},196},
31 {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},164},
32 {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},228},
33 {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},148},
34 {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},212},
35 {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},180},
36 {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},244},
37 {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
38 {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},204},
39 {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},172},
40 {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},236},
41 {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},156},
42 {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},220},
43 {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},188},
44 {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},252},
45 {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
46 {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},194},
47 {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},162},
48 {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},226},
49 {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},146},
50 {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},210},
51 {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},178},
52 {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},242},
53 {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
54 {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},202},
55 {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},170},
56 {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},234},
57 {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},154},
58 {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},218},
59 {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},186},
60 {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},250},
61 {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
62 {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},198},
63 {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},166},
64 {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},230},
65 {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},150},
66 {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},214},
67 {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},182},
68 {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},246},
69 {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
70 {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},206},
71 {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},174},
72 {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},238},
73 {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},158},
74 {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},222},
75 {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},190},
76 {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},254},
77 {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
78 {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},193},
79 {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},161},
80 {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},225},
81 {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},145},
82 {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},209},
83 {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},177},
84 {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},241},
85 {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
86 {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},201},
87 {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},169},
88 {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},233},
89 {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},153},
90 {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},217},
91 {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},185},
92 {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},249},
93 {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
94 {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},197},
95 {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},165},
96 {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},229},
97 {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},149},
98 {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},213},
99 {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},181},
100 {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},245},
101 {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
102 {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},205},
103 {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},173},
104 {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},237},
105 {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},157},
106 {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},221},
107 {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},189},
108 {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},253},
109 {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
110 {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},195},
111 {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},163},
112 {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},227},
113 {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},147},
114 {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},211},
115 {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},179},
116 {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},243},
117 {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
118 {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},203},
119 {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},171},
120 {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},235},
121 {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},155},
122 {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},219},
123 {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},187},
124 {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},251},
125 {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
126 {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},199},
127 {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},167},
128 {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},231},
129 {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},151},
130 {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},215},
131 {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},183},
132 {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},247},
133 {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
134 {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},207},
135 {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},175},
136 {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},239},
137 {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},159},
138 {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},223},
139 {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},191},
140 {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},255}
141 };
142local inflate_huft fixed_td[] = {
143 {{{80,5}},1}, {{{87,5}},257}, {{{83,5}},17}, {{{91,5}},4097},
144 {{{81,5}},5}, {{{89,5}},1025}, {{{85,5}},65}, {{{93,5}},16385},
145 {{{80,5}},3}, {{{88,5}},513}, {{{84,5}},33}, {{{92,5}},8193},
146 {{{82,5}},9}, {{{90,5}},2049}, {{{86,5}},129}, {{{192,5}},24577},
147 {{{80,5}},2}, {{{87,5}},385}, {{{83,5}},25}, {{{91,5}},6145},
148 {{{81,5}},7}, {{{89,5}},1537}, {{{85,5}},97}, {{{93,5}},24577},
149 {{{80,5}},4}, {{{88,5}},769}, {{{84,5}},49}, {{{92,5}},12289},
150 {{{82,5}},13}, {{{90,5}},3073}, {{{86,5}},193}, {{{192,5}},24577}
151 };
diff --git a/pwmanager/libcrypt/zlib/inflate.c b/pwmanager/libcrypt/zlib/inflate.c
new file mode 100644
index 0000000..dfb2e86
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/inflate.c
@@ -0,0 +1,366 @@
1/* inflate.c -- zlib interface to inflate modules
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6#include "zutil.h"
7#include "infblock.h"
8
9struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
10
11typedef enum {
12 METHOD, /* waiting for method byte */
13 FLAG, /* waiting for flag byte */
14 DICT4, /* four dictionary check bytes to go */
15 DICT3, /* three dictionary check bytes to go */
16 DICT2, /* two dictionary check bytes to go */
17 DICT1, /* one dictionary check byte to go */
18 DICT0, /* waiting for inflateSetDictionary */
19 BLOCKS, /* decompressing blocks */
20 CHECK4, /* four check bytes to go */
21 CHECK3, /* three check bytes to go */
22 CHECK2, /* two check bytes to go */
23 CHECK1, /* one check byte to go */
24 DONE, /* finished check, done */
25 BAD} /* got an error--stay here */
26inflate_mode;
27
28/* inflate private state */
29struct internal_state {
30
31 /* mode */
32 inflate_mode mode; /* current inflate mode */
33
34 /* mode dependent information */
35 union {
36 uInt method; /* if FLAGS, method byte */
37 struct {
38 uLong was; /* computed check value */
39 uLong need; /* stream check value */
40 } check; /* if CHECK, check values to compare */
41 uInt marker; /* if BAD, inflateSync's marker bytes count */
42 } sub; /* submode */
43
44 /* mode independent information */
45 int nowrap; /* flag for no wrapper */
46 uInt wbits; /* log2(window size) (8..15, defaults to 15) */
47 inflate_blocks_statef
48 *blocks; /* current inflate_blocks state */
49
50};
51
52
53int ZEXPORT inflateReset(z)
54z_streamp z;
55{
56 if (z == Z_NULL || z->state == Z_NULL)
57 return Z_STREAM_ERROR;
58 z->total_in = z->total_out = 0;
59 z->msg = Z_NULL;
60 z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
61 inflate_blocks_reset(z->state->blocks, z, Z_NULL);
62 Tracev((stderr, "inflate: reset\n"));
63 return Z_OK;
64}
65
66
67int ZEXPORT inflateEnd(z)
68z_streamp z;
69{
70 if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
71 return Z_STREAM_ERROR;
72 if (z->state->blocks != Z_NULL)
73 inflate_blocks_free(z->state->blocks, z);
74 ZFREE(z, z->state);
75 z->state = Z_NULL;
76 Tracev((stderr, "inflate: end\n"));
77 return Z_OK;
78}
79
80
81int ZEXPORT inflateInit2_(z, w, version, stream_size)
82z_streamp z;
83int w;
84const char *version;
85int stream_size;
86{
87 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
88 stream_size != sizeof(z_stream))
89 return Z_VERSION_ERROR;
90
91 /* initialize state */
92 if (z == Z_NULL)
93 return Z_STREAM_ERROR;
94 z->msg = Z_NULL;
95 if (z->zalloc == Z_NULL)
96 {
97 z->zalloc = zcalloc;
98 z->opaque = (voidpf)0;
99 }
100 if (z->zfree == Z_NULL) z->zfree = zcfree;
101 if ((z->state = (struct internal_state FAR *)
102 ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
103 return Z_MEM_ERROR;
104 z->state->blocks = Z_NULL;
105
106 /* handle undocumented nowrap option (no zlib header or check) */
107 z->state->nowrap = 0;
108 if (w < 0)
109 {
110 w = - w;
111 z->state->nowrap = 1;
112 }
113
114 /* set window size */
115 if (w < 8 || w > 15)
116 {
117 inflateEnd(z);
118 return Z_STREAM_ERROR;
119 }
120 z->state->wbits = (uInt)w;
121
122 /* create inflate_blocks state */
123 if ((z->state->blocks =
124 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
125 == Z_NULL)
126 {
127 inflateEnd(z);
128 return Z_MEM_ERROR;
129 }
130 Tracev((stderr, "inflate: allocated\n"));
131
132 /* reset state */
133 inflateReset(z);
134 return Z_OK;
135}
136
137
138int ZEXPORT inflateInit_(z, version, stream_size)
139z_streamp z;
140const char *version;
141int stream_size;
142{
143 return inflateInit2_(z, DEF_WBITS, version, stream_size);
144}
145
146
147#define NEEDBYTE {if(z->avail_in==0)return r;r=f;}
148#define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
149
150int ZEXPORT inflate(z, f)
151z_streamp z;
152int f;
153{
154 int r;
155 uInt b;
156
157 if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
158 return Z_STREAM_ERROR;
159 f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
160 r = Z_BUF_ERROR;
161 while (1) switch (z->state->mode)
162 {
163 case METHOD:
164 NEEDBYTE
165 if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
166 {
167 z->state->mode = BAD;
168 z->msg = (char*)"unknown compression method";
169 z->state->sub.marker = 5; /* can't try inflateSync */
170 break;
171 }
172 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
173 {
174 z->state->mode = BAD;
175 z->msg = (char*)"invalid window size";
176 z->state->sub.marker = 5; /* can't try inflateSync */
177 break;
178 }
179 z->state->mode = FLAG;
180 case FLAG:
181 NEEDBYTE
182 b = NEXTBYTE;
183 if (((z->state->sub.method << 8) + b) % 31)
184 {
185 z->state->mode = BAD;
186 z->msg = (char*)"incorrect header check";
187 z->state->sub.marker = 5; /* can't try inflateSync */
188 break;
189 }
190 Tracev((stderr, "inflate: zlib header ok\n"));
191 if (!(b & PRESET_DICT))
192 {
193 z->state->mode = BLOCKS;
194 break;
195 }
196 z->state->mode = DICT4;
197 case DICT4:
198 NEEDBYTE
199 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
200 z->state->mode = DICT3;
201 case DICT3:
202 NEEDBYTE
203 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
204 z->state->mode = DICT2;
205 case DICT2:
206 NEEDBYTE
207 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
208 z->state->mode = DICT1;
209 case DICT1:
210 NEEDBYTE
211 z->state->sub.check.need += (uLong)NEXTBYTE;
212 z->adler = z->state->sub.check.need;
213 z->state->mode = DICT0;
214 return Z_NEED_DICT;
215 case DICT0:
216 z->state->mode = BAD;
217 z->msg = (char*)"need dictionary";
218 z->state->sub.marker = 0; /* can try inflateSync */
219 return Z_STREAM_ERROR;
220 case BLOCKS:
221 r = inflate_blocks(z->state->blocks, z, r);
222 if (r == Z_DATA_ERROR)
223 {
224 z->state->mode = BAD;
225 z->state->sub.marker = 0; /* can try inflateSync */
226 break;
227 }
228 if (r == Z_OK)
229 r = f;
230 if (r != Z_STREAM_END)
231 return r;
232 r = f;
233 inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
234 if (z->state->nowrap)
235 {
236 z->state->mode = DONE;
237 break;
238 }
239 z->state->mode = CHECK4;
240 case CHECK4:
241 NEEDBYTE
242 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
243 z->state->mode = CHECK3;
244 case CHECK3:
245 NEEDBYTE
246 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
247 z->state->mode = CHECK2;
248 case CHECK2:
249 NEEDBYTE
250 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
251 z->state->mode = CHECK1;
252 case CHECK1:
253 NEEDBYTE
254 z->state->sub.check.need += (uLong)NEXTBYTE;
255
256 if (z->state->sub.check.was != z->state->sub.check.need)
257 {
258 z->state->mode = BAD;
259 z->msg = (char*)"incorrect data check";
260 z->state->sub.marker = 5; /* can't try inflateSync */
261 break;
262 }
263 Tracev((stderr, "inflate: zlib check ok\n"));
264 z->state->mode = DONE;
265 case DONE:
266 return Z_STREAM_END;
267 case BAD:
268 return Z_DATA_ERROR;
269 default:
270 return Z_STREAM_ERROR;
271 }
272#ifdef NEED_DUMMY_RETURN
273 return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
274#endif
275}
276
277
278int ZEXPORT inflateSetDictionary(z, dictionary, dictLength)
279z_streamp z;
280const Bytef *dictionary;
281uInt dictLength;
282{
283 uInt length = dictLength;
284
285 if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
286 return Z_STREAM_ERROR;
287
288 if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
289 z->adler = 1L;
290
291 if (length >= ((uInt)1<<z->state->wbits))
292 {
293 length = (1<<z->state->wbits)-1;
294 dictionary += dictLength - length;
295 }
296 inflate_set_dictionary(z->state->blocks, dictionary, length);
297 z->state->mode = BLOCKS;
298 return Z_OK;
299}
300
301
302int ZEXPORT inflateSync(z)
303z_streamp z;
304{
305 uInt n; /* number of bytes to look at */
306 Bytef *p; /* pointer to bytes */
307 uInt m; /* number of marker bytes found in a row */
308 uLong r, w; /* temporaries to save total_in and total_out */
309
310 /* set up */
311 if (z == Z_NULL || z->state == Z_NULL)
312 return Z_STREAM_ERROR;
313 if (z->state->mode != BAD)
314 {
315 z->state->mode = BAD;
316 z->state->sub.marker = 0;
317 }
318 if ((n = z->avail_in) == 0)
319 return Z_BUF_ERROR;
320 p = z->next_in;
321 m = z->state->sub.marker;
322
323 /* search */
324 while (n && m < 4)
325 {
326 static const Byte mark[4] = {0, 0, 0xff, 0xff};
327 if (*p == mark[m])
328 m++;
329 else if (*p)
330 m = 0;
331 else
332 m = 4 - m;
333 p++, n--;
334 }
335
336 /* restore */
337 z->total_in += p - z->next_in;
338 z->next_in = p;
339 z->avail_in = n;
340 z->state->sub.marker = m;
341
342 /* return no joy or set up to restart on a new block */
343 if (m != 4)
344 return Z_DATA_ERROR;
345 r = z->total_in; w = z->total_out;
346 inflateReset(z);
347 z->total_in = r; z->total_out = w;
348 z->state->mode = BLOCKS;
349 return Z_OK;
350}
351
352
353/* Returns true if inflate is currently at the end of a block generated
354 * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
355 * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
356 * but removes the length bytes of the resulting empty stored block. When
357 * decompressing, PPP checks that at the end of input packet, inflate is
358 * waiting for these length bytes.
359 */
360int ZEXPORT inflateSyncPoint(z)
361z_streamp z;
362{
363 if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
364 return Z_STREAM_ERROR;
365 return inflate_blocks_sync_point(z->state->blocks);
366}
diff --git a/pwmanager/libcrypt/zlib/inftrees.c b/pwmanager/libcrypt/zlib/inftrees.c
new file mode 100644
index 0000000..4c32ca3
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/inftrees.c
@@ -0,0 +1,454 @@
1/* inftrees.c -- generate Huffman trees for efficient decoding
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6#include "zutil.h"
7#include "inftrees.h"
8
9#if !defined(BUILDFIXED) && !defined(STDC)
10# define BUILDFIXED /* non ANSI compilers may not accept inffixed.h */
11#endif
12
13const char inflate_copyright[] =
14 " inflate 1.1.4 Copyright 1995-2002 Mark Adler ";
15/*
16 If you use the zlib library in a product, an acknowledgment is welcome
17 in the documentation of your product. If for some reason you cannot
18 include such an acknowledgment, I would appreciate that you keep this
19 copyright string in the executable of your product.
20 */
21struct internal_state {int dummy;}; /* for buggy compilers */
22
23/* simplify the use of the inflate_huft type with some defines */
24#define exop word.what.Exop
25#define bits word.what.Bits
26
27
28local int huft_build OF((
29 uIntf *, /* code lengths in bits */
30 uInt, /* number of codes */
31 uInt, /* number of "simple" codes */
32 const uIntf *, /* list of base values for non-simple codes */
33 const uIntf *, /* list of extra bits for non-simple codes */
34 inflate_huft * FAR*,/* result: starting table */
35 uIntf *, /* maximum lookup bits (returns actual) */
36 inflate_huft *, /* space for trees */
37 uInt *, /* hufts used in space */
38 uIntf * )); /* space for values */
39
40/* Tables for deflate from PKZIP's appnote.txt. */
41local const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
42 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
43 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
44 /* see note #13 above about 258 */
45local const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
46 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
47 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
48local const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
49 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
50 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
51 8193, 12289, 16385, 24577};
52local const uInt cpdext[30] = { /* Extra bits for distance codes */
53 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
54 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
55 12, 12, 13, 13};
56
57/*
58 Huffman code decoding is performed using a multi-level table lookup.
59 The fastest way to decode is to simply build a lookup table whose
60 size is determined by the longest code. However, the time it takes
61 to build this table can also be a factor if the data being decoded
62 is not very long. The most common codes are necessarily the
63 shortest codes, so those codes dominate the decoding time, and hence
64 the speed. The idea is you can have a shorter table that decodes the
65 shorter, more probable codes, and then point to subsidiary tables for
66 the longer codes. The time it costs to decode the longer codes is
67 then traded against the time it takes to make longer tables.
68
69 This results of this trade are in the variables lbits and dbits
70 below. lbits is the number of bits the first level table for literal/
71 length codes can decode in one step, and dbits is the same thing for
72 the distance codes. Subsequent tables are also less than or equal to
73 those sizes. These values may be adjusted either when all of the
74 codes are shorter than that, in which case the longest code length in
75 bits is used, or when the shortest code is *longer* than the requested
76 table size, in which case the length of the shortest code in bits is
77 used.
78
79 There are two different values for the two tables, since they code a
80 different number of possibilities each. The literal/length table
81 codes 286 possible values, or in a flat code, a little over eight
82 bits. The distance table codes 30 possible values, or a little less
83 than five bits, flat. The optimum values for speed end up being
84 about one bit more than those, so lbits is 8+1 and dbits is 5+1.
85 The optimum values may differ though from machine to machine, and
86 possibly even between compilers. Your mileage may vary.
87 */
88
89
90/* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
91#define BMAX 15 /* maximum bit length of any code */
92
93local int huft_build(b, n, s, d, e, t, m, hp, hn, v)
94uIntf *b; /* code lengths in bits (all assumed <= BMAX) */
95uInt n; /* number of codes (assumed <= 288) */
96uInt s; /* number of simple-valued codes (0..s-1) */
97const uIntf *d; /* list of base values for non-simple codes */
98const uIntf *e; /* list of extra bits for non-simple codes */
99inflate_huft * FAR *t; /* result: starting table */
100uIntf *m; /* maximum lookup bits, returns actual */
101inflate_huft *hp; /* space for trees */
102uInt *hn; /* hufts used in space */
103uIntf *v; /* working area: values in order of bit length */
104/* Given a list of code lengths and a maximum table size, make a set of
105 tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
106 if the given code set is incomplete (the tables are still built in this
107 case), or Z_DATA_ERROR if the input is invalid. */
108{
109
110 uInt a; /* counter for codes of length k */
111 uInt c[BMAX+1]; /* bit length count table */
112 uInt f; /* i repeats in table every f entries */
113 int g; /* maximum code length */
114 int h; /* table level */
115 register uInt i; /* counter, current code */
116 register uInt j; /* counter */
117 register int k; /* number of bits in current code */
118 int l; /* bits per table (returned in m) */
119 uInt mask; /* (1 << w) - 1, to avoid cc -O bug on HP */
120 register uIntf *p; /* pointer into c[], b[], or v[] */
121 inflate_huft *q; /* points to current table */
122 struct inflate_huft_s r; /* table entry for structure assignment */
123 inflate_huft *u[BMAX]; /* table stack */
124 register int w; /* bits before this table == (l * h) */
125 uInt x[BMAX+1]; /* bit offsets, then code stack */
126 uIntf *xp; /* pointer into x */
127 int y; /* number of dummy codes added */
128 uInt z; /* number of entries in current table */
129
130
131 /* Generate counts for each bit length */
132 p = c;
133#define C0 *p++ = 0;
134#define C2 C0 C0 C0 C0
135#define C4 C2 C2 C2 C2
136 C4 /* clear c[]--assume BMAX+1 is 16 */
137 p = b; i = n;
138 do {
139 c[*p++]++; /* assume all entries <= BMAX */
140 } while (--i);
141 if (c[0] == n) /* null input--all zero length codes */
142 {
143 *t = (inflate_huft *)Z_NULL;
144 *m = 0;
145 return Z_OK;
146 }
147
148
149 /* Find minimum and maximum length, bound *m by those */
150 l = *m;
151 for (j = 1; j <= BMAX; j++)
152 if (c[j])
153 break;
154 k = j; /* minimum code length */
155 if ((uInt)l < j)
156 l = j;
157 for (i = BMAX; i; i--)
158 if (c[i])
159 break;
160 g = i; /* maximum code length */
161 if ((uInt)l > i)
162 l = i;
163 *m = l;
164
165
166 /* Adjust last length count to fill out codes, if needed */
167 for (y = 1 << j; j < i; j++, y <<= 1)
168 if ((y -= c[j]) < 0)
169 return Z_DATA_ERROR;
170 if ((y -= c[i]) < 0)
171 return Z_DATA_ERROR;
172 c[i] += y;
173
174
175 /* Generate starting offsets into the value table for each length */
176 x[1] = j = 0;
177 p = c + 1; xp = x + 2;
178 while (--i) { /* note that i == g from above */
179 *xp++ = (j += *p++);
180 }
181
182
183 /* Make a table of values in order of bit lengths */
184 p = b; i = 0;
185 do {
186 if ((j = *p++) != 0)
187 v[x[j]++] = i;
188 } while (++i < n);
189 n = x[g]; /* set n to length of v */
190
191
192 /* Generate the Huffman codes and for each, make the table entries */
193 x[0] = i = 0; /* first Huffman code is zero */
194 p = v; /* grab values in bit order */
195 h = -1; /* no tables yet--level -1 */
196 w = -l; /* bits decoded == (l * h) */
197 u[0] = (inflate_huft *)Z_NULL; /* just to keep compilers happy */
198 q = (inflate_huft *)Z_NULL; /* ditto */
199 z = 0; /* ditto */
200
201 /* go through the bit lengths (k already is bits in shortest code) */
202 for (; k <= g; k++)
203 {
204 a = c[k];
205 while (a--)
206 {
207 /* here i is the Huffman code of length k bits for value *p */
208 /* make tables up to required level */
209 while (k > w + l)
210 {
211 h++;
212 w += l; /* previous table always l bits */
213
214 /* compute minimum size table less than or equal to l bits */
215 z = g - w;
216 z = z > (uInt)l ? l : z; /* table size upper limit */
217 if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
218 { /* too few codes for k-w bit table */
219 f -= a + 1; /* deduct codes from patterns left */
220 xp = c + k;
221 if (j < z)
222 while (++j < z) /* try smaller tables up to z bits */
223 {
224 if ((f <<= 1) <= *++xp)
225 break; /* enough codes to use up j bits */
226 f -= *xp; /* else deduct codes from patterns */
227 }
228 }
229 z = 1 << j; /* table entries for j-bit table */
230
231 /* allocate new table */
232 if (*hn + z > MANY) /* (note: doesn't matter for fixed) */
233 return Z_DATA_ERROR; /* overflow of MANY */
234 u[h] = q = hp + *hn;
235 *hn += z;
236
237 /* connect to last table, if there is one */
238 if (h)
239 {
240 x[h] = i; /* save pattern for backing up */
241 r.bits = (Byte)l; /* bits to dump before this table */
242 r.exop = (Byte)j; /* bits in this table */
243 j = i >> (w - l);
244 r.base = (uInt)(q - u[h-1] - j); /* offset to this table */
245 u[h-1][j] = r; /* connect to last table */
246 }
247 else
248 *t = q; /* first table is returned result */
249 }
250
251 /* set up table entry in r */
252 r.bits = (Byte)(k - w);
253 if (p >= v + n)
254 r.exop = 128 + 64; /* out of values--invalid code */
255 else if (*p < s)
256 {
257 r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); /* 256 is end-of-block */
258 r.base = *p++; /* simple code is just the value */
259 }
260 else
261 {
262 r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
263 r.base = d[*p++ - s];
264 }
265
266 /* fill code-like entries with r */
267 f = 1 << (k - w);
268 for (j = i >> w; j < z; j += f)
269 q[j] = r;
270
271 /* backwards increment the k-bit code i */
272 for (j = 1 << (k - 1); i & j; j >>= 1)
273 i ^= j;
274 i ^= j;
275
276 /* backup over finished tables */
277 mask = (1 << w) - 1; /* needed on HP, cc -O bug */
278 while ((i & mask) != x[h])
279 {
280 h--; /* don't need to update q */
281 w -= l;
282 mask = (1 << w) - 1;
283 }
284 }
285 }
286
287
288 /* Return Z_BUF_ERROR if we were given an incomplete table */
289 return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
290}
291
292
293int inflate_trees_bits(c, bb, tb, hp, z)
294uIntf *c; /* 19 code lengths */
295uIntf *bb; /* bits tree desired/actual depth */
296inflate_huft * FAR *tb; /* bits tree result */
297inflate_huft *hp; /* space for trees */
298z_streamp z; /* for messages */
299{
300 int r;
301 uInt hn = 0; /* hufts used in space */
302 uIntf *v; /* work area for huft_build */
303
304 if ((v = (uIntf*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
305 return Z_MEM_ERROR;
306 r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL,
307 tb, bb, hp, &hn, v);
308 if (r == Z_DATA_ERROR)
309 z->msg = (char*)"oversubscribed dynamic bit lengths tree";
310 else if (r == Z_BUF_ERROR || *bb == 0)
311 {
312 z->msg = (char*)"incomplete dynamic bit lengths tree";
313 r = Z_DATA_ERROR;
314 }
315 ZFREE(z, v);
316 return r;
317}
318
319
320int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, hp, z)
321uInt nl; /* number of literal/length codes */
322uInt nd; /* number of distance codes */
323uIntf *c; /* that many (total) code lengths */
324uIntf *bl; /* literal desired/actual bit depth */
325uIntf *bd; /* distance desired/actual bit depth */
326inflate_huft * FAR *tl; /* literal/length tree result */
327inflate_huft * FAR *td; /* distance tree result */
328inflate_huft *hp; /* space for trees */
329z_streamp z; /* for messages */
330{
331 int r;
332 uInt hn = 0; /* hufts used in space */
333 uIntf *v; /* work area for huft_build */
334
335 /* allocate work area */
336 if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
337 return Z_MEM_ERROR;
338
339 /* build literal/length tree */
340 r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
341 if (r != Z_OK || *bl == 0)
342 {
343 if (r == Z_DATA_ERROR)
344 z->msg = (char*)"oversubscribed literal/length tree";
345 else if (r != Z_MEM_ERROR)
346 {
347 z->msg = (char*)"incomplete literal/length tree";
348 r = Z_DATA_ERROR;
349 }
350 ZFREE(z, v);
351 return r;
352 }
353
354 /* build distance tree */
355 r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
356 if (r != Z_OK || (*bd == 0 && nl > 257))
357 {
358 if (r == Z_DATA_ERROR)
359 z->msg = (char*)"oversubscribed distance tree";
360 else if (r == Z_BUF_ERROR) {
361#ifdef PKZIP_BUG_WORKAROUND
362 r = Z_OK;
363 }
364#else
365 z->msg = (char*)"incomplete distance tree";
366 r = Z_DATA_ERROR;
367 }
368 else if (r != Z_MEM_ERROR)
369 {
370 z->msg = (char*)"empty distance tree with lengths";
371 r = Z_DATA_ERROR;
372 }
373 ZFREE(z, v);
374 return r;
375#endif
376 }
377
378 /* done */
379 ZFREE(z, v);
380 return Z_OK;
381}
382
383
384/* build fixed tables only once--keep them here */
385#ifdef BUILDFIXED
386local int fixed_built = 0;
387#define FIXEDH 544 /* number of hufts used by fixed tables */
388local inflate_huft fixed_mem[FIXEDH];
389local uInt fixed_bl;
390local uInt fixed_bd;
391local inflate_huft *fixed_tl;
392local inflate_huft *fixed_td;
393#else
394#include "inffixed.h"
395#endif
396
397
398int inflate_trees_fixed(bl, bd, tl, td, z)
399uIntf *bl; /* literal desired/actual bit depth */
400uIntf *bd; /* distance desired/actual bit depth */
401inflate_huft * FAR *tl; /* literal/length tree result */
402inflate_huft * FAR *td; /* distance tree result */
403z_streamp z; /* for memory allocation */
404{
405#ifdef BUILDFIXED
406 /* build fixed tables if not already */
407 if (!fixed_built)
408 {
409 int k; /* temporary variable */
410 uInt f = 0; /* number of hufts used in fixed_mem */
411 uIntf *c; /* length list for huft_build */
412 uIntf *v; /* work area for huft_build */
413
414 /* allocate memory */
415 if ((c = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
416 return Z_MEM_ERROR;
417 if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
418 {
419 ZFREE(z, c);
420 return Z_MEM_ERROR;
421 }
422
423 /* literal table */
424 for (k = 0; k < 144; k++)
425 c[k] = 8;
426 for (; k < 256; k++)
427 c[k] = 9;
428 for (; k < 280; k++)
429 c[k] = 7;
430 for (; k < 288; k++)
431 c[k] = 8;
432 fixed_bl = 9;
433 huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl,
434 fixed_mem, &f, v);
435
436 /* distance table */
437 for (k = 0; k < 30; k++)
438 c[k] = 5;
439 fixed_bd = 5;
440 huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd,
441 fixed_mem, &f, v);
442
443 /* done */
444 ZFREE(z, v);
445 ZFREE(z, c);
446 fixed_built = 1;
447 }
448#endif
449 *bl = fixed_bl;
450 *bd = fixed_bd;
451 *tl = fixed_tl;
452 *td = fixed_td;
453 return Z_OK;
454}
diff --git a/pwmanager/libcrypt/zlib/inftrees.h b/pwmanager/libcrypt/zlib/inftrees.h
new file mode 100644
index 0000000..04b73b7
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/inftrees.h
@@ -0,0 +1,58 @@
1/* inftrees.h -- header to use inftrees.c
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* WARNING: this file should *not* be used by applications. It is
7 part of the implementation of the compression library and is
8 subject to change. Applications should only use zlib.h.
9 */
10
11/* Huffman code lookup table entry--this entry is four bytes for machines
12 that have 16-bit pointers (e.g. PC's in the small or medium model). */
13
14typedef struct inflate_huft_s FAR inflate_huft;
15
16struct inflate_huft_s {
17 union {
18 struct {
19 Byte Exop; /* number of extra bits or operation */
20 Byte Bits; /* number of bits in this code or subcode */
21 } what;
22 uInt pad; /* pad structure to a power of 2 (4 bytes for */
23 } word; /* 16-bit, 8 bytes for 32-bit int's) */
24 uInt base; /* literal, length base, distance base,
25 or table offset */
26};
27
28/* Maximum size of dynamic tree. The maximum found in a long but non-
29 exhaustive search was 1004 huft structures (850 for length/literals
30 and 154 for distances, the latter actually the result of an
31 exhaustive search). The actual maximum is not known, but the
32 value below is more than safe. */
33#define MANY 1440
34
35extern int inflate_trees_bits OF((
36 uIntf *, /* 19 code lengths */
37 uIntf *, /* bits tree desired/actual depth */
38 inflate_huft * FAR *, /* bits tree result */
39 inflate_huft *, /* space for trees */
40 z_streamp)); /* for messages */
41
42extern int inflate_trees_dynamic OF((
43 uInt, /* number of literal/length codes */
44 uInt, /* number of distance codes */
45 uIntf *, /* that many (total) code lengths */
46 uIntf *, /* literal desired/actual bit depth */
47 uIntf *, /* distance desired/actual bit depth */
48 inflate_huft * FAR *, /* literal/length tree result */
49 inflate_huft * FAR *, /* distance tree result */
50 inflate_huft *, /* space for trees */
51 z_streamp)); /* for messages */
52
53extern int inflate_trees_fixed OF((
54 uIntf *, /* literal desired/actual bit depth */
55 uIntf *, /* distance desired/actual bit depth */
56 inflate_huft * FAR *, /* literal/length tree result */
57 inflate_huft * FAR *, /* distance tree result */
58 z_streamp)); /* for memory allocation */
diff --git a/pwmanager/libcrypt/zlib/infutil.c b/pwmanager/libcrypt/zlib/infutil.c
new file mode 100644
index 0000000..9a07622
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/infutil.c
@@ -0,0 +1,87 @@
1/* inflate_util.c -- data and routines common to blocks and codes
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6#include "zutil.h"
7#include "infblock.h"
8#include "inftrees.h"
9#include "infcodes.h"
10#include "infutil.h"
11
12struct inflate_codes_state {int dummy;}; /* for buggy compilers */
13
14/* And'ing with mask[n] masks the lower n bits */
15uInt inflate_mask[17] = {
16 0x0000,
17 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
18 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
19};
20
21
22/* copy as much as possible from the sliding window to the output area */
23int inflate_flush(s, z, r)
24inflate_blocks_statef *s;
25z_streamp z;
26int r;
27{
28 uInt n;
29 Bytef *p;
30 Bytef *q;
31
32 /* local copies of source and destination pointers */
33 p = z->next_out;
34 q = s->read;
35
36 /* compute number of bytes to copy as far as end of window */
37 n = (uInt)((q <= s->write ? s->write : s->end) - q);
38 if (n > z->avail_out) n = z->avail_out;
39 if (n && r == Z_BUF_ERROR) r = Z_OK;
40
41 /* update counters */
42 z->avail_out -= n;
43 z->total_out += n;
44
45 /* update check information */
46 if (s->checkfn != Z_NULL)
47 z->adler = s->check = (*s->checkfn)(s->check, q, n);
48
49 /* copy as far as end of window */
50 zmemcpy(p, q, n);
51 p += n;
52 q += n;
53
54 /* see if more to copy at beginning of window */
55 if (q == s->end)
56 {
57 /* wrap pointers */
58 q = s->window;
59 if (s->write == s->end)
60 s->write = s->window;
61
62 /* compute bytes to copy */
63 n = (uInt)(s->write - q);
64 if (n > z->avail_out) n = z->avail_out;
65 if (n && r == Z_BUF_ERROR) r = Z_OK;
66
67 /* update counters */
68 z->avail_out -= n;
69 z->total_out += n;
70
71 /* update check information */
72 if (s->checkfn != Z_NULL)
73 z->adler = s->check = (*s->checkfn)(s->check, q, n);
74
75 /* copy */
76 zmemcpy(p, q, n);
77 p += n;
78 q += n;
79 }
80
81 /* update pointers */
82 z->next_out = p;
83 s->read = q;
84
85 /* done */
86 return r;
87}
diff --git a/pwmanager/libcrypt/zlib/infutil.h b/pwmanager/libcrypt/zlib/infutil.h
new file mode 100644
index 0000000..4401df8
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/infutil.h
@@ -0,0 +1,98 @@
1/* infutil.h -- types and macros common to blocks and codes
2 * Copyright (C) 1995-2002 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* WARNING: this file should *not* be used by applications. It is
7 part of the implementation of the compression library and is
8 subject to change. Applications should only use zlib.h.
9 */
10
11#ifndef _INFUTIL_H
12#define _INFUTIL_H
13
14typedef enum {
15 TYPE, /* get type bits (3, including end bit) */
16 LENS, /* get lengths for stored */
17 STORED, /* processing stored block */
18 TABLE, /* get table lengths */
19 BTREE, /* get bit lengths tree for a dynamic block */
20 DTREE, /* get length, distance trees for a dynamic block */
21 CODES, /* processing fixed or dynamic block */
22 DRY, /* output remaining window bytes */
23 DONE, /* finished last block, done */
24 BAD} /* got a data error--stuck here */
25inflate_block_mode;
26
27/* inflate blocks semi-private state */
28struct inflate_blocks_state {
29
30 /* mode */
31 inflate_block_mode mode; /* current inflate_block mode */
32
33 /* mode dependent information */
34 union {
35 uInt left; /* if STORED, bytes left to copy */
36 struct {
37 uInt table; /* table lengths (14 bits) */
38 uInt index; /* index into blens (or border) */
39 uIntf *blens; /* bit lengths of codes */
40 uInt bb; /* bit length tree depth */
41 inflate_huft *tb; /* bit length decoding tree */
42 } trees; /* if DTREE, decoding info for trees */
43 struct {
44 inflate_codes_statef
45 *codes;
46 } decode; /* if CODES, current state */
47 } sub; /* submode */
48 uInt last; /* true if this block is the last block */
49
50 /* mode independent information */
51 uInt bitk; /* bits in bit buffer */
52 uLong bitb; /* bit buffer */
53 inflate_huft *hufts; /* single malloc for tree space */
54 Bytef *window; /* sliding window */
55 Bytef *end; /* one byte after sliding window */
56 Bytef *read; /* window read pointer */
57 Bytef *write; /* window write pointer */
58 check_func checkfn; /* check function */
59 uLong check; /* check on output */
60
61};
62
63
64/* defines for inflate input/output */
65/* update pointers and return */
66#define UPDBITS {s->bitb=b;s->bitk=k;}
67#define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;}
68#define UPDOUT {s->write=q;}
69#define UPDATE {UPDBITS UPDIN UPDOUT}
70#define LEAVE {UPDATE return inflate_flush(s,z,r);}
71/* get bytes and bits */
72#define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
73#define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
74#define NEXTBYTE (n--,*p++)
75#define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
76#define DUMPBITS(j) {b>>=(j);k-=(j);}
77/* output bytes */
78#define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
79#define LOADOUT {q=s->write;m=(uInt)WAVAIL;}
80#define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
81#define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
82#define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
83#define OUTBYTE(a) {*q++=(Byte)(a);m--;}
84/* load local pointers */
85#define LOAD {LOADIN LOADOUT}
86
87/* masks for lower bits (size given to avoid silly warnings with Visual C++) */
88extern uInt inflate_mask[17];
89
90/* copy as much as possible from the sliding window to the output area */
91extern int inflate_flush OF((
92 inflate_blocks_statef *,
93 z_streamp ,
94 int));
95
96struct internal_state {int dummy;}; /* for buggy compilers */
97
98#endif
diff --git a/pwmanager/libcrypt/zlib/trees.c b/pwmanager/libcrypt/zlib/trees.c
new file mode 100644
index 0000000..0a98405
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/trees.c
@@ -0,0 +1,1214 @@
1/* trees.c -- output deflated data using Huffman coding
2 * Copyright (C) 1995-2002 Jean-loup Gailly
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/*
7 * ALGORITHM
8 *
9 * The "deflation" process uses several Huffman trees. The more
10 * common source values are represented by shorter bit sequences.
11 *
12 * Each code tree is stored in a compressed form which is itself
13 * a Huffman encoding of the lengths of all the code strings (in
14 * ascending order by source values). The actual code strings are
15 * reconstructed from the lengths in the inflate process, as described
16 * in the deflate specification.
17 *
18 * REFERENCES
19 *
20 * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
21 * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
22 *
23 * Storer, James A.
24 * Data Compression: Methods and Theory, pp. 49-50.
25 * Computer Science Press, 1988. ISBN 0-7167-8156-5.
26 *
27 * Sedgewick, R.
28 * Algorithms, p290.
29 * Addison-Wesley, 1983. ISBN 0-201-06672-6.
30 */
31
32/* @(#) $Id$ */
33
34/* #define GEN_TREES_H */
35
36#include "deflate.h"
37
38#ifdef DEBUG
39# include <ctype.h>
40#endif
41
42/* ===========================================================================
43 * Constants
44 */
45
46#define MAX_BL_BITS 7
47/* Bit length codes must not exceed MAX_BL_BITS bits */
48
49#define END_BLOCK 256
50/* end of block literal code */
51
52#define REP_3_6 16
53/* repeat previous bit length 3-6 times (2 bits of repeat count) */
54
55#define REPZ_3_10 17
56/* repeat a zero length 3-10 times (3 bits of repeat count) */
57
58#define REPZ_11_138 18
59/* repeat a zero length 11-138 times (7 bits of repeat count) */
60
61local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
62 = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
63
64local const int extra_dbits[D_CODES] /* extra bits for each distance code */
65 = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
66
67local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */
68 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
69
70local const uch bl_order[BL_CODES]
71 = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
72/* The lengths of the bit length codes are sent in order of decreasing
73 * probability, to avoid transmitting the lengths for unused bit length codes.
74 */
75
76#define Buf_size (8 * 2*sizeof(char))
77/* Number of bits used within bi_buf. (bi_buf might be implemented on
78 * more than 16 bits on some systems.)
79 */
80
81/* ===========================================================================
82 * Local data. These are initialized only once.
83 */
84
85#define DIST_CODE_LEN 512 /* see definition of array dist_code below */
86
87#if defined(GEN_TREES_H) || !defined(STDC)
88/* non ANSI compilers may not accept trees.h */
89
90local ct_data static_ltree[L_CODES+2];
91/* The static literal tree. Since the bit lengths are imposed, there is no
92 * need for the L_CODES extra codes used during heap construction. However
93 * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
94 * below).
95 */
96
97local ct_data static_dtree[D_CODES];
98/* The static distance tree. (Actually a trivial tree since all codes use
99 * 5 bits.)
100 */
101
102uch _dist_code[DIST_CODE_LEN];
103/* Distance codes. The first 256 values correspond to the distances
104 * 3 .. 258, the last 256 values correspond to the top 8 bits of
105 * the 15 bit distances.
106 */
107
108uch _length_code[MAX_MATCH-MIN_MATCH+1];
109/* length code for each normalized match length (0 == MIN_MATCH) */
110
111local int base_length[LENGTH_CODES];
112/* First normalized length for each code (0 = MIN_MATCH) */
113
114local int base_dist[D_CODES];
115/* First normalized distance for each code (0 = distance of 1) */
116
117#else
118# include "trees.h"
119#endif /* GEN_TREES_H */
120
121struct static_tree_desc_s {
122 const ct_data *static_tree; /* static tree or NULL */
123 const intf *extra_bits; /* extra bits for each code or NULL */
124 int extra_base; /* base index for extra_bits */
125 int elems; /* max number of elements in the tree */
126 int max_length; /* max bit length for the codes */
127};
128
129local static_tree_desc static_l_desc =
130{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
131
132local static_tree_desc static_d_desc =
133{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
134
135local static_tree_desc static_bl_desc =
136{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
137
138/* ===========================================================================
139 * Local (static) routines in this file.
140 */
141
142local void tr_static_init OF((void));
143local void init_block OF((deflate_state *s));
144local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
145local void gen_bitlen OF((deflate_state *s, tree_desc *desc));
146local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count));
147local void build_tree OF((deflate_state *s, tree_desc *desc));
148local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code));
149local void send_tree OF((deflate_state *s, ct_data *tree, int max_code));
150local int build_bl_tree OF((deflate_state *s));
151local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
152 int blcodes));
153local void compress_block OF((deflate_state *s, ct_data *ltree,
154 ct_data *dtree));
155local void set_data_type OF((deflate_state *s));
156local unsigned bi_reverse OF((unsigned value, int length));
157local void bi_windup OF((deflate_state *s));
158local void bi_flush OF((deflate_state *s));
159local void copy_block OF((deflate_state *s, charf *buf, unsigned len,
160 int header));
161
162#ifdef GEN_TREES_H
163local void gen_trees_header OF((void));
164#endif
165
166#ifndef DEBUG
167# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
168 /* Send a code of the given tree. c and tree must not have side effects */
169
170#else /* DEBUG */
171# define send_code(s, c, tree) \
172 { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
173 send_bits(s, tree[c].Code, tree[c].Len); }
174#endif
175
176/* ===========================================================================
177 * Output a short LSB first on the stream.
178 * IN assertion: there is enough room in pendingBuf.
179 */
180#define put_short(s, w) { \
181 put_byte(s, (uch)((w) & 0xff)); \
182 put_byte(s, (uch)((ush)(w) >> 8)); \
183}
184
185/* ===========================================================================
186 * Send a value on a given number of bits.
187 * IN assertion: length <= 16 and value fits in length bits.
188 */
189#ifdef DEBUG
190local void send_bits OF((deflate_state *s, int value, int length));
191
192local void send_bits(s, value, length)
193 deflate_state *s;
194 int value; /* value to send */
195 int length; /* number of bits */
196{
197 Tracevv((stderr," l %2d v %4x ", length, value));
198 Assert(length > 0 && length <= 15, "invalid length");
199 s->bits_sent += (ulg)length;
200
201 /* If not enough room in bi_buf, use (valid) bits from bi_buf and
202 * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
203 * unused bits in value.
204 */
205 if (s->bi_valid > (int)Buf_size - length) {
206 s->bi_buf |= (value << s->bi_valid);
207 put_short(s, s->bi_buf);
208 s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
209 s->bi_valid += length - Buf_size;
210 } else {
211 s->bi_buf |= value << s->bi_valid;
212 s->bi_valid += length;
213 }
214}
215#else /* !DEBUG */
216
217#define send_bits(s, value, length) \
218{ int len = length;\
219 if (s->bi_valid > (int)Buf_size - len) {\
220 int val = value;\
221 s->bi_buf |= (val << s->bi_valid);\
222 put_short(s, s->bi_buf);\
223 s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
224 s->bi_valid += len - Buf_size;\
225 } else {\
226 s->bi_buf |= (value) << s->bi_valid;\
227 s->bi_valid += len;\
228 }\
229}
230#endif /* DEBUG */
231
232
233#define MAX(a,b) (a >= b ? a : b)
234/* the arguments must not have side effects */
235
236/* ===========================================================================
237 * Initialize the various 'constant' tables.
238 */
239local void tr_static_init()
240{
241#if defined(GEN_TREES_H) || !defined(STDC)
242 static int static_init_done = 0;
243 int n; /* iterates over tree elements */
244 int bits; /* bit counter */
245 int length; /* length value */
246 int code; /* code value */
247 int dist; /* distance index */
248 ush bl_count[MAX_BITS+1];
249 /* number of codes at each bit length for an optimal tree */
250
251 if (static_init_done) return;
252
253 /* For some embedded targets, global variables are not initialized: */
254 static_l_desc.static_tree = static_ltree;
255 static_l_desc.extra_bits = extra_lbits;
256 static_d_desc.static_tree = static_dtree;
257 static_d_desc.extra_bits = extra_dbits;
258 static_bl_desc.extra_bits = extra_blbits;
259
260 /* Initialize the mapping length (0..255) -> length code (0..28) */
261 length = 0;
262 for (code = 0; code < LENGTH_CODES-1; code++) {
263 base_length[code] = length;
264 for (n = 0; n < (1<<extra_lbits[code]); n++) {
265 _length_code[length++] = (uch)code;
266 }
267 }
268 Assert (length == 256, "tr_static_init: length != 256");
269 /* Note that the length 255 (match length 258) can be represented
270 * in two different ways: code 284 + 5 bits or code 285, so we
271 * overwrite length_code[255] to use the best encoding:
272 */
273 _length_code[length-1] = (uch)code;
274
275 /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
276 dist = 0;
277 for (code = 0 ; code < 16; code++) {
278 base_dist[code] = dist;
279 for (n = 0; n < (1<<extra_dbits[code]); n++) {
280 _dist_code[dist++] = (uch)code;
281 }
282 }
283 Assert (dist == 256, "tr_static_init: dist != 256");
284 dist >>= 7; /* from now on, all distances are divided by 128 */
285 for ( ; code < D_CODES; code++) {
286 base_dist[code] = dist << 7;
287 for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
288 _dist_code[256 + dist++] = (uch)code;
289 }
290 }
291 Assert (dist == 256, "tr_static_init: 256+dist != 512");
292
293 /* Construct the codes of the static literal tree */
294 for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
295 n = 0;
296 while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
297 while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
298 while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
299 while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
300 /* Codes 286 and 287 do not exist, but we must include them in the
301 * tree construction to get a canonical Huffman tree (longest code
302 * all ones)
303 */
304 gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
305
306 /* The static distance tree is trivial: */
307 for (n = 0; n < D_CODES; n++) {
308 static_dtree[n].Len = 5;
309 static_dtree[n].Code = bi_reverse((unsigned)n, 5);
310 }
311 static_init_done = 1;
312
313# ifdef GEN_TREES_H
314 gen_trees_header();
315# endif
316#endif /* defined(GEN_TREES_H) || !defined(STDC) */
317}
318
319/* ===========================================================================
320 * Genererate the file trees.h describing the static trees.
321 */
322#ifdef GEN_TREES_H
323# ifndef DEBUG
324# include <stdio.h>
325# endif
326
327# define SEPARATOR(i, last, width) \
328 ((i) == (last)? "\n};\n\n" : \
329 ((i) % (width) == (width)-1 ? ",\n" : ", "))
330
331void gen_trees_header()
332{
333 FILE *header = fopen("trees.h", "w");
334 int i;
335
336 Assert (header != NULL, "Can't open trees.h");
337 fprintf(header,
338 "/* header created automatically with -DGEN_TREES_H */\n\n");
339
340 fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
341 for (i = 0; i < L_CODES+2; i++) {
342 fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
343 static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
344 }
345
346 fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
347 for (i = 0; i < D_CODES; i++) {
348 fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
349 static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
350 }
351
352 fprintf(header, "const uch _dist_code[DIST_CODE_LEN] = {\n");
353 for (i = 0; i < DIST_CODE_LEN; i++) {
354 fprintf(header, "%2u%s", _dist_code[i],
355 SEPARATOR(i, DIST_CODE_LEN-1, 20));
356 }
357
358 fprintf(header, "const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
359 for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
360 fprintf(header, "%2u%s", _length_code[i],
361 SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
362 }
363
364 fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
365 for (i = 0; i < LENGTH_CODES; i++) {
366 fprintf(header, "%1u%s", base_length[i],
367 SEPARATOR(i, LENGTH_CODES-1, 20));
368 }
369
370 fprintf(header, "local const int base_dist[D_CODES] = {\n");
371 for (i = 0; i < D_CODES; i++) {
372 fprintf(header, "%5u%s", base_dist[i],
373 SEPARATOR(i, D_CODES-1, 10));
374 }
375
376 fclose(header);
377}
378#endif /* GEN_TREES_H */
379
380/* ===========================================================================
381 * Initialize the tree data structures for a new zlib stream.
382 */
383void _tr_init(s)
384 deflate_state *s;
385{
386 tr_static_init();
387
388 s->l_desc.dyn_tree = s->dyn_ltree;
389 s->l_desc.stat_desc = &static_l_desc;
390
391 s->d_desc.dyn_tree = s->dyn_dtree;
392 s->d_desc.stat_desc = &static_d_desc;
393
394 s->bl_desc.dyn_tree = s->bl_tree;
395 s->bl_desc.stat_desc = &static_bl_desc;
396
397 s->bi_buf = 0;
398 s->bi_valid = 0;
399 s->last_eob_len = 8; /* enough lookahead for inflate */
400#ifdef DEBUG
401 s->compressed_len = 0L;
402 s->bits_sent = 0L;
403#endif
404
405 /* Initialize the first block of the first file: */
406 init_block(s);
407}
408
409/* ===========================================================================
410 * Initialize a new block.
411 */
412local void init_block(s)
413 deflate_state *s;
414{
415 int n; /* iterates over tree elements */
416
417 /* Initialize the trees. */
418 for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
419 for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
420 for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
421
422 s->dyn_ltree[END_BLOCK].Freq = 1;
423 s->opt_len = s->static_len = 0L;
424 s->last_lit = s->matches = 0;
425}
426
427#define SMALLEST 1
428/* Index within the heap array of least frequent node in the Huffman tree */
429
430
431/* ===========================================================================
432 * Remove the smallest element from the heap and recreate the heap with
433 * one less element. Updates heap and heap_len.
434 */
435#define pqremove(s, tree, top) \
436{\
437 top = s->heap[SMALLEST]; \
438 s->heap[SMALLEST] = s->heap[s->heap_len--]; \
439 pqdownheap(s, tree, SMALLEST); \
440}
441
442/* ===========================================================================
443 * Compares to subtrees, using the tree depth as tie breaker when
444 * the subtrees have equal frequency. This minimizes the worst case length.
445 */
446#define smaller(tree, n, m, depth) \
447 (tree[n].Freq < tree[m].Freq || \
448 (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
449
450/* ===========================================================================
451 * Restore the heap property by moving down the tree starting at node k,
452 * exchanging a node with the smallest of its two sons if necessary, stopping
453 * when the heap property is re-established (each father smaller than its
454 * two sons).
455 */
456local void pqdownheap(s, tree, k)
457 deflate_state *s;
458 ct_data *tree; /* the tree to restore */
459 int k; /* node to move down */
460{
461 int v = s->heap[k];
462 int j = k << 1; /* left son of k */
463 while (j <= s->heap_len) {
464 /* Set j to the smallest of the two sons: */
465 if (j < s->heap_len &&
466 smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
467 j++;
468 }
469 /* Exit if v is smaller than both sons */
470 if (smaller(tree, v, s->heap[j], s->depth)) break;
471
472 /* Exchange v with the smallest son */
473 s->heap[k] = s->heap[j]; k = j;
474
475 /* And continue down the tree, setting j to the left son of k */
476 j <<= 1;
477 }
478 s->heap[k] = v;
479}
480
481/* ===========================================================================
482 * Compute the optimal bit lengths for a tree and update the total bit length
483 * for the current block.
484 * IN assertion: the fields freq and dad are set, heap[heap_max] and
485 * above are the tree nodes sorted by increasing frequency.
486 * OUT assertions: the field len is set to the optimal bit length, the
487 * array bl_count contains the frequencies for each bit length.
488 * The length opt_len is updated; static_len is also updated if stree is
489 * not null.
490 */
491local void gen_bitlen(s, desc)
492 deflate_state *s;
493 tree_desc *desc; /* the tree descriptor */
494{
495 ct_data *tree = desc->dyn_tree;
496 int max_code = desc->max_code;
497 const ct_data *stree = desc->stat_desc->static_tree;
498 const intf *extra = desc->stat_desc->extra_bits;
499 int base = desc->stat_desc->extra_base;
500 int max_length = desc->stat_desc->max_length;
501 int h; /* heap index */
502 int n, m; /* iterate over the tree elements */
503 int bits; /* bit length */
504 int xbits; /* extra bits */
505 ush f; /* frequency */
506 int overflow = 0; /* number of elements with bit length too large */
507
508 for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
509
510 /* In a first pass, compute the optimal bit lengths (which may
511 * overflow in the case of the bit length tree).
512 */
513 tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
514
515 for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
516 n = s->heap[h];
517 bits = tree[tree[n].Dad].Len + 1;
518 if (bits > max_length) bits = max_length, overflow++;
519 tree[n].Len = (ush)bits;
520 /* We overwrite tree[n].Dad which is no longer needed */
521
522 if (n > max_code) continue; /* not a leaf node */
523
524 s->bl_count[bits]++;
525 xbits = 0;
526 if (n >= base) xbits = extra[n-base];
527 f = tree[n].Freq;
528 s->opt_len += (ulg)f * (bits + xbits);
529 if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
530 }
531 if (overflow == 0) return;
532
533 Trace((stderr,"\nbit length overflow\n"));
534 /* This happens for example on obj2 and pic of the Calgary corpus */
535
536 /* Find the first bit length which could increase: */
537 do {
538 bits = max_length-1;
539 while (s->bl_count[bits] == 0) bits--;
540 s->bl_count[bits]--; /* move one leaf down the tree */
541 s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
542 s->bl_count[max_length]--;
543 /* The brother of the overflow item also moves one step up,
544 * but this does not affect bl_count[max_length]
545 */
546 overflow -= 2;
547 } while (overflow > 0);
548
549 /* Now recompute all bit lengths, scanning in increasing frequency.
550 * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
551 * lengths instead of fixing only the wrong ones. This idea is taken
552 * from 'ar' written by Haruhiko Okumura.)
553 */
554 for (bits = max_length; bits != 0; bits--) {
555 n = s->bl_count[bits];
556 while (n != 0) {
557 m = s->heap[--h];
558 if (m > max_code) continue;
559 if (tree[m].Len != (unsigned) bits) {
560 Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
561 s->opt_len += ((long)bits - (long)tree[m].Len)
562 *(long)tree[m].Freq;
563 tree[m].Len = (ush)bits;
564 }
565 n--;
566 }
567 }
568}
569
570/* ===========================================================================
571 * Generate the codes for a given tree and bit counts (which need not be
572 * optimal).
573 * IN assertion: the array bl_count contains the bit length statistics for
574 * the given tree and the field len is set for all tree elements.
575 * OUT assertion: the field code is set for all tree elements of non
576 * zero code length.
577 */
578local void gen_codes (tree, max_code, bl_count)
579 ct_data *tree; /* the tree to decorate */
580 int max_code; /* largest code with non zero frequency */
581 ushf *bl_count; /* number of codes at each bit length */
582{
583 ush next_code[MAX_BITS+1]; /* next code value for each bit length */
584 ush code = 0; /* running code value */
585 int bits; /* bit index */
586 int n; /* code index */
587
588 /* The distribution counts are first used to generate the code values
589 * without bit reversal.
590 */
591 for (bits = 1; bits <= MAX_BITS; bits++) {
592 next_code[bits] = code = (code + bl_count[bits-1]) << 1;
593 }
594 /* Check that the bit counts in bl_count are consistent. The last code
595 * must be all ones.
596 */
597 Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
598 "inconsistent bit counts");
599 Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
600
601 for (n = 0; n <= max_code; n++) {
602 int len = tree[n].Len;
603 if (len == 0) continue;
604 /* Now reverse the bits */
605 tree[n].Code = bi_reverse(next_code[len]++, len);
606
607 Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
608 n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
609 }
610}
611
612/* ===========================================================================
613 * Construct one Huffman tree and assigns the code bit strings and lengths.
614 * Update the total bit length for the current block.
615 * IN assertion: the field freq is set for all tree elements.
616 * OUT assertions: the fields len and code are set to the optimal bit length
617 * and corresponding code. The length opt_len is updated; static_len is
618 * also updated if stree is not null. The field max_code is set.
619 */
620local void build_tree(s, desc)
621 deflate_state *s;
622 tree_desc *desc; /* the tree descriptor */
623{
624 ct_data *tree = desc->dyn_tree;
625 const ct_data *stree = desc->stat_desc->static_tree;
626 int elems = desc->stat_desc->elems;
627 int n, m; /* iterate over heap elements */
628 int max_code = -1; /* largest code with non zero frequency */
629 int node; /* new node being created */
630
631 /* Construct the initial heap, with least frequent element in
632 * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
633 * heap[0] is not used.
634 */
635 s->heap_len = 0, s->heap_max = HEAP_SIZE;
636
637 for (n = 0; n < elems; n++) {
638 if (tree[n].Freq != 0) {
639 s->heap[++(s->heap_len)] = max_code = n;
640 s->depth[n] = 0;
641 } else {
642 tree[n].Len = 0;
643 }
644 }
645
646 /* The pkzip format requires that at least one distance code exists,
647 * and that at least one bit should be sent even if there is only one
648 * possible code. So to avoid special checks later on we force at least
649 * two codes of non zero frequency.
650 */
651 while (s->heap_len < 2) {
652 node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
653 tree[node].Freq = 1;
654 s->depth[node] = 0;
655 s->opt_len--; if (stree) s->static_len -= stree[node].Len;
656 /* node is 0 or 1 so it does not have extra bits */
657 }
658 desc->max_code = max_code;
659
660 /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
661 * establish sub-heaps of increasing lengths:
662 */
663 for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
664
665 /* Construct the Huffman tree by repeatedly combining the least two
666 * frequent nodes.
667 */
668 node = elems; /* next internal node of the tree */
669 do {
670 pqremove(s, tree, n); /* n = node of least frequency */
671 m = s->heap[SMALLEST]; /* m = node of next least frequency */
672
673 s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
674 s->heap[--(s->heap_max)] = m;
675
676 /* Create a new node father of n and m */
677 tree[node].Freq = tree[n].Freq + tree[m].Freq;
678 s->depth[node] = (uch) (MAX(s->depth[n], s->depth[m]) + 1);
679 tree[n].Dad = tree[m].Dad = (ush)node;
680#ifdef DUMP_BL_TREE
681 if (tree == s->bl_tree) {
682 fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
683 node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
684 }
685#endif
686 /* and insert the new node in the heap */
687 s->heap[SMALLEST] = node++;
688 pqdownheap(s, tree, SMALLEST);
689
690 } while (s->heap_len >= 2);
691
692 s->heap[--(s->heap_max)] = s->heap[SMALLEST];
693
694 /* At this point, the fields freq and dad are set. We can now
695 * generate the bit lengths.
696 */
697 gen_bitlen(s, (tree_desc *)desc);
698
699 /* The field len is now set, we can generate the bit codes */
700 gen_codes ((ct_data *)tree, max_code, s->bl_count);
701}
702
703/* ===========================================================================
704 * Scan a literal or distance tree to determine the frequencies of the codes
705 * in the bit length tree.
706 */
707local void scan_tree (s, tree, max_code)
708 deflate_state *s;
709 ct_data *tree; /* the tree to be scanned */
710 int max_code; /* and its largest code of non zero frequency */
711{
712 int n; /* iterates over all tree elements */
713 int prevlen = -1; /* last emitted length */
714 int curlen; /* length of current code */
715 int nextlen = tree[0].Len; /* length of next code */
716 int count = 0; /* repeat count of the current code */
717 int max_count = 7; /* max repeat count */
718 int min_count = 4; /* min repeat count */
719
720 if (nextlen == 0) max_count = 138, min_count = 3;
721 tree[max_code+1].Len = (ush)0xffff; /* guard */
722
723 for (n = 0; n <= max_code; n++) {
724 curlen = nextlen; nextlen = tree[n+1].Len;
725 if (++count < max_count && curlen == nextlen) {
726 continue;
727 } else if (count < min_count) {
728 s->bl_tree[curlen].Freq += count;
729 } else if (curlen != 0) {
730 if (curlen != prevlen) s->bl_tree[curlen].Freq++;
731 s->bl_tree[REP_3_6].Freq++;
732 } else if (count <= 10) {
733 s->bl_tree[REPZ_3_10].Freq++;
734 } else {
735 s->bl_tree[REPZ_11_138].Freq++;
736 }
737 count = 0; prevlen = curlen;
738 if (nextlen == 0) {
739 max_count = 138, min_count = 3;
740 } else if (curlen == nextlen) {
741 max_count = 6, min_count = 3;
742 } else {
743 max_count = 7, min_count = 4;
744 }
745 }
746}
747
748/* ===========================================================================
749 * Send a literal or distance tree in compressed form, using the codes in
750 * bl_tree.
751 */
752local void send_tree (s, tree, max_code)
753 deflate_state *s;
754 ct_data *tree; /* the tree to be scanned */
755 int max_code; /* and its largest code of non zero frequency */
756{
757 int n; /* iterates over all tree elements */
758 int prevlen = -1; /* last emitted length */
759 int curlen; /* length of current code */
760 int nextlen = tree[0].Len; /* length of next code */
761 int count = 0; /* repeat count of the current code */
762 int max_count = 7; /* max repeat count */
763 int min_count = 4; /* min repeat count */
764
765 /* tree[max_code+1].Len = -1; */ /* guard already set */
766 if (nextlen == 0) max_count = 138, min_count = 3;
767
768 for (n = 0; n <= max_code; n++) {
769 curlen = nextlen; nextlen = tree[n+1].Len;
770 if (++count < max_count && curlen == nextlen) {
771 continue;
772 } else if (count < min_count) {
773 do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
774
775 } else if (curlen != 0) {
776 if (curlen != prevlen) {
777 send_code(s, curlen, s->bl_tree); count--;
778 }
779 Assert(count >= 3 && count <= 6, " 3_6?");
780 send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
781
782 } else if (count <= 10) {
783 send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
784
785 } else {
786 send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
787 }
788 count = 0; prevlen = curlen;
789 if (nextlen == 0) {
790 max_count = 138, min_count = 3;
791 } else if (curlen == nextlen) {
792 max_count = 6, min_count = 3;
793 } else {
794 max_count = 7, min_count = 4;
795 }
796 }
797}
798
799/* ===========================================================================
800 * Construct the Huffman tree for the bit lengths and return the index in
801 * bl_order of the last bit length code to send.
802 */
803local int build_bl_tree(s)
804 deflate_state *s;
805{
806 int max_blindex; /* index of last bit length code of non zero freq */
807
808 /* Determine the bit length frequencies for literal and distance trees */
809 scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
810 scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
811
812 /* Build the bit length tree: */
813 build_tree(s, (tree_desc *)(&(s->bl_desc)));
814 /* opt_len now includes the length of the tree representations, except
815 * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
816 */
817
818 /* Determine the number of bit length codes to send. The pkzip format
819 * requires that at least 4 bit length codes be sent. (appnote.txt says
820 * 3 but the actual value used is 4.)
821 */
822 for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
823 if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
824 }
825 /* Update opt_len to include the bit length tree and counts */
826 s->opt_len += 3*(max_blindex+1) + 5+5+4;
827 Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
828 s->opt_len, s->static_len));
829
830 return max_blindex;
831}
832
833/* ===========================================================================
834 * Send the header for a block using dynamic Huffman trees: the counts, the
835 * lengths of the bit length codes, the literal tree and the distance tree.
836 * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
837 */
838local void send_all_trees(s, lcodes, dcodes, blcodes)
839 deflate_state *s;
840 int lcodes, dcodes, blcodes; /* number of codes for each tree */
841{
842 int rank; /* index in bl_order */
843
844 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
845 Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
846 "too many codes");
847 Tracev((stderr, "\nbl counts: "));
848 send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
849 send_bits(s, dcodes-1, 5);
850 send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
851 for (rank = 0; rank < blcodes; rank++) {
852 Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
853 send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
854 }
855 Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
856
857 send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
858 Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
859
860 send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
861 Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
862}
863
864/* ===========================================================================
865 * Send a stored block
866 */
867void _tr_stored_block(s, buf, stored_len, eof)
868 deflate_state *s;
869 charf *buf; /* input block */
870 ulg stored_len; /* length of input block */
871 int eof; /* true if this is the last block for a file */
872{
873 send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */
874#ifdef DEBUG
875 s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
876 s->compressed_len += (stored_len + 4) << 3;
877#endif
878 copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
879}
880
881/* ===========================================================================
882 * Send one empty static block to give enough lookahead for inflate.
883 * This takes 10 bits, of which 7 may remain in the bit buffer.
884 * The current inflate code requires 9 bits of lookahead. If the
885 * last two codes for the previous block (real code plus EOB) were coded
886 * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
887 * the last real code. In this case we send two empty static blocks instead
888 * of one. (There are no problems if the previous block is stored or fixed.)
889 * To simplify the code, we assume the worst case of last real code encoded
890 * on one bit only.
891 */
892void _tr_align(s)
893 deflate_state *s;
894{
895 send_bits(s, STATIC_TREES<<1, 3);
896 send_code(s, END_BLOCK, static_ltree);
897#ifdef DEBUG
898 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
899#endif
900 bi_flush(s);
901 /* Of the 10 bits for the empty block, we have already sent
902 * (10 - bi_valid) bits. The lookahead for the last real code (before
903 * the EOB of the previous block) was thus at least one plus the length
904 * of the EOB plus what we have just sent of the empty static block.
905 */
906 if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
907 send_bits(s, STATIC_TREES<<1, 3);
908 send_code(s, END_BLOCK, static_ltree);
909#ifdef DEBUG
910 s->compressed_len += 10L;
911#endif
912 bi_flush(s);
913 }
914 s->last_eob_len = 7;
915}
916
917/* ===========================================================================
918 * Determine the best encoding for the current block: dynamic trees, static
919 * trees or store, and output the encoded block to the zip file.
920 */
921void _tr_flush_block(s, buf, stored_len, eof)
922 deflate_state *s;
923 charf *buf; /* input block, or NULL if too old */
924 ulg stored_len; /* length of input block */
925 int eof; /* true if this is the last block for a file */
926{
927 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
928 int max_blindex = 0; /* index of last bit length code of non zero freq */
929
930 /* Build the Huffman trees unless a stored block is forced */
931 if (s->level > 0) {
932
933 /* Check if the file is ascii or binary */
934 if (s->data_type == Z_UNKNOWN) set_data_type(s);
935
936 /* Construct the literal and distance trees */
937 build_tree(s, (tree_desc *)(&(s->l_desc)));
938 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
939 s->static_len));
940
941 build_tree(s, (tree_desc *)(&(s->d_desc)));
942 Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
943 s->static_len));
944 /* At this point, opt_len and static_len are the total bit lengths of
945 * the compressed block data, excluding the tree representations.
946 */
947
948 /* Build the bit length tree for the above two trees, and get the index
949 * in bl_order of the last bit length code to send.
950 */
951 max_blindex = build_bl_tree(s);
952
953 /* Determine the best encoding. Compute first the block length in bytes*/
954 opt_lenb = (s->opt_len+3+7)>>3;
955 static_lenb = (s->static_len+3+7)>>3;
956
957 Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
958 opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
959 s->last_lit));
960
961 if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
962
963 } else {
964 Assert(buf != (char*)0, "lost buf");
965 opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
966 }
967
968#ifdef FORCE_STORED
969 if (buf != (char*)0) { /* force stored block */
970#else
971 if (stored_len+4 <= opt_lenb && buf != (char*)0) {
972 /* 4: two words for the lengths */
973#endif
974 /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
975 * Otherwise we can't have processed more than WSIZE input bytes since
976 * the last block flush, because compression would have been
977 * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
978 * transform a block into a stored block.
979 */
980 _tr_stored_block(s, buf, stored_len, eof);
981
982#ifdef FORCE_STATIC
983 } else if (static_lenb >= 0) { /* force static trees */
984#else
985 } else if (static_lenb == opt_lenb) {
986#endif
987 send_bits(s, (STATIC_TREES<<1)+eof, 3);
988 compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
989#ifdef DEBUG
990 s->compressed_len += 3 + s->static_len;
991#endif
992 } else {
993 send_bits(s, (DYN_TREES<<1)+eof, 3);
994 send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
995 max_blindex+1);
996 compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
997#ifdef DEBUG
998 s->compressed_len += 3 + s->opt_len;
999#endif
1000 }
1001 Assert (s->compressed_len == s->bits_sent, "bad compressed size");
1002 /* The above check is made mod 2^32, for files larger than 512 MB
1003 * and uLong implemented on 32 bits.
1004 */
1005 init_block(s);
1006
1007 if (eof) {
1008 bi_windup(s);
1009#ifdef DEBUG
1010 s->compressed_len += 7; /* align on byte boundary */
1011#endif
1012 }
1013 Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
1014 s->compressed_len-7*eof));
1015}
1016
1017/* ===========================================================================
1018 * Save the match info and tally the frequency counts. Return true if
1019 * the current block must be flushed.
1020 */
1021int _tr_tally (s, dist, lc)
1022 deflate_state *s;
1023 unsigned dist; /* distance of matched string */
1024 unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
1025{
1026 s->d_buf[s->last_lit] = (ush)dist;
1027 s->l_buf[s->last_lit++] = (uch)lc;
1028 if (dist == 0) {
1029 /* lc is the unmatched char */
1030 s->dyn_ltree[lc].Freq++;
1031 } else {
1032 s->matches++;
1033 /* Here, lc is the match length - MIN_MATCH */
1034 dist--; /* dist = match distance - 1 */
1035 Assert((ush)dist < (ush)MAX_DIST(s) &&
1036 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
1037 (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
1038
1039 s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
1040 s->dyn_dtree[d_code(dist)].Freq++;
1041 }
1042
1043#ifdef TRUNCATE_BLOCK
1044 /* Try to guess if it is profitable to stop the current block here */
1045 if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
1046 /* Compute an upper bound for the compressed length */
1047 ulg out_length = (ulg)s->last_lit*8L;
1048 ulg in_length = (ulg)((long)s->strstart - s->block_start);
1049 int dcode;
1050 for (dcode = 0; dcode < D_CODES; dcode++) {
1051 out_length += (ulg)s->dyn_dtree[dcode].Freq *
1052 (5L+extra_dbits[dcode]);
1053 }
1054 out_length >>= 3;
1055 Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
1056 s->last_lit, in_length, out_length,
1057 100L - out_length*100L/in_length));
1058 if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
1059 }
1060#endif
1061 return (s->last_lit == s->lit_bufsize-1);
1062 /* We avoid equality with lit_bufsize because of wraparound at 64K
1063 * on 16 bit machines and because stored blocks are restricted to
1064 * 64K-1 bytes.
1065 */
1066}
1067
1068/* ===========================================================================
1069 * Send the block data compressed using the given Huffman trees
1070 */
1071local void compress_block(s, ltree, dtree)
1072 deflate_state *s;
1073 ct_data *ltree; /* literal tree */
1074 ct_data *dtree; /* distance tree */
1075{
1076 unsigned dist; /* distance of matched string */
1077 int lc; /* match length or unmatched char (if dist == 0) */
1078 unsigned lx = 0; /* running index in l_buf */
1079 unsigned code; /* the code to send */
1080 int extra; /* number of extra bits to send */
1081
1082 if (s->last_lit != 0) do {
1083 dist = s->d_buf[lx];
1084 lc = s->l_buf[lx++];
1085 if (dist == 0) {
1086 send_code(s, lc, ltree); /* send a literal byte */
1087 Tracecv(isgraph(lc), (stderr," '%c' ", lc));
1088 } else {
1089 /* Here, lc is the match length - MIN_MATCH */
1090 code = _length_code[lc];
1091 send_code(s, code+LITERALS+1, ltree); /* send the length code */
1092 extra = extra_lbits[code];
1093 if (extra != 0) {
1094 lc -= base_length[code];
1095 send_bits(s, lc, extra); /* send the extra length bits */
1096 }
1097 dist--; /* dist is now the match distance - 1 */
1098 code = d_code(dist);
1099 Assert (code < D_CODES, "bad d_code");
1100
1101 send_code(s, code, dtree); /* send the distance code */
1102 extra = extra_dbits[code];
1103 if (extra != 0) {
1104 dist -= base_dist[code];
1105 send_bits(s, dist, extra); /* send the extra distance bits */
1106 }
1107 } /* literal or match pair ? */
1108
1109 /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
1110 Assert(s->pending < s->lit_bufsize + 2*lx, "pendingBuf overflow");
1111
1112 } while (lx < s->last_lit);
1113
1114 send_code(s, END_BLOCK, ltree);
1115 s->last_eob_len = ltree[END_BLOCK].Len;
1116}
1117
1118/* ===========================================================================
1119 * Set the data type to ASCII or BINARY, using a crude approximation:
1120 * binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
1121 * IN assertion: the fields freq of dyn_ltree are set and the total of all
1122 * frequencies does not exceed 64K (to fit in an int on 16 bit machines).
1123 */
1124local void set_data_type(s)
1125 deflate_state *s;
1126{
1127 int n = 0;
1128 unsigned ascii_freq = 0;
1129 unsigned bin_freq = 0;
1130 while (n < 7) bin_freq += s->dyn_ltree[n++].Freq;
1131 while (n < 128) ascii_freq += s->dyn_ltree[n++].Freq;
1132 while (n < LITERALS) bin_freq += s->dyn_ltree[n++].Freq;
1133 s->data_type = (Byte)(bin_freq > (ascii_freq >> 2) ? Z_BINARY : Z_ASCII);
1134}
1135
1136/* ===========================================================================
1137 * Reverse the first len bits of a code, using straightforward code (a faster
1138 * method would use a table)
1139 * IN assertion: 1 <= len <= 15
1140 */
1141local unsigned bi_reverse(code, len)
1142 unsigned code; /* the value to invert */
1143 int len; /* its bit length */
1144{
1145 register unsigned res = 0;
1146 do {
1147 res |= code & 1;
1148 code >>= 1, res <<= 1;
1149 } while (--len > 0);
1150 return res >> 1;
1151}
1152
1153/* ===========================================================================
1154 * Flush the bit buffer, keeping at most 7 bits in it.
1155 */
1156local void bi_flush(s)
1157 deflate_state *s;
1158{
1159 if (s->bi_valid == 16) {
1160 put_short(s, s->bi_buf);
1161 s->bi_buf = 0;
1162 s->bi_valid = 0;
1163 } else if (s->bi_valid >= 8) {
1164 put_byte(s, (Byte)s->bi_buf);
1165 s->bi_buf >>= 8;
1166 s->bi_valid -= 8;
1167 }
1168}
1169
1170/* ===========================================================================
1171 * Flush the bit buffer and align the output on a byte boundary
1172 */
1173local void bi_windup(s)
1174 deflate_state *s;
1175{
1176 if (s->bi_valid > 8) {
1177 put_short(s, s->bi_buf);
1178 } else if (s->bi_valid > 0) {
1179 put_byte(s, (Byte)s->bi_buf);
1180 }
1181 s->bi_buf = 0;
1182 s->bi_valid = 0;
1183#ifdef DEBUG
1184 s->bits_sent = (s->bits_sent+7) & ~7;
1185#endif
1186}
1187
1188/* ===========================================================================
1189 * Copy a stored block, storing first the length and its
1190 * one's complement if requested.
1191 */
1192local void copy_block(s, buf, len, header)
1193 deflate_state *s;
1194 charf *buf; /* the input data */
1195 unsigned len; /* its length */
1196 int header; /* true if block header must be written */
1197{
1198 bi_windup(s); /* align on byte boundary */
1199 s->last_eob_len = 8; /* enough lookahead for inflate */
1200
1201 if (header) {
1202 put_short(s, (ush)len);
1203 put_short(s, (ush)~len);
1204#ifdef DEBUG
1205 s->bits_sent += 2*16;
1206#endif
1207 }
1208#ifdef DEBUG
1209 s->bits_sent += (ulg)len<<3;
1210#endif
1211 while (len--) {
1212 put_byte(s, *buf++);
1213 }
1214}
diff --git a/pwmanager/libcrypt/zlib/trees.h b/pwmanager/libcrypt/zlib/trees.h
new file mode 100644
index 0000000..72facf9
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/trees.h
@@ -0,0 +1,128 @@
1/* header created automatically with -DGEN_TREES_H */
2
3local const ct_data static_ltree[L_CODES+2] = {
4{{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}},
5{{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}},
6{{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}},
7{{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}},
8{{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}},
9{{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}},
10{{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}},
11{{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}},
12{{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}},
13{{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}},
14{{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}},
15{{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}},
16{{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}},
17{{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}},
18{{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}},
19{{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}},
20{{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}},
21{{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}},
22{{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}},
23{{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}},
24{{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}},
25{{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}},
26{{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}},
27{{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}},
28{{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}},
29{{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}},
30{{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}},
31{{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}},
32{{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}},
33{{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}},
34{{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}},
35{{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}},
36{{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}},
37{{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}},
38{{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}},
39{{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}},
40{{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}},
41{{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}},
42{{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}},
43{{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}},
44{{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}},
45{{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}},
46{{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}},
47{{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}},
48{{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}},
49{{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}},
50{{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}},
51{{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}},
52{{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}},
53{{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}},
54{{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}},
55{{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}},
56{{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}},
57{{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}},
58{{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}},
59{{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}},
60{{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}},
61{{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}}
62};
63
64local const ct_data static_dtree[D_CODES] = {
65{{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}},
66{{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}},
67{{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}},
68{{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}},
69{{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}},
70{{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}}
71};
72
73const uch _dist_code[DIST_CODE_LEN] = {
74 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
75 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
7610, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
7711, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
7812, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
7913, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
8013, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
8114, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
8214, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
8314, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
8415, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
8515, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
8615, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
8718, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
8823, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
8924, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
9026, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
9126, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
9227, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
9327, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
9428, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
9528, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
9628, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
9729, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
9829, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
9929, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
100};
101
102const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {
103 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
10413, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
10517, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
10619, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
10721, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
10822, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
10923, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
11024, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
11125, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
11225, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
11326, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
11426, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
11527, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
116};
117
118local const int base_length[LENGTH_CODES] = {
1190, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
12064, 80, 96, 112, 128, 160, 192, 224, 0
121};
122
123local const int base_dist[D_CODES] = {
124 0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
125 32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
126 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
127};
128
diff --git a/pwmanager/libcrypt/zlib/uncompr.c b/pwmanager/libcrypt/zlib/uncompr.c
new file mode 100644
index 0000000..a287714
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/uncompr.c
@@ -0,0 +1,58 @@
1/* uncompr.c -- decompress a memory buffer
2 * Copyright (C) 1995-2002 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* @(#) $Id$ */
7
8#include "zlib.h"
9
10/* ===========================================================================
11 Decompresses the source buffer into the destination buffer. sourceLen is
12 the byte length of the source buffer. Upon entry, destLen is the total
13 size of the destination buffer, which must be large enough to hold the
14 entire uncompressed data. (The size of the uncompressed data must have
15 been saved previously by the compressor and transmitted to the decompressor
16 by some mechanism outside the scope of this compression library.)
17 Upon exit, destLen is the actual size of the compressed buffer.
18 This function can be used to decompress a whole file at once if the
19 input file is mmap'ed.
20
21 uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
22 enough memory, Z_BUF_ERROR if there was not enough room in the output
23 buffer, or Z_DATA_ERROR if the input data was corrupted.
24*/
25int ZEXPORT uncompress (dest, destLen, source, sourceLen)
26 Bytef *dest;
27 uLongf *destLen;
28 const Bytef *source;
29 uLong sourceLen;
30{
31 z_stream stream;
32 int err;
33
34 stream.next_in = (Bytef*)source;
35 stream.avail_in = (uInt)sourceLen;
36 /* Check for source > 64K on 16-bit machine: */
37 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
38
39 stream.next_out = dest;
40 stream.avail_out = (uInt)*destLen;
41 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
42
43 stream.zalloc = (alloc_func)0;
44 stream.zfree = (free_func)0;
45
46 err = inflateInit(&stream);
47 if (err != Z_OK) return err;
48
49 err = inflate(&stream, Z_FINISH);
50 if (err != Z_STREAM_END) {
51 inflateEnd(&stream);
52 return err == Z_OK ? Z_BUF_ERROR : err;
53 }
54 *destLen = stream.total_out;
55
56 err = inflateEnd(&stream);
57 return err;
58}
diff --git a/pwmanager/libcrypt/zlib/zconf.h b/pwmanager/libcrypt/zlib/zconf.h
new file mode 100644
index 0000000..eb0ae2e
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/zconf.h
@@ -0,0 +1,279 @@
1/* zconf.h -- configuration of the zlib compression library
2 * Copyright (C) 1995-2002 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* @(#) $Id$ */
7
8#ifndef _ZCONF_H
9#define _ZCONF_H
10
11/*
12 * If you *really* need a unique prefix for all types and library functions,
13 * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
14 */
15#ifdef Z_PREFIX
16 # define deflateInit_z_deflateInit_
17 # define deflatez_deflate
18 # define deflateEndz_deflateEnd
19 # define inflateInit_ z_inflateInit_
20 # define inflatez_inflate
21 # define inflateEndz_inflateEnd
22 # define deflateInit2_z_deflateInit2_
23# define deflateSetDictionary z_deflateSetDictionary
24 # define deflateCopyz_deflateCopy
25 # define deflateResetz_deflateReset
26 # define deflateParamsz_deflateParams
27 # define inflateInit2_z_inflateInit2_
28# define inflateSetDictionary z_inflateSetDictionary
29 # define inflateSyncz_inflateSync
30# define inflateSyncPoint z_inflateSyncPoint
31 # define inflateResetz_inflateReset
32 # define compressz_compress
33 # define compress2z_compress2
34 # define uncompressz_uncompress
35 # define adler32z_adler32
36 # define crc32 z_crc32
37# define get_crc_table z_get_crc_table
38
39 # define Byte z_Byte
40 # define uInt z_uInt
41 # define uLong z_uLong
42 # define Bytef z_Bytef
43 # define charf z_charf
44 # define intf z_intf
45 # define uIntf z_uIntf
46 # define uLongfz_uLongf
47 # define voidpfz_voidpf
48 # define voidp z_voidp
49#endif
50
51#if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
52# define WIN32
53#endif
54#if defined(__GNUC__) || defined(WIN32) || defined(__386__) || defined(i386)
55# ifndef __32BIT__
56# define __32BIT__
57# endif
58#endif
59#if defined(__MSDOS__) && !defined(MSDOS)
60# define MSDOS
61#endif
62
63/*
64 * Compile with -DMAXSEG_64K if the alloc function cannot allocate more
65 * than 64k bytes at a time (needed on systems with 16-bit int).
66 */
67#if defined(MSDOS) && !defined(__32BIT__)
68# define MAXSEG_64K
69#endif
70#ifdef MSDOS
71# define UNALIGNED_OK
72#endif
73
74#if (defined(MSDOS) || defined(_WINDOWS) || defined(WIN32)) && !defined(STDC)
75# define STDC
76#endif
77#if defined(__STDC__) || defined(__cplusplus) || defined(__OS2__)
78# ifndef STDC
79# define STDC
80# endif
81#endif
82
83#ifndef STDC
84# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
85# define const
86# endif
87#endif
88
89/* Some Mac compilers merge all .h files incorrectly: */
90#if defined(__MWERKS__) || defined(applec) ||defined(THINK_C) ||defined(__SC__)
91# define NO_DUMMY_DECL
92#endif
93
94/* Old Borland C incorrectly complains about missing returns: */
95#if defined(__BORLANDC__) && (__BORLANDC__ < 0x500)
96# define NEED_DUMMY_RETURN
97#endif
98
99
100/* Maximum value for memLevel in deflateInit2 */
101#ifndef MAX_MEM_LEVEL
102# ifdef MAXSEG_64K
103# define MAX_MEM_LEVEL 8
104# else
105# define MAX_MEM_LEVEL 9
106# endif
107#endif
108
109/* Maximum value for windowBits in deflateInit2 and inflateInit2.
110 * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
111 * created by gzip. (Files created by minigzip can still be extracted by
112 * gzip.)
113 */
114#ifndef MAX_WBITS
115# define MAX_WBITS 15 /* 32K LZ77 window */
116#endif
117
118/* The memory requirements for deflate are (in bytes):
119 (1 << (windowBits+2)) + (1 << (memLevel+9))
120 that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
121 plus a few kilobytes for small objects. For example, if you want to reduce
122 the default memory requirements from 256K to 128K, compile with
123 make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
124 Of course this will generally degrade compression (there's no free lunch).
125
126 The memory requirements for inflate are (in bytes) 1 << windowBits
127 that is, 32K for windowBits=15 (default value) plus a few kilobytes
128 for small objects.
129*/
130
131 /* Type declarations */
132
133#ifndef OF /* function prototypes */
134# ifdef STDC
135# define OF(args) args
136# else
137# define OF(args) ()
138# endif
139#endif
140
141/* The following definitions for FAR are needed only for MSDOS mixed
142 * model programming (small or medium model with some far allocations).
143 * This was tested only with MSC; for other MSDOS compilers you may have
144 * to define NO_MEMCPY in zutil.h. If you don't need the mixed model,
145 * just define FAR to be empty.
146 */
147#if (defined(M_I86SM) || defined(M_I86MM)) && !defined(__32BIT__)
148 /* MSC small or medium model */
149# define SMALL_MEDIUM
150# ifdef _MSC_VER
151# define FAR _far
152# else
153# define FAR far
154# endif
155#endif
156#if defined(__BORLANDC__) && (defined(__SMALL__) || defined(__MEDIUM__))
157# ifndef __32BIT__
158# define SMALL_MEDIUM
159# define FAR _far
160# endif
161#endif
162
163/* Compile with -DZLIB_DLL for Windows DLL support */
164#if defined(ZLIB_DLL)
165# if defined(_WINDOWS) || defined(WINDOWS)
166# ifdef FAR
167# undef FAR
168# endif
169# include <windows.h>
170# define ZEXPORT WINAPI
171# ifdef WIN32
172# define ZEXPORTVA WINAPIV
173# else
174# define ZEXPORTVA FAR _cdecl _export
175# endif
176# endif
177# if defined (__BORLANDC__)
178# if (__BORLANDC__ >= 0x0500) && defined (WIN32)
179# include <windows.h>
180# define ZEXPORT __declspec(dllexport) WINAPI
181# define ZEXPORTRVA __declspec(dllexport) WINAPIV
182# else
183# if defined (_Windows) && defined (__DLL__)
184# define ZEXPORT _export
185# define ZEXPORTVA _export
186# endif
187# endif
188# endif
189#endif
190
191#if defined (__BEOS__)
192# if defined (ZLIB_DLL)
193# define ZEXTERN extern __declspec(dllexport)
194# else
195# define ZEXTERN extern __declspec(dllimport)
196# endif
197#endif
198
199#ifndef ZEXPORT
200# define ZEXPORT
201#endif
202#ifndef ZEXPORTVA
203# define ZEXPORTVA
204#endif
205#ifndef ZEXTERN
206# define ZEXTERN extern
207#endif
208
209#ifndef FAR
210# define FAR
211#endif
212
213#if !defined(MACOS) && !defined(TARGET_OS_MAC)
214typedef unsigned char Byte; /* 8 bits */
215#endif
216typedef unsigned int uInt; /* 16 bits or more */
217typedef unsigned long uLong; /* 32 bits or more */
218
219#ifdef SMALL_MEDIUM
220 /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */
221# define Bytef Byte FAR
222#else
223 typedef Byte FAR Bytef;
224#endif
225typedef char FAR charf;
226typedef int FAR intf;
227typedef uInt FAR uIntf;
228typedef uLong FAR uLongf;
229
230#ifdef STDC
231 typedef void FAR *voidpf;
232 typedef void *voidp;
233#else
234 typedef Byte FAR *voidpf;
235 typedef Byte *voidp;
236#endif
237
238#ifdef HAVE_UNISTD_H
239# include <sys/types.h> /* for off_t */
240# include <unistd.h> /* for SEEK_* and off_t */
241# define z_off_t off_t
242#endif
243#ifndef SEEK_SET
244# define SEEK_SET 0 /* Seek from beginning of file. */
245# define SEEK_CUR 1 /* Seek from current position. */
246# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
247#endif
248#ifndef z_off_t
249# define z_off_t long
250#endif
251
252/* MVS linker does not support external names larger than 8 bytes */
253#if defined(__MVS__)
254# pragma map(deflateInit_,"DEIN")
255# pragma map(deflateInit2_,"DEIN2")
256# pragma map(deflateEnd,"DEEND")
257# pragma map(inflateInit_,"ININ")
258# pragma map(inflateInit2_,"ININ2")
259# pragma map(inflateEnd,"INEND")
260# pragma map(inflateSync,"INSY")
261# pragma map(inflateSetDictionary,"INSEDI")
262# pragma map(inflate_blocks,"INBL")
263# pragma map(inflate_blocks_new,"INBLNE")
264# pragma map(inflate_blocks_free,"INBLFR")
265# pragma map(inflate_blocks_reset,"INBLRE")
266# pragma map(inflate_codes_free,"INCOFR")
267# pragma map(inflate_codes,"INCO")
268# pragma map(inflate_fast,"INFA")
269# pragma map(inflate_flush,"INFLU")
270# pragma map(inflate_mask,"INMA")
271# pragma map(inflate_set_dictionary,"INSEDI2")
272# pragma map(inflate_copyright,"INCOPY")
273# pragma map(inflate_trees_bits,"INTRBI")
274# pragma map(inflate_trees_dynamic,"INTRDY")
275# pragma map(inflate_trees_fixed,"INTRFI")
276# pragma map(inflate_trees_free,"INTRFR")
277#endif
278
279#endif /* _ZCONF_H */
diff --git a/pwmanager/libcrypt/zlib/zlib.h b/pwmanager/libcrypt/zlib/zlib.h
new file mode 100644
index 0000000..52cb529
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/zlib.h
@@ -0,0 +1,893 @@
1/* zlib.h -- interface of the 'zlib' general purpose compression library
2 version 1.1.4, March 11th, 2002
3
4 Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler
5
6 This software is provided 'as-is', without any express or implied
7 warranty. In no event will the authors be held liable for any damages
8 arising from the use of this software.
9
10 Permission is granted to anyone to use this software for any purpose,
11 including commercial applications, and to alter it and redistribute it
12 freely, subject to the following restrictions:
13
14 1. The origin of this software must not be misrepresented; you must not
15 claim that you wrote the original software. If you use this software
16 in a product, an acknowledgment in the product documentation would be
17 appreciated but is not required.
18 2. Altered source versions must be plainly marked as such, and must not be
19 misrepresented as being the original software.
20 3. This notice may not be removed or altered from any source distribution.
21
22 Jean-loup Gailly Mark Adler
23 jloup@gzip.org madler@alumni.caltech.edu
24
25
26 The data format used by the zlib library is described by RFCs (Request for
27 Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
28 (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
29*/
30
31#ifndef _ZLIB_H
32#define _ZLIB_H
33
34#include "zconf.h"
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40#define ZLIB_VERSION "1.1.4"
41
42/*
43 The 'zlib' compression library provides in-memory compression and
44 decompression functions, including integrity checks of the uncompressed
45 data. This version of the library supports only one compression method
46 (deflation) but other algorithms will be added later and will have the same
47 stream interface.
48
49 Compression can be done in a single step if the buffers are large
50 enough (for example if an input file is mmap'ed), or can be done by
51 repeated calls of the compression function. In the latter case, the
52 application must provide more input and/or consume the output
53 (providing more output space) before each call.
54
55 The library also supports reading and writing files in gzip (.gz) format
56 with an interface similar to that of stdio.
57
58 The library does not install any signal handler. The decoder checks
59 the consistency of the compressed data, so the library should never
60 crash even in case of corrupted input.
61*/
62
63typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
64typedef void (*free_func) OF((voidpf opaque, voidpf address));
65
66struct internal_state;
67
68typedef struct z_stream_s {
69 Bytef *next_in; /* next input byte */
70 uInt avail_in; /* number of bytes available at next_in */
71 uLong total_in; /* total nb of input bytes read so far */
72
73 Bytef *next_out; /* next output byte should be put there */
74 uInt avail_out; /* remaining free space at next_out */
75 uLong total_out; /* total nb of bytes output so far */
76
77 char *msg; /* last error message, NULL if no error */
78 struct internal_state FAR *state; /* not visible by applications */
79
80 alloc_func zalloc; /* used to allocate the internal state */
81 free_func zfree; /* used to free the internal state */
82 voidpf opaque; /* private data object passed to zalloc and zfree */
83
84 int data_type; /* best guess about the data type: ascii or binary */
85 uLong adler; /* adler32 value of the uncompressed data */
86 uLong reserved; /* reserved for future use */
87} z_stream;
88
89typedef z_stream FAR *z_streamp;
90
91/*
92 The application must update next_in and avail_in when avail_in has
93 dropped to zero. It must update next_out and avail_out when avail_out
94 has dropped to zero. The application must initialize zalloc, zfree and
95 opaque before calling the init function. All other fields are set by the
96 compression library and must not be updated by the application.
97
98 The opaque value provided by the application will be passed as the first
99 parameter for calls of zalloc and zfree. This can be useful for custom
100 memory management. The compression library attaches no meaning to the
101 opaque value.
102
103 zalloc must return Z_NULL if there is not enough memory for the object.
104 If zlib is used in a multi-threaded application, zalloc and zfree must be
105 thread safe.
106
107 On 16-bit systems, the functions zalloc and zfree must be able to allocate
108 exactly 65536 bytes, but will not be required to allocate more than this
109 if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
110 pointers returned by zalloc for objects of exactly 65536 bytes *must*
111 have their offset normalized to zero. The default allocation function
112 provided by this library ensures this (see zutil.c). To reduce memory
113 requirements and avoid any allocation of 64K objects, at the expense of
114 compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
115
116 The fields total_in and total_out can be used for statistics or
117 progress reports. After compression, total_in holds the total size of
118 the uncompressed data and may be saved for use in the decompressor
119 (particularly if the decompressor wants to decompress everything in
120 a single step).
121*/
122
123 /* constants */
124
125#define Z_NO_FLUSH 0
126#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
127#define Z_SYNC_FLUSH 2
128#define Z_FULL_FLUSH 3
129#define Z_FINISH 4
130/* Allowed flush values; see deflate() below for details */
131
132#define Z_OK 0
133#define Z_STREAM_END 1
134#define Z_NEED_DICT 2
135#define Z_ERRNO (-1)
136#define Z_STREAM_ERROR (-2)
137#define Z_DATA_ERROR (-3)
138#define Z_MEM_ERROR (-4)
139#define Z_BUF_ERROR (-5)
140#define Z_VERSION_ERROR (-6)
141/* Return codes for the compression/decompression functions. Negative
142 * values are errors, positive values are used for special but normal events.
143 */
144
145#define Z_NO_COMPRESSION 0
146#define Z_BEST_SPEED 1
147#define Z_BEST_COMPRESSION 9
148#define Z_DEFAULT_COMPRESSION (-1)
149/* compression levels */
150
151#define Z_FILTERED 1
152#define Z_HUFFMAN_ONLY 2
153#define Z_DEFAULT_STRATEGY 0
154/* compression strategy; see deflateInit2() below for details */
155
156#define Z_BINARY 0
157#define Z_ASCII 1
158#define Z_UNKNOWN 2
159/* Possible values of the data_type field */
160
161#define Z_DEFLATED 8
162/* The deflate compression method (the only one supported in this version) */
163
164#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
165
166#define zlib_version zlibVersion()
167/* for compatibility with versions < 1.0.2 */
168
169 /* basic functions */
170
171ZEXTERN const char * ZEXPORT zlibVersion OF((void));
172/* The application can compare zlibVersion and ZLIB_VERSION for consistency.
173 If the first character differs, the library code actually used is
174 not compatible with the zlib.h header file used by the application.
175 This check is automatically made by deflateInit and inflateInit.
176 */
177
178/*
179ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level));
180
181 Initializes the internal stream state for compression. The fields
182 zalloc, zfree and opaque must be initialized before by the caller.
183 If zalloc and zfree are set to Z_NULL, deflateInit updates them to
184 use default allocation functions.
185
186 The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
187 1 gives best speed, 9 gives best compression, 0 gives no compression at
188 all (the input data is simply copied a block at a time).
189 Z_DEFAULT_COMPRESSION requests a default compromise between speed and
190 compression (currently equivalent to level 6).
191
192 deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
193 enough memory, Z_STREAM_ERROR if level is not a valid compression level,
194 Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
195 with the version assumed by the caller (ZLIB_VERSION).
196 msg is set to null if there is no error message. deflateInit does not
197 perform any compression: this will be done by deflate().
198*/
199
200
201ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
202/*
203 deflate compresses as much data as possible, and stops when the input
204 buffer becomes empty or the output buffer becomes full. It may introduce some
205 output latency (reading input without producing any output) except when
206 forced to flush.
207
208 The detailed semantics are as follows. deflate performs one or both of the
209 following actions:
210
211 - Compress more input starting at next_in and update next_in and avail_in
212 accordingly. If not all input can be processed (because there is not
213 enough room in the output buffer), next_in and avail_in are updated and
214 processing will resume at this point for the next call of deflate().
215
216 - Provide more output starting at next_out and update next_out and avail_out
217 accordingly. This action is forced if the parameter flush is non zero.
218 Forcing flush frequently degrades the compression ratio, so this parameter
219 should be set only when necessary (in interactive applications).
220 Some output may be provided even if flush is not set.
221
222 Before the call of deflate(), the application should ensure that at least
223 one of the actions is possible, by providing more input and/or consuming
224 more output, and updating avail_in or avail_out accordingly; avail_out
225 should never be zero before the call. The application can consume the
226 compressed output when it wants, for example when the output buffer is full
227 (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
228 and with zero avail_out, it must be called again after making room in the
229 output buffer because there might be more output pending.
230
231 If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
232 flushed to the output buffer and the output is aligned on a byte boundary, so
233 that the decompressor can get all input data available so far. (In particular
234 avail_in is zero after the call if enough output space has been provided
235 before the call.) Flushing may degrade compression for some compression
236 algorithms and so it should be used only when necessary.
237
238 If flush is set to Z_FULL_FLUSH, all output is flushed as with
239 Z_SYNC_FLUSH, and the compression state is reset so that decompression can
240 restart from this point if previous compressed data has been damaged or if
241 random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
242 the compression.
243
244 If deflate returns with avail_out == 0, this function must be called again
245 with the same value of the flush parameter and more output space (updated
246 avail_out), until the flush is complete (deflate returns with non-zero
247 avail_out).
248
249 If the parameter flush is set to Z_FINISH, pending input is processed,
250 pending output is flushed and deflate returns with Z_STREAM_END if there
251 was enough output space; if deflate returns with Z_OK, this function must be
252 called again with Z_FINISH and more output space (updated avail_out) but no
253 more input data, until it returns with Z_STREAM_END or an error. After
254 deflate has returned Z_STREAM_END, the only possible operations on the
255 stream are deflateReset or deflateEnd.
256
257 Z_FINISH can be used immediately after deflateInit if all the compression
258 is to be done in a single step. In this case, avail_out must be at least
259 0.1% larger than avail_in plus 12 bytes. If deflate does not return
260 Z_STREAM_END, then it must be called again as described above.
261
262 deflate() sets strm->adler to the adler32 checksum of all input read
263 so far (that is, total_in bytes).
264
265 deflate() may update data_type if it can make a good guess about
266 the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered
267 binary. This field is only for information purposes and does not affect
268 the compression algorithm in any manner.
269
270 deflate() returns Z_OK if some progress has been made (more input
271 processed or more output produced), Z_STREAM_END if all input has been
272 consumed and all output has been produced (only when flush is set to
273 Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
274 if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
275 (for example avail_in or avail_out was zero).
276*/
277
278
279ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
280/*
281 All dynamically allocated data structures for this stream are freed.
282 This function discards any unprocessed input and does not flush any
283 pending output.
284
285 deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
286 stream state was inconsistent, Z_DATA_ERROR if the stream was freed
287 prematurely (some input or output was discarded). In the error case,
288 msg may be set but then points to a static string (which must not be
289 deallocated).
290*/
291
292
293/*
294ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
295
296 Initializes the internal stream state for decompression. The fields
297 next_in, avail_in, zalloc, zfree and opaque must be initialized before by
298 the caller. If next_in is not Z_NULL and avail_in is large enough (the exact
299 value depends on the compression method), inflateInit determines the
300 compression method from the zlib header and allocates all data structures
301 accordingly; otherwise the allocation will be deferred to the first call of
302 inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to
303 use default allocation functions.
304
305 inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
306 memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
307 version assumed by the caller. msg is set to null if there is no error
308 message. inflateInit does not perform any decompression apart from reading
309 the zlib header if present: this will be done by inflate(). (So next_in and
310 avail_in may be modified, but next_out and avail_out are unchanged.)
311*/
312
313
314ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
315/*
316 inflate decompresses as much data as possible, and stops when the input
317 buffer becomes empty or the output buffer becomes full. It may some
318 introduce some output latency (reading input without producing any output)
319 except when forced to flush.
320
321 The detailed semantics are as follows. inflate performs one or both of the
322 following actions:
323
324 - Decompress more input starting at next_in and update next_in and avail_in
325 accordingly. If not all input can be processed (because there is not
326 enough room in the output buffer), next_in is updated and processing
327 will resume at this point for the next call of inflate().
328
329 - Provide more output starting at next_out and update next_out and avail_out
330 accordingly. inflate() provides as much output as possible, until there
331 is no more input data or no more space in the output buffer (see below
332 about the flush parameter).
333
334 Before the call of inflate(), the application should ensure that at least
335 one of the actions is possible, by providing more input and/or consuming
336 more output, and updating the next_* and avail_* values accordingly.
337 The application can consume the uncompressed output when it wants, for
338 example when the output buffer is full (avail_out == 0), or after each
339 call of inflate(). If inflate returns Z_OK and with zero avail_out, it
340 must be called again after making room in the output buffer because there
341 might be more output pending.
342
343 If the parameter flush is set to Z_SYNC_FLUSH, inflate flushes as much
344 output as possible to the output buffer. The flushing behavior of inflate is
345 not specified for values of the flush parameter other than Z_SYNC_FLUSH
346 and Z_FINISH, but the current implementation actually flushes as much output
347 as possible anyway.
348
349 inflate() should normally be called until it returns Z_STREAM_END or an
350 error. However if all decompression is to be performed in a single step
351 (a single call of inflate), the parameter flush should be set to
352 Z_FINISH. In this case all pending input is processed and all pending
353 output is flushed; avail_out must be large enough to hold all the
354 uncompressed data. (The size of the uncompressed data may have been saved
355 by the compressor for this purpose.) The next operation on this stream must
356 be inflateEnd to deallocate the decompression state. The use of Z_FINISH
357 is never required, but can be used to inform inflate that a faster routine
358 may be used for the single inflate() call.
359
360 If a preset dictionary is needed at this point (see inflateSetDictionary
361 below), inflate sets strm-adler to the adler32 checksum of the
362 dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise
363 it sets strm->adler to the adler32 checksum of all output produced
364 so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or
365 an error code as described below. At the end of the stream, inflate()
366 checks that its computed adler32 checksum is equal to that saved by the
367 compressor and returns Z_STREAM_END only if the checksum is correct.
368
369 inflate() returns Z_OK if some progress has been made (more input processed
370 or more output produced), Z_STREAM_END if the end of the compressed data has
371 been reached and all uncompressed output has been produced, Z_NEED_DICT if a
372 preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
373 corrupted (input stream not conforming to the zlib format or incorrect
374 adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent
375 (for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not
376 enough memory, Z_BUF_ERROR if no progress is possible or if there was not
377 enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR
378 case, the application may then call inflateSync to look for a good
379 compression block.
380*/
381
382
383ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));
384/*
385 All dynamically allocated data structures for this stream are freed.
386 This function discards any unprocessed input and does not flush any
387 pending output.
388
389 inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
390 was inconsistent. In the error case, msg may be set but then points to a
391 static string (which must not be deallocated).
392*/
393
394 /* Advanced functions */
395
396/*
397 The following functions are needed only in some special applications.
398*/
399
400/*
401ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
402 int level,
403 int method,
404 int windowBits,
405 int memLevel,
406 int strategy));
407
408 This is another version of deflateInit with more compression options. The
409 fields next_in, zalloc, zfree and opaque must be initialized before by
410 the caller.
411
412 The method parameter is the compression method. It must be Z_DEFLATED in
413 this version of the library.
414
415 The windowBits parameter is the base two logarithm of the window size
416 (the size of the history buffer). It should be in the range 8..15 for this
417 version of the library. Larger values of this parameter result in better
418 compression at the expense of memory usage. The default value is 15 if
419 deflateInit is used instead.
420
421 The memLevel parameter specifies how much memory should be allocated
422 for the internal compression state. memLevel=1 uses minimum memory but
423 is slow and reduces compression ratio; memLevel=9 uses maximum memory
424 for optimal speed. The default value is 8. See zconf.h for total memory
425 usage as a function of windowBits and memLevel.
426
427 The strategy parameter is used to tune the compression algorithm. Use the
428 value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
429 filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no
430 string match). Filtered data consists mostly of small values with a
431 somewhat random distribution. In this case, the compression algorithm is
432 tuned to compress them better. The effect of Z_FILTERED is to force more
433 Huffman coding and less string matching; it is somewhat intermediate
434 between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects
435 the compression ratio but not the correctness of the compressed output even
436 if it is not set appropriately.
437
438 deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
439 memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
440 method). msg is set to null if there is no error message. deflateInit2 does
441 not perform any compression: this will be done by deflate().
442*/
443
444ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
445 const Bytef *dictionary,
446 uInt dictLength));
447/*
448 Initializes the compression dictionary from the given byte sequence
449 without producing any compressed output. This function must be called
450 immediately after deflateInit, deflateInit2 or deflateReset, before any
451 call of deflate. The compressor and decompressor must use exactly the same
452 dictionary (see inflateSetDictionary).
453
454 The dictionary should consist of strings (byte sequences) that are likely
455 to be encountered later in the data to be compressed, with the most commonly
456 used strings preferably put towards the end of the dictionary. Using a
457 dictionary is most useful when the data to be compressed is short and can be
458 predicted with good accuracy; the data can then be compressed better than
459 with the default empty dictionary.
460
461 Depending on the size of the compression data structures selected by
462 deflateInit or deflateInit2, a part of the dictionary may in effect be
463 discarded, for example if the dictionary is larger than the window size in
464 deflate or deflate2. Thus the strings most likely to be useful should be
465 put at the end of the dictionary, not at the front.
466
467 Upon return of this function, strm->adler is set to the Adler32 value
468 of the dictionary; the decompressor may later use this value to determine
469 which dictionary has been used by the compressor. (The Adler32 value
470 applies to the whole dictionary even if only a subset of the dictionary is
471 actually used by the compressor.)
472
473 deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
474 parameter is invalid (such as NULL dictionary) or the stream state is
475 inconsistent (for example if deflate has already been called for this stream
476 or if the compression method is bsort). deflateSetDictionary does not
477 perform any compression: this will be done by deflate().
478*/
479
480ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
481 z_streamp source));
482/*
483 Sets the destination stream as a complete copy of the source stream.
484
485 This function can be useful when several compression strategies will be
486 tried, for example when there are several ways of pre-processing the input
487 data with a filter. The streams that will be discarded should then be freed
488 by calling deflateEnd. Note that deflateCopy duplicates the internal
489 compression state which can be quite large, so this strategy is slow and
490 can consume lots of memory.
491
492 deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
493 enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
494 (such as zalloc being NULL). msg is left unchanged in both source and
495 destination.
496*/
497
498ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));
499/*
500 This function is equivalent to deflateEnd followed by deflateInit,
501 but does not free and reallocate all the internal compression state.
502 The stream will keep the same compression level and any other attributes
503 that may have been set by deflateInit2.
504
505 deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
506 stream state was inconsistent (such as zalloc or state being NULL).
507*/
508
509ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
510 int level,
511 int strategy));
512/*
513 Dynamically update the compression level and compression strategy. The
514 interpretation of level and strategy is as in deflateInit2. This can be
515 used to switch between compression and straight copy of the input data, or
516 to switch to a different kind of input data requiring a different
517 strategy. If the compression level is changed, the input available so far
518 is compressed with the old level (and may be flushed); the new level will
519 take effect only at the next call of deflate().
520
521 Before the call of deflateParams, the stream state must be set as for
522 a call of deflate(), since the currently available input may have to
523 be compressed and flushed. In particular, strm->avail_out must be non-zero.
524
525 deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
526 stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
527 if strm->avail_out was zero.
528*/
529
530/*
531ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
532 int windowBits));
533
534 This is another version of inflateInit with an extra parameter. The
535 fields next_in, avail_in, zalloc, zfree and opaque must be initialized
536 before by the caller.
537
538 The windowBits parameter is the base two logarithm of the maximum window
539 size (the size of the history buffer). It should be in the range 8..15 for
540 this version of the library. The default value is 15 if inflateInit is used
541 instead. If a compressed stream with a larger window size is given as
542 input, inflate() will return with the error code Z_DATA_ERROR instead of
543 trying to allocate a larger window.
544
545 inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
546 memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative
547 memLevel). msg is set to null if there is no error message. inflateInit2
548 does not perform any decompression apart from reading the zlib header if
549 present: this will be done by inflate(). (So next_in and avail_in may be
550 modified, but next_out and avail_out are unchanged.)
551*/
552
553ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
554 const Bytef *dictionary,
555 uInt dictLength));
556/*
557 Initializes the decompression dictionary from the given uncompressed byte
558 sequence. This function must be called immediately after a call of inflate
559 if this call returned Z_NEED_DICT. The dictionary chosen by the compressor
560 can be determined from the Adler32 value returned by this call of
561 inflate. The compressor and decompressor must use exactly the same
562 dictionary (see deflateSetDictionary).
563
564 inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
565 parameter is invalid (such as NULL dictionary) or the stream state is
566 inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
567 expected one (incorrect Adler32 value). inflateSetDictionary does not
568 perform any decompression: this will be done by subsequent calls of
569 inflate().
570*/
571
572ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
573/*
574 Skips invalid compressed data until a full flush point (see above the
575 description of deflate with Z_FULL_FLUSH) can be found, or until all
576 available input is skipped. No output is provided.
577
578 inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
579 if no more input was provided, Z_DATA_ERROR if no flush point has been found,
580 or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
581 case, the application may save the current current value of total_in which
582 indicates where valid compressed data was found. In the error case, the
583 application may repeatedly call inflateSync, providing more input each time,
584 until success or end of the input data.
585*/
586
587ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
588/*
589 This function is equivalent to inflateEnd followed by inflateInit,
590 but does not free and reallocate all the internal decompression state.
591 The stream will keep attributes that may have been set by inflateInit2.
592
593 inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
594 stream state was inconsistent (such as zalloc or state being NULL).
595*/
596
597
598 /* utility functions */
599
600/*
601 The following utility functions are implemented on top of the
602 basic stream-oriented functions. To simplify the interface, some
603 default options are assumed (compression level and memory usage,
604 standard memory allocation functions). The source code of these
605 utility functions can easily be modified if you need special options.
606*/
607
608ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
609 const Bytef *source, uLong sourceLen));
610/*
611 Compresses the source buffer into the destination buffer. sourceLen is
612 the byte length of the source buffer. Upon entry, destLen is the total
613 size of the destination buffer, which must be at least 0.1% larger than
614 sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
615 compressed buffer.
616 This function can be used to compress a whole file at once if the
617 input file is mmap'ed.
618 compress returns Z_OK if success, Z_MEM_ERROR if there was not
619 enough memory, Z_BUF_ERROR if there was not enough room in the output
620 buffer.
621*/
622
623ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen,
624 const Bytef *source, uLong sourceLen,
625 int level));
626/*
627 Compresses the source buffer into the destination buffer. The level
628 parameter has the same meaning as in deflateInit. sourceLen is the byte
629 length of the source buffer. Upon entry, destLen is the total size of the
630 destination buffer, which must be at least 0.1% larger than sourceLen plus
631 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
632
633 compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
634 memory, Z_BUF_ERROR if there was not enough room in the output buffer,
635 Z_STREAM_ERROR if the level parameter is invalid.
636*/
637
638ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
639 const Bytef *source, uLong sourceLen));
640/*
641 Decompresses the source buffer into the destination buffer. sourceLen is
642 the byte length of the source buffer. Upon entry, destLen is the total
643 size of the destination buffer, which must be large enough to hold the
644 entire uncompressed data. (The size of the uncompressed data must have
645 been saved previously by the compressor and transmitted to the decompressor
646 by some mechanism outside the scope of this compression library.)
647 Upon exit, destLen is the actual size of the compressed buffer.
648 This function can be used to decompress a whole file at once if the
649 input file is mmap'ed.
650
651 uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
652 enough memory, Z_BUF_ERROR if there was not enough room in the output
653 buffer, or Z_DATA_ERROR if the input data was corrupted.
654*/
655
656
657typedef voidp gzFile;
658
659ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode));
660/*
661 Opens a gzip (.gz) file for reading or writing. The mode parameter
662 is as in fopen ("rb" or "wb") but can also include a compression level
663 ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
664 Huffman only compression as in "wb1h". (See the description
665 of deflateInit2 for more information about the strategy parameter.)
666
667 gzopen can be used to read a file which is not in gzip format; in this
668 case gzread will directly read from the file without decompression.
669
670 gzopen returns NULL if the file could not be opened or if there was
671 insufficient memory to allocate the (de)compression state; errno
672 can be checked to distinguish the two cases (if errno is zero, the
673 zlib error is Z_MEM_ERROR). */
674
675ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode));
676/*
677 gzdopen() associates a gzFile with the file descriptor fd. File
678 descriptors are obtained from calls like open, dup, creat, pipe or
679 fileno (in the file has been previously opened with fopen).
680 The mode parameter is as in gzopen.
681 The next call of gzclose on the returned gzFile will also close the
682 file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
683 descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
684 gzdopen returns NULL if there was insufficient memory to allocate
685 the (de)compression state.
686*/
687
688ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
689/*
690 Dynamically update the compression level or strategy. See the description
691 of deflateInit2 for the meaning of these parameters.
692 gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
693 opened for writing.
694*/
695
696ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
697/*
698 Reads the given number of uncompressed bytes from the compressed file.
699 If the input file was not in gzip format, gzread copies the given number
700 of bytes into the buffer.
701 gzread returns the number of uncompressed bytes actually read (0 for
702 end of file, -1 for error). */
703
704ZEXTERN int ZEXPORT gzwrite OF((gzFile file,
705 const voidp buf, unsigned len));
706/*
707 Writes the given number of uncompressed bytes into the compressed file.
708 gzwrite returns the number of uncompressed bytes actually written
709 (0 in case of error).
710*/
711
712ZEXTERN int ZEXPORTVA gzprintf OF((gzFile file, const char *format, ...));
713/*
714 Converts, formats, and writes the args to the compressed file under
715 control of the format string, as in fprintf. gzprintf returns the number of
716 uncompressed bytes actually written (0 in case of error).
717*/
718
719ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
720/*
721 Writes the given null-terminated string to the compressed file, excluding
722 the terminating null character.
723 gzputs returns the number of characters written, or -1 in case of error.
724*/
725
726ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
727/*
728 Reads bytes from the compressed file until len-1 characters are read, or
729 a newline character is read and transferred to buf, or an end-of-file
730 condition is encountered. The string is then terminated with a null
731 character.
732 gzgets returns buf, or Z_NULL in case of error.
733*/
734
735ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c));
736/*
737 Writes c, converted to an unsigned char, into the compressed file.
738 gzputc returns the value that was written, or -1 in case of error.
739*/
740
741ZEXTERN int ZEXPORT gzgetc OF((gzFile file));
742/*
743 Reads one byte from the compressed file. gzgetc returns this byte
744 or -1 in case of end of file or error.
745*/
746
747ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush));
748/*
749 Flushes all pending output into the compressed file. The parameter
750 flush is as in the deflate() function. The return value is the zlib
751 error number (see function gzerror below). gzflush returns Z_OK if
752 the flush parameter is Z_FINISH and all output could be flushed.
753 gzflush should be called only when strictly necessary because it can
754 degrade compression.
755*/
756
757ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file,
758 z_off_t offset, int whence));
759/*
760 Sets the starting position for the next gzread or gzwrite on the
761 given compressed file. The offset represents a number of bytes in the
762 uncompressed data stream. The whence parameter is defined as in lseek(2);
763 the value SEEK_END is not supported.
764 If the file is opened for reading, this function is emulated but can be
765 extremely slow. If the file is opened for writing, only forward seeks are
766 supported; gzseek then compresses a sequence of zeroes up to the new
767 starting position.
768
769 gzseek returns the resulting offset location as measured in bytes from
770 the beginning of the uncompressed stream, or -1 in case of error, in
771 particular if the file is opened for writing and the new starting position
772 would be before the current position.
773*/
774
775ZEXTERN int ZEXPORT gzrewind OF((gzFile file));
776/*
777 Rewinds the given file. This function is supported only for reading.
778
779 gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
780*/
781
782ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file));
783/*
784 Returns the starting position for the next gzread or gzwrite on the
785 given compressed file. This position represents a number of bytes in the
786 uncompressed data stream.
787
788 gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
789*/
790
791ZEXTERN int ZEXPORT gzeof OF((gzFile file));
792/*
793 Returns 1 when EOF has previously been detected reading the given
794 input stream, otherwise zero.
795*/
796
797ZEXTERN int ZEXPORT gzclose OF((gzFile file));
798/*
799 Flushes all pending output if necessary, closes the compressed file
800 and deallocates all the (de)compression state. The return value is the zlib
801 error number (see function gzerror below).
802*/
803
804ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
805/*
806 Returns the error message for the last error which occurred on the
807 given compressed file. errnum is set to zlib error number. If an
808 error occurred in the file system and not in the compression library,
809 errnum is set to Z_ERRNO and the application may consult errno
810 to get the exact error code.
811*/
812
813 /* checksum functions */
814
815/*
816 These functions are not related to compression but are exported
817 anyway because they might be useful in applications using the
818 compression library.
819*/
820
821ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
822
823/*
824 Update a running Adler-32 checksum with the bytes buf[0..len-1] and
825 return the updated checksum. If buf is NULL, this function returns
826 the required initial value for the checksum.
827 An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
828 much faster. Usage example:
829
830 uLong adler = adler32(0L, Z_NULL, 0);
831
832 while (read_buffer(buffer, length) != EOF) {
833 adler = adler32(adler, buffer, length);
834 }
835 if (adler != original_adler) error();
836*/
837
838ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len));
839/*
840 Update a running crc with the bytes buf[0..len-1] and return the updated
841 crc. If buf is NULL, this function returns the required initial value
842 for the crc. Pre- and post-conditioning (one's complement) is performed
843 within this function so it shouldn't be done by the application.
844 Usage example:
845
846 uLong crc = crc32(0L, Z_NULL, 0);
847
848 while (read_buffer(buffer, length) != EOF) {
849 crc = crc32(crc, buffer, length);
850 }
851 if (crc != original_crc) error();
852*/
853
854
855 /* various hacks, don't look :) */
856
857/* deflateInit and inflateInit are macros to allow checking the zlib version
858 * and the compiler's view of z_stream:
859 */
860ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level,
861 const char *version, int stream_size));
862ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm,
863 const char *version, int stream_size));
864ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method,
865 int windowBits, int memLevel,
866 int strategy, const char *version,
867 int stream_size));
868ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits,
869 const char *version, int stream_size));
870#define deflateInit(strm, level) \
871 deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
872#define inflateInit(strm) \
873 inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream))
874#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
875 deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
876 (strategy), ZLIB_VERSION, sizeof(z_stream))
877#define inflateInit2(strm, windowBits) \
878 inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
879
880
881#if !defined(_Z_UTIL_H) && !defined(NO_DUMMY_DECL)
882 struct internal_state {int dummy;}; /* hack for buggy compilers */
883#endif
884
885ZEXTERN const char * ZEXPORT zError OF((int err));
886ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp z));
887ZEXTERN const uLongf * ZEXPORT get_crc_table OF((void));
888
889#ifdef __cplusplus
890}
891#endif
892
893#endif /* _ZLIB_H */
diff --git a/pwmanager/libcrypt/zlib/zlib.pro b/pwmanager/libcrypt/zlib/zlib.pro
new file mode 100644
index 0000000..2841aaf
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/zlib.pro
@@ -0,0 +1,36 @@
1######################################################################
2# Automatically generated by qmake (1.07a) Tue Oct 19 14:18:17 2004
3######################################################################
4
5TEMPLATE = lib
6 CONFIG += staticlib
7INCLUDEPATH += .
8TARGET = zlib
9OBJECTS_DIR = obj/$(PLATFORM)
10MOC_DIR = moc/$(PLATFORM)
11DESTDIR= ../$(PLATFORM)
12# Input
13HEADERS += deflate.h \
14 infblock.h \
15 infcodes.h \
16 inffast.h \
17 inffixed.h \
18 inftrees.h \
19 infutil.h \
20 trees.h \
21 zconf.h \
22 zlib.h \
23 zutil.h
24SOURCES += adler32.c \
25 compress.c \
26 crc32.c \
27 deflate.c \
28 infblock.c \
29 infcodes.c \
30 inffast.c \
31 inflate.c \
32 inftrees.c \
33 infutil.c \
34 trees.c \
35 uncompr.c \
36 zutil.c
diff --git a/pwmanager/libcrypt/zlib/zutil.c b/pwmanager/libcrypt/zlib/zutil.c
new file mode 100644
index 0000000..dfc38ec
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/zutil.c
@@ -0,0 +1,225 @@
1/* zutil.c -- target dependent utility functions for the compression library
2 * Copyright (C) 1995-2002 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* @(#) $Id$ */
7
8#include "zutil.h"
9
10struct internal_state {int dummy;}; /* for buggy compilers */
11
12#ifndef STDC
13extern void exit OF((int));
14#endif
15
16const char *z_errmsg[10] = {
17"need dictionary", /* Z_NEED_DICT 2 */
18"stream end", /* Z_STREAM_END 1 */
19"", /* Z_OK 0 */
20"file error", /* Z_ERRNO (-1) */
21"stream error", /* Z_STREAM_ERROR (-2) */
22"data error", /* Z_DATA_ERROR (-3) */
23"insufficient memory", /* Z_MEM_ERROR (-4) */
24"buffer error", /* Z_BUF_ERROR (-5) */
25"incompatible version",/* Z_VERSION_ERROR (-6) */
26""};
27
28
29const char * ZEXPORT zlibVersion()
30{
31 return ZLIB_VERSION;
32}
33
34#ifdef DEBUG
35
36# ifndef verbose
37# define verbose 0
38# endif
39int z_verbose = verbose;
40
41void z_error (m)
42 char *m;
43{
44 fprintf(stderr, "%s\n", m);
45 exit(1);
46}
47#endif
48
49/* exported to allow conversion of error code to string for compress() and
50 * uncompress()
51 */
52const char * ZEXPORT zError(err)
53 int err;
54{
55 return ERR_MSG(err);
56}
57
58
59#ifndef HAVE_MEMCPY
60
61void zmemcpy(dest, source, len)
62 Bytef* dest;
63 const Bytef* source;
64 uInt len;
65{
66 if (len == 0) return;
67 do {
68 *dest++ = *source++; /* ??? to be unrolled */
69 } while (--len != 0);
70}
71
72int zmemcmp(s1, s2, len)
73 const Bytef* s1;
74 const Bytef* s2;
75 uInt len;
76{
77 uInt j;
78
79 for (j = 0; j < len; j++) {
80 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
81 }
82 return 0;
83}
84
85void zmemzero(dest, len)
86 Bytef* dest;
87 uInt len;
88{
89 if (len == 0) return;
90 do {
91 *dest++ = 0; /* ??? to be unrolled */
92 } while (--len != 0);
93}
94#endif
95
96#ifdef __TURBOC__
97#if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
98/* Small and medium model in Turbo C are for now limited to near allocation
99 * with reduced MAX_WBITS and MAX_MEM_LEVEL
100 */
101# define MY_ZCALLOC
102
103/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
104 * and farmalloc(64K) returns a pointer with an offset of 8, so we
105 * must fix the pointer. Warning: the pointer must be put back to its
106 * original form in order to free it, use zcfree().
107 */
108
109#define MAX_PTR 10
110/* 10*64K = 640K */
111
112local int next_ptr = 0;
113
114typedef struct ptr_table_s {
115 voidpf org_ptr;
116 voidpf new_ptr;
117} ptr_table;
118
119local ptr_table table[MAX_PTR];
120/* This table is used to remember the original form of pointers
121 * to large buffers (64K). Such pointers are normalized with a zero offset.
122 * Since MSDOS is not a preemptive multitasking OS, this table is not
123 * protected from concurrent access. This hack doesn't work anyway on
124 * a protected system like OS/2. Use Microsoft C instead.
125 */
126
127voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
128{
129 voidpf buf = opaque; /* just to make some compilers happy */
130 ulg bsize = (ulg)items*size;
131
132 /* If we allocate less than 65520 bytes, we assume that farmalloc
133 * will return a usable pointer which doesn't have to be normalized.
134 */
135 if (bsize < 65520L) {
136 buf = farmalloc(bsize);
137 if (*(ush*)&buf != 0) return buf;
138 } else {
139 buf = farmalloc(bsize + 16L);
140 }
141 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
142 table[next_ptr].org_ptr = buf;
143
144 /* Normalize the pointer to seg:0 */
145 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
146 *(ush*)&buf = 0;
147 table[next_ptr++].new_ptr = buf;
148 return buf;
149}
150
151void zcfree (voidpf opaque, voidpf ptr)
152{
153 int n;
154 if (*(ush*)&ptr != 0) { /* object < 64K */
155 farfree(ptr);
156 return;
157 }
158 /* Find the original pointer */
159 for (n = 0; n < next_ptr; n++) {
160 if (ptr != table[n].new_ptr) continue;
161
162 farfree(table[n].org_ptr);
163 while (++n < next_ptr) {
164 table[n-1] = table[n];
165 }
166 next_ptr--;
167 return;
168 }
169 ptr = opaque; /* just to make some compilers happy */
170 Assert(0, "zcfree: ptr not found");
171}
172#endif
173#endif /* __TURBOC__ */
174
175
176#if defined(M_I86) && !defined(__32BIT__)
177/* Microsoft C in 16-bit mode */
178
179# define MY_ZCALLOC
180
181#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
182# define _halloc halloc
183# define _hfree hfree
184#endif
185
186voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
187{
188 if (opaque) opaque = 0; /* to make compiler happy */
189 return _halloc((long)items, size);
190}
191
192void zcfree (voidpf opaque, voidpf ptr)
193{
194 if (opaque) opaque = 0; /* to make compiler happy */
195 _hfree(ptr);
196}
197
198#endif /* MSC */
199
200
201#ifndef MY_ZCALLOC /* Any system without a special alloc function */
202
203#ifndef STDC
204extern voidp calloc OF((uInt items, uInt size));
205extern void free OF((voidpf ptr));
206#endif
207
208voidpf zcalloc (opaque, items, size)
209 voidpf opaque;
210 unsigned items;
211 unsigned size;
212{
213 if (opaque) items += size - size; /* make compiler happy */
214 return (voidpf)calloc(items, size);
215}
216
217void zcfree (opaque, ptr)
218 voidpf opaque;
219 voidpf ptr;
220{
221 free(ptr);
222 if (opaque) return; /* make compiler happy */
223}
224
225#endif /* MY_ZCALLOC */
diff --git a/pwmanager/libcrypt/zlib/zutil.h b/pwmanager/libcrypt/zlib/zutil.h
new file mode 100644
index 0000000..718ebc1
--- a/dev/null
+++ b/pwmanager/libcrypt/zlib/zutil.h
@@ -0,0 +1,220 @@
1/* zutil.h -- internal interface and configuration of the compression library
2 * Copyright (C) 1995-2002 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* WARNING: this file should *not* be used by applications. It is
7 part of the implementation of the compression library and is
8 subject to change. Applications should only use zlib.h.
9 */
10
11/* @(#) $Id$ */
12
13#ifndef _Z_UTIL_H
14#define _Z_UTIL_H
15
16#include "zlib.h"
17
18#ifdef STDC
19# include <stddef.h>
20# include <string.h>
21# include <stdlib.h>
22#endif
23#ifdef NO_ERRNO_H
24 extern int errno;
25#else
26# include <errno.h>
27#endif
28
29#ifndef local
30# define local static
31#endif
32/* compile with -Dlocal if your debugger can't find static symbols */
33
34typedef unsigned char uch;
35typedef uch FAR uchf;
36typedef unsigned short ush;
37typedef ush FAR ushf;
38typedef unsigned long ulg;
39
40extern const char *z_errmsg[10]; /* indexed by 2-zlib_error */
41/* (size given to avoid silly warnings with Visual C++) */
42
43#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
44
45#define ERR_RETURN(strm,err) \
46 return (strm->msg = (char*)ERR_MSG(err), (err))
47/* To be used only when the state is known to be valid */
48
49 /* common constants */
50
51#ifndef DEF_WBITS
52# define DEF_WBITS MAX_WBITS
53#endif
54/* default windowBits for decompression. MAX_WBITS is for compression only */
55
56#if MAX_MEM_LEVEL >= 8
57# define DEF_MEM_LEVEL 8
58#else
59# define DEF_MEM_LEVEL MAX_MEM_LEVEL
60#endif
61/* default memLevel */
62
63#define STORED_BLOCK 0
64#define STATIC_TREES 1
65#define DYN_TREES 2
66/* The three kinds of block type */
67
68#define MIN_MATCH 3
69#define MAX_MATCH 258
70/* The minimum and maximum match lengths */
71
72#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
73
74 /* target dependencies */
75
76#ifdef MSDOS
77# define OS_CODE 0x00
78# if defined(__TURBOC__) || defined(__BORLANDC__)
79# if(__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
80 /* Allow compilation with ANSI keywords only enabled */
81 void _Cdecl farfree( void *block );
82 void *_Cdecl farmalloc( unsigned long nbytes );
83# else
84# include <alloc.h>
85# endif
86# else /* MSC or DJGPP */
87# include <malloc.h>
88# endif
89#endif
90
91#ifdef OS2
92# define OS_CODE 0x06
93#endif
94
95#ifdef WIN32 /* Window 95 & Windows NT */
96# define OS_CODE 0x0b
97#endif
98
99#if defined(VAXC) || defined(VMS)
100# define OS_CODE 0x02
101# define F_OPEN(name, mode) \
102 fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
103#endif
104
105#ifdef AMIGA
106# define OS_CODE 0x01
107#endif
108
109#if defined(ATARI) || defined(atarist)
110# define OS_CODE 0x05
111#endif
112
113#if defined(MACOS) || defined(TARGET_OS_MAC)
114# define OS_CODE 0x07
115# if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
116# include <unix.h> /* for fdopen */
117# else
118# ifndef fdopen
119# define fdopen(fd,mode) NULL /* No fdopen() */
120# endif
121# endif
122#endif
123
124#ifdef __50SERIES /* Prime/PRIMOS */
125# define OS_CODE 0x0F
126#endif
127
128#ifdef TOPS20
129# define OS_CODE 0x0a
130#endif
131
132#if defined(_BEOS_) || defined(RISCOS)
133# define fdopen(fd,mode) NULL /* No fdopen() */
134#endif
135
136#if (defined(_MSC_VER) && (_MSC_VER > 600))
137# define fdopen(fd,type) _fdopen(fd,type)
138#endif
139
140
141 /* Common defaults */
142
143#ifndef OS_CODE
144# define OS_CODE 0x03 /* assume Unix */
145#endif
146
147#ifndef F_OPEN
148# define F_OPEN(name, mode) fopen((name), (mode))
149#endif
150
151 /* functions */
152
153#ifdef HAVE_STRERROR
154 extern char *strerror OF((int));
155# define zstrerror(errnum) strerror(errnum)
156#else
157# define zstrerror(errnum) ""
158#endif
159
160#if defined(pyr)
161# define NO_MEMCPY
162#endif
163#if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
164 /* Use our own functions for small and medium model with MSC <= 5.0.
165 * You may have to use the same strategy for Borland C (untested).
166 * The __SC__ check is for Symantec.
167 */
168# define NO_MEMCPY
169#endif
170#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
171# define HAVE_MEMCPY
172#endif
173#ifdef HAVE_MEMCPY
174# ifdef SMALL_MEDIUM /* MSDOS small or medium model */
175# define zmemcpy _fmemcpy
176# define zmemcmp _fmemcmp
177# define zmemzero(dest, len) _fmemset(dest, 0, len)
178# else
179# define zmemcpy memcpy
180# define zmemcmp memcmp
181# define zmemzero(dest, len) memset(dest, 0, len)
182# endif
183#else
184 extern void zmemcpy OF((Bytef* dest, const Bytef* source, uInt len));
185 extern int zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len));
186 extern void zmemzero OF((Bytef* dest, uInt len));
187#endif
188
189/* Diagnostic functions */
190#ifdef DEBUG
191# include <stdio.h>
192 extern int z_verbose;
193 extern void z_error OF((char *m));
194# define Assert(cond,msg) {if(!(cond)) z_error(msg);}
195# define Trace(x) {if (z_verbose>=0) fprintf x ;}
196# define Tracev(x) {if (z_verbose>0) fprintf x ;}
197# define Tracevv(x) {if (z_verbose>1) fprintf x ;}
198# define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
199# define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
200#else
201# define Assert(cond,msg)
202# define Trace(x)
203# define Tracev(x)
204# define Tracevv(x)
205# define Tracec(c,x)
206# define Tracecv(c,x)
207#endif
208
209
210typedef uLong (ZEXPORT *check_func) OF((uLong check, const Bytef *buf,
211 uInt len));
212voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
213void zcfree OF((voidpf opaque, voidpf ptr));
214
215#define ZALLOC(strm, items, size) \
216 (*((strm)->zalloc))((strm)->opaque, (items), (size))
217#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
218#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
219
220#endif /* _Z_UTIL_H */