Jussi Kivilinna [Sat, 26 Oct 2013 12:00:48 +0000 (15:00 +0300)]
Add new benchmarking utility, bench-slope
* tests/Makefile.am (TESTS): Add 'bench-slope'.
* tests/bench-slope.c: New.
--
Bench-slope is new benchmarking tool for libgcrypt for obtaining overheadless
cycles/byte speed of cipher and hash algorithms. Tool measures the time each
operation (hash/encrypt/decrypt/authentication) takes for different buffer
sizes of from ~0kB to ~4kB and calculates the slope for these data points.
The default output is then given as nanosecs/byte and mebibytes/sec. If user
provides the speed of used CPU, tool also outputs cycles/byte result (CPU-Ghz *
ns/B = c/B).
Output without CPU speed (with ARM Cortex-A8):
$ tests/bench-slope hash
Hash:
| nanosecs/byte mebibytes/sec cycles/byte
MD5 | 7.35 ns/B 129.7 MiB/s - c/B
SHA1 | 12.30 ns/B 77.53 MiB/s - c/B
RIPEMD160 | 15.96 ns/B 59.77 MiB/s - c/B
TIGER192 | 55.55 ns/B 17.17 MiB/s - c/B
SHA256 | 24.38 ns/B 39.12 MiB/s - c/B
SHA384 | 34.24 ns/B 27.86 MiB/s - c/B
SHA512 | 34.19 ns/B 27.90 MiB/s - c/B
SHA224 | 24.38 ns/B 39.12 MiB/s - c/B
MD4 | 5.68 ns/B 168.0 MiB/s - c/B
CRC32 | 9.26 ns/B 103.0 MiB/s - c/B
CRC32RFC1510 | 9.20 ns/B 103.6 MiB/s - c/B
CRC24RFC2440 | 87.31 ns/B 10.92 MiB/s - c/B
WHIRLPOOL | 253.3 ns/B 3.77 MiB/s - c/B
TIGER | 55.55 ns/B 17.17 MiB/s - c/B
TIGER2 | 55.55 ns/B 17.17 MiB/s - c/B
GOSTR3411_94 | 212.0 ns/B 4.50 MiB/s - c/B
STRIBOG256 | 630.1 ns/B 1.51 MiB/s - c/B
STRIBOG512 | 630.1 ns/B 1.51 MiB/s - c/B
=
With CPU speed (with Intel i5-4570, 3.2Ghz when turbo-boost disabled):
$ tests/bench-slope --cpu-mhz 3201 cipher arcfour blowfish aes
Cipher:
ARCFOUR | nanosecs/byte mebibytes/sec cycles/byte
STREAM enc | 2.43 ns/B 392.1 MiB/s 7.79 c/B
STREAM dec | 2.44 ns/B 390.2 MiB/s 7.82 c/B
=
BLOWFISH | nanosecs/byte mebibytes/sec cycles/byte
ECB enc | 7.62 ns/B 125.2 MiB/s 24.38 c/B
ECB dec | 7.63 ns/B 125.0 MiB/s 24.43 c/B
CBC enc | 9.18 ns/B 103.9 MiB/s 29.38 c/B
CBC dec | 2.60 ns/B 366.2 MiB/s 8.34 c/B
CFB enc | 9.17 ns/B 104.0 MiB/s 29.35 c/B
CFB dec | 2.66 ns/B 358.1 MiB/s 8.53 c/B
OFB enc | 8.97 ns/B 106.3 MiB/s 28.72 c/B
OFB dec | 8.97 ns/B 106.3 MiB/s 28.71 c/B
CTR enc | 2.60 ns/B 366.5 MiB/s 8.33 c/B
CTR dec | 2.60 ns/B 367.1 MiB/s 8.32 c/B
=
AES | nanosecs/byte mebibytes/sec cycles/byte
ECB enc | 0.439 ns/B 2173.0 MiB/s 1.40 c/B
ECB dec | 0.489 ns/B 1949.5 MiB/s 1.57 c/B
CBC enc | 1.64 ns/B 580.8 MiB/s 5.26 c/B
CBC dec | 0.219 ns/B 4357.6 MiB/s 0.701 c/B
CFB enc | 1.53 ns/B 623.6 MiB/s 4.90 c/B
CFB dec | 0.219 ns/B 4350.5 MiB/s 0.702 c/B
OFB enc | 1.51 ns/B 629.9 MiB/s 4.85 c/B
OFB dec | 1.51 ns/B 629.9 MiB/s 4.85 c/B
CTR enc | 0.288 ns/B 3308.5 MiB/s 0.923 c/B
CTR dec | 0.288 ns/B 3316.9 MiB/s 0.920 c/B
CCM enc | 1.93 ns/B 493.8 MiB/s 6.18 c/B
CCM dec | 1.93 ns/B 494.0 MiB/s 6.18 c/B
CCM auth | 1.64 ns/B 580.1 MiB/s 5.26 c/B
=
Note: It's highly recommented to disable turbo-boost and dynamic CPU frequency
features when making these kind of measurements to reduce variance.
Note: The results are maximum performance for each operation; the actual speed
in application depends on various matters, such as: used buffer sizes, cache
usage, etc.
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Jussi Kivilinna [Mon, 28 Oct 2013 06:54:59 +0000 (08:54 +0200)]
Change .global to .globl in assembly files
* cipher/blowfish-arm.S: Change '.global' to '.globl'.
* cipher/camellia-aesni-avx-amd64.S: Ditto.
* cipher/camellia-aesni-avx2-amd64.S: Ditto.
* cipher/camellia-arm.S: Ditto.
* cipher/cast5-amd64.S: Ditto.
* cipher/rijndael-amd64.S: Ditto.
* cipher/rijndael-arm.S: Ditto.
* cipher/serpent-avx2-amd64.S: Ditto.
* cipher/serpent-sse2-amd64.S: Ditto.
* cipher/twofish-amd64.S: Ditto.
* cipher/twofish-arm.S: Ditto.
--
The .global keyword is used only in newer versions of GAS, so change these to
older .globl for better portability.
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Jussi Kivilinna [Sat, 26 Oct 2013 11:51:44 +0000 (14:51 +0300)]
Deduplicate code for ECB encryption and decryption
* cipher/cipher.c (do_ecb_crypt): New, based on old 'do_ecb_encrypt'.
(do_ecb_encrypt): Use 'do_ecb_crypt', pass encryption function.
(do_ecb_decrypt): Use 'do_ecb_crypt', pass decryption function.
--
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Dmitry Eremin-Solenikov [Sat, 26 Oct 2013 11:51:44 +0000 (14:51 +0300)]
Drop _gcry_cipher_ofb_decrypt as it duplicates _gcry_cipher_ofb_encrypt
* cipher/cipher.c (cipher_decrypt): Use _gcry_cipher_ofb_encrypt for OFB
decryption.
* cipher/cipher-internal.h: Remove _gcry_cipher_ofb_decrypt declaration.
* cipher/cipher-ofb.c (_gcry_cipher_ofb_decrypt): Remove.
(_gcry_cipher_ofb_encrypt): remove copying of IV to lastiv, it's
unused there.
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
Werner Koch [Fri, 25 Oct 2013 09:43:31 +0000 (11:43 +0200)]
tests: Add tests for mpi_cmp.
* tests/mpitests.c (die): Modernize.
(fail): New.
(test_opaque, test_add, test_sub, test_mul): Use gcry_log_xx
(main): Return error count.
(test_cmp): New.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Thu, 24 Oct 2013 11:59:29 +0000 (13:59 +0200)]
ecc: Change algorithm for
Ed25519 x recovery.
* cipher/ecc-eddsa.c (scanval): Add as temporary hack.
(_gcry_ecc_eddsa_recover_x): Use the algorithm from page 15 of the
paper. Return an error code.
(_gcry_ecc_eddsa_decodepoint): Take care of the error code.
* mpi/mpi-mul.c (gcry_mpi_mulm): Use truncated division.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Wed, 23 Oct 2013 12:20:10 +0000 (14:20 +0200)]
ecc: Refactor _gcry_ecc_eddsa_decodepoint.
* cipher/ecc-eddsa.c (_gcry_ecc_eddsa_decodepoint): Factor some code
out to ..
(_gcry_ecc_eddsa_recover_x): new.
Signed-off-by: Werner Koch <wk@gnupg.org>
Jussi Kivilinna [Thu, 24 Oct 2013 09:20:55 +0000 (12:20 +0300)]
ecc-gost: Add missing include
* ecc-gost.c: Include "pubkey-internal.h".
--
Patch fixes compiler warning:
ecc-gost.c: In function '_gcry_ecc_gost_sign':
ecc-gost.c:95:11: warning: implicit declaration of function '_gcry_dsa_gen_k' [-Wimplicit-function-declaration]
k = _gcry_dsa_gen_k (skey->E.n, GCRY_STRONG_RANDOM);
^
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Jussi Kivilinna [Wed, 23 Oct 2013 15:36:18 +0000 (18:36 +0300)]
Replace architecture specific fast_wipememory2 with generic
* src/g10lib.h (fast_wipememory2): Remove architecture specific
implementations and add generic implementation.
--
Reduce code size, adds support for other architectures and gcc appears to
generated better code without assembly parts.
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Jussi Kivilinna [Wed, 23 Oct 2013 15:36:18 +0000 (18:36 +0300)]
Improve the speed of the cipher mode code
* cipher/bufhelp.h (buf_cpy): New.
(buf_xor, buf_xor_2dst): If buffers unaligned, always jump to per-byte
processing.
(buf_xor_n_copy_2): New.
(buf_xor_n_copy): Use 'buf_xor_n_copy_2'.
* cipher/blowfish.c (_gcry_blowfish_cbc_dec): Avoid extra memory copy
and use new 'buf_xor_n_copy_2'.
* cipher/camellia-glue.c (_gcry_camellia_cbc_dec): Ditto.
* cipher/cast5.c (_gcry_cast_cbc_dec): Ditto.
* cipher/serpent.c (_gcry_serpent_cbc_dec): Ditto.
* cipher/twofish.c (_gcry_twofish_cbc_dec): Ditto.
* cipher/rijndael.c (_gcry_aes_cbc_dec): Ditto.
(do_encrypt, do_decrypt): Use 'buf_cpy' instead of 'memcpy'.
(_gcry_aes_cbc_enc): Avoid copying IV, use 'last_iv' pointer instead.
* cipher/cipher-cbc.c (_gcry_cipher_cbc_encrypt): Avoid copying IV,
update pointer to IV instead.
(_gcry_cipher_cbc_decrypt): Avoid extra memory copy and use new
'buf_xor_n_copy_2'.
(_gcry_cipher_cbc_encrypt, _gcry_cipher_cbc_decrypt): Avoid extra
accesses to c->spec, use 'buf_cpy' instead of memcpy.
* cipher/cipher-ccm.c (do_cbc_mac): Ditto.
* cipher/cipher-cfb.c (_gcry_cipher_cfb_encrypt)
(_gcry_cipher_cfb_decrypt): Ditto.
* cipher/cipher-ctr.c (_gcry_cipher_ctr_encrypt): Ditto.
* cipher/cipher-ofb.c (_gcry_cipher_ofb_encrypt)
(_gcry_cipher_ofb_decrypt): Ditto.
* cipher/cipher.c (do_ecb_encrypt, do_ecb_decrypt): Ditto.
--
Patch improves the speed of the generic block cipher mode code. Especially on
targets without faster unaligned memory accesses, the generic code was slower
than the algorithm specific bulk versions. With this patch, this issue should
be solved.
Tests on Cortex-A8; compiled for ARMv4, without unaligned-accesses:
Before:
ECB/Stream CBC CFB OFB CTR CCM
--------------- --------------- --------------- --------------- --------------- ---------------
SEED 490ms 500ms 560ms 580ms 530ms 540ms 560ms 560ms 550ms 540ms 1080ms 1080ms
TWOFISH 230ms 230ms 290ms 300ms 260ms 240ms 290ms 290ms 240ms 240ms 520ms 510ms
DES 720ms 720ms 800ms 860ms 770ms 770ms 810ms 820ms 770ms 780ms - -
CAST5 340ms 340ms 440ms 250ms 390ms 250ms 440ms 430ms 260ms 250ms - -
After:
ECB/Stream CBC CFB OFB CTR CCM
--------------- --------------- --------------- --------------- --------------- ---------------
SEED 500ms 490ms 520ms 520ms 530ms 520ms 530ms 540ms 500ms 520ms 1060ms 1070ms
TWOFISH 230ms 220ms 250ms 230ms 260ms 230ms 260ms 260ms 230ms 230ms 500ms 490ms
DES 720ms 720ms 750ms 760ms 740ms 750ms 770ms 770ms 760ms 760ms - -
CAST5 340ms 340ms 370ms 250ms 370ms 250ms 380ms 390ms 250ms 250ms - -
Tests on Cortex-A8; compiled for ARMv7-A, with unaligned-accesses:
Before:
ECB/Stream CBC CFB OFB CTR CCM
--------------- --------------- --------------- --------------- --------------- ---------------
SEED 430ms 440ms 480ms 530ms 470ms 460ms 490ms 480ms 470ms 460ms 930ms 940ms
TWOFISH 220ms 220ms 250ms 230ms 240ms 230ms 270ms 250ms 230ms 240ms 480ms 470ms
DES 550ms 540ms 620ms 690ms 570ms 540ms 630ms 650ms 590ms 580ms - -
CAST5 300ms 300ms 380ms 230ms 330ms 230ms 380ms 370ms 230ms 230ms - -
After:
ECB/Stream CBC CFB OFB CTR CCM
--------------- --------------- --------------- --------------- --------------- ---------------
SEED 430ms 430ms 460ms 450ms 460ms 450ms 470ms 470ms 460ms 470ms 900ms 930ms
TWOFISH 220ms 210ms 240ms 230ms 230ms 230ms 250ms 250ms 230ms 230ms 470ms 470ms
DES 540ms 540ms 580ms 570ms 570ms 570ms 560ms 620ms 580ms 570ms - -
CAST5 300ms 290ms 310ms 230ms 320ms 230ms 350ms 350ms 230ms 230ms - -
Tests on Intel Atom N160 (i386):
Before:
ECB/Stream CBC CFB OFB CTR CCM
--------------- --------------- --------------- --------------- --------------- ---------------
SEED 380ms 380ms 410ms 420ms 400ms 400ms 410ms 410ms 390ms 400ms 820ms 800ms
TWOFISH 340ms 340ms 370ms 350ms 360ms 340ms 370ms 370ms 330ms 340ms 710ms 700ms
DES 660ms 650ms 710ms 740ms 680ms 700ms 700ms 710ms 680ms 680ms - -
CAST5 340ms 340ms 380ms 330ms 360ms 330ms 390ms 390ms 320ms 330ms - -
After:
ECB/Stream CBC CFB OFB CTR CCM
--------------- --------------- --------------- --------------- --------------- ---------------
SEED 380ms 380ms 390ms 410ms 400ms 390ms 410ms 400ms 400ms 390ms 810ms 800ms
TWOFISH 330ms 340ms 350ms 360ms 350ms 340ms 380ms 370ms 340ms 360ms 700ms 710ms
DES 630ms 640ms 660ms 690ms 680ms 680ms 700ms 690ms 680ms 680ms - -
CAST5 340ms 330ms 350ms 330ms 370ms 340ms 380ms 390ms 330ms 330ms - -
Tests in Intel i5-4570 (x86-64):
Before:
ECB/Stream CBC CFB OFB CTR CCM
--------------- --------------- --------------- --------------- --------------- ---------------
SEED 560ms 560ms 600ms 590ms 600ms 570ms 570ms 570ms 580ms 590ms 1200ms 1180ms
TWOFISH 240ms 240ms 270ms 160ms 260ms 160ms 250ms 250ms 160ms 160ms 430ms 430ms
DES 570ms 570ms 640ms 590ms 630ms 580ms 600ms 600ms 610ms 620ms - -
CAST5 410ms 410ms 470ms 150ms 470ms 150ms 450ms 450ms 150ms 160ms - -
After:
ECB/Stream CBC CFB OFB CTR CCM
--------------- --------------- --------------- --------------- --------------- ---------------
SEED 560ms 560ms 590ms 570ms 580ms 570ms 570ms 570ms 590ms 590ms 1200ms 1200ms
TWOFISH 240ms 240ms 260ms 160ms 250ms 170ms 250ms 250ms 160ms 160ms 430ms 430ms
DES 570ms 570ms 620ms 580ms 630ms 570ms 600ms 590ms 620ms 620ms - -
CAST5 410ms 410ms 460ms 150ms 460ms 160ms 450ms 450ms 150ms 150ms - -
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Jussi Kivilinna [Wed, 23 Oct 2013 15:36:18 +0000 (18:36 +0300)]
bufhelp: enable unaligned memory accesses for AArch64 (64-bit ARM)
* cipher/bufhelp.h [__aarch64__] (BUFHELP_FAST_UNALIGNED_ACCESS): Set
macro on AArch64.
--
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Dmitry Eremin-Solenikov [Wed, 23 Oct 2013 15:36:10 +0000 (18:36 +0300)]
Enable assembler optimizations on earlier ARM cores
* cipher/blowfish-armv6.S => cipher/blowfish-arm.S: adapt to pre-armv6 CPUs.
* cipher/blowfish.c: enable assembly on armv4/armv5 little-endian CPUs.
* cipher/camellia-armv6.S => cipher/camellia-arm.S: adapt to pre-armv6 CPUs.
* cipher/camellia.c, cipher-camellia-glue.c: enable assembly on armv4/armv5
little-endian CPUs.
* cipher/cast5-armv6.S => cipher/cast5-arm.S: adapt to pre-armv6 CPUs.
* cipher/cast5.c: enable assembly on armv4/armv5 little-endian CPUs.
* cipher/rijndael-armv6.S => cipher/rijndael-arm.S: adapt to pre-armv6 CPUs.
* cipher/rijndael.c: enable assembly on armv4/armv5 little-endian CPUs.
* cipher/twofish-armv6.S => cipher/twofish-arm.S: adapt to pre-armv6 CPUs.
* cipher/twofish.c: enable assembly on armv4/armv5 little-endian CPUs.
--
Our ARMv6 assembly optimized code can be easily adapted to earlier CPUs.
The only incompatible place is rev instruction used to do byte swapping.
Replace it on <= ARMv6 with a series of 4 instructions.
Compare:
ECB/Stream CBC CFB OFB CTR
--------------- --------------- --------------- --------------- ---------------
AES 620ms 610ms 650ms 680ms 620ms 630ms 660ms 660ms 630ms 630ms
CAMELLIA128 720ms 720ms 780ms 790ms 770ms 760ms 780ms 780ms 770ms 760ms
CAMELLIA256 910ms 910ms 970ms 970ms 960ms 950ms 970ms 970ms 960ms 950ms
CAST5 820ms 820ms 930ms 920ms 890ms 860ms 930ms 920ms 880ms 890ms
BLOWFISH 550ms 560ms 650ms 660ms 630ms 600ms 660ms 650ms 610ms 620ms
ECB/Stream CBC CFB OFB CTR
--------------- --------------- --------------- --------------- ---------------
AES 130ms 140ms 180ms 200ms 160ms 170ms 190ms 200ms 170ms 170ms
CAMELLIA128 150ms 160ms 210ms 220ms 200ms 190ms 210ms 220ms 190ms 190ms
CAMELLIA256 180ms 180ms 260ms 240ms 240ms 230ms 250ms 250ms 230ms 230ms
CAST5 170ms 160ms 270ms 120ms 240ms 130ms 260ms 270ms 130ms 120ms
BLOWFISH 160ms 150ms 260ms 110ms 230ms 120ms 250ms 260ms 110ms 120ms
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
[ jk: in camellia.h and twofish.c, USE_ARMV6_ASM => USE_ARM_ASM ]
[ jk: fix blowfish-arm.S when __ARM_FEATURE_UNALIGNED defined ]
[ jk: in twofish.S remove defined(HAVE_ARM_ARCH_V6) ]
[ jk: ARMv6 => ARM in comments ]
Dmitry Eremin-Solenikov [Wed, 23 Oct 2013 15:36:10 +0000 (18:36 +0300)]
mpi: enable assembler on all arm architectures
* mpi/config.links: remove check for arm >= v6
* mpi/armv6 => mpi/arm: rename directory to reflect that is is generic
enough
--
MPI ARM assembly do not depend on CPU being armv6. Verified on PXA255:
Before:
Algorithm generate 100*sign 100*verify
------------------------------------------------
RSA 1024 bit 3990ms 57980ms 1680ms
RSA 2048 bit 59620ms 389430ms 5690ms
RSA 3072 bit 535850ms 1223200ms 12000ms
RSA 4096 bit 449350ms 2707370ms 20050ms
After:
Algorithm generate 100*sign 100*verify
------------------------------------------------
RSA 1024 bit 2190ms 13730ms 320ms
RSA 2048 bit 12750ms 67640ms 810ms
RSA 3072 bit 110520ms 166100ms 1350ms
RSA 4096 bit 100870ms 357560ms 2170ms
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
[ jk: ARMv6 => ARM in header comments ]
Dmitry Eremin-Solenikov [Wed, 23 Oct 2013 15:36:10 +0000 (18:36 +0300)]
Correct ASM assembly test in configure.ac
* configure.ac: correct HAVE_COMPATIBLE_GCC_ARM_PLATFORM_AS test to
require neither ARMv6, nor thumb mode. Our assembly code works
perfectly even on ARMv4 now.
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
Werner Koch [Wed, 23 Oct 2013 12:08:29 +0000 (14:08 +0200)]
ecc: Refactor ecc.c
* cipher/ecc-ecdsa.c, cipher/ecc-eddsa.c, cipher/ecc-gost.c: New.
* cipher/Makefile.am (EXTRA_libcipher_la_SOURCES): Add new files.
* configure.ac (GCRYPT_PUBKEY_CIPHERS): Add new files.
* cipher/ecc.c (point_init, point_free): Move to ecc-common.h.
(sign_ecdsa): Move to ecc-ecdsa.c as _gcry_ecc_ecdsa_sign.
(verify_ecdsa): Move to ecc-ecdsa.c as _gcry_ecc_ecdsa_verify.
(sign_gost): Move to ecc-gots.c as _gcry_ecc_gost_sign.
(verify_gost): Move to ecc-gost.c as _gcry_ecc_gost_verify.
(sign_eddsa): Move to ecc-eddsa.c as _gcry_ecc_eddsa_sign.
(verify_eddsa): Move to ecc-eddsa.c as _gcry_ecc_eddsa_verify.
(eddsa_generate_key): Move to ecc-eddsa.c as _gcry_ecc_eddsa_genkey.
(reverse_buffer): Move to ecc-eddsa.c.
(eddsa_encodempi, eddsa_encode_x_y): Ditto.
(_gcry_ecc_eddsa_encodepoint, _gcry_ecc_eddsa_decodepoint): Ditto.
--
This change should make it easier to add new ECC algorithms.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Wed, 23 Oct 2013 09:41:37 +0000 (11:41 +0200)]
mpi: Fix scanning of negative SSH formats and add more tests.
* mpi/mpicoder.c (gcry_mpi_scan): Fix sign setting for SSH format.
* tests/t-convert.c (negative_zero): Test all formats.
(check_formats): Add tests for PGP and scan tests for SSH and USG.
* src/gcrypt.h.in (mpi_is_neg): Fix macro.
* mpi/mpi-scan.c (_gcry_mpi_getbyte, _gcry_mpi_putbyte): Comment out
these unused functions.
Signed-off-by: Werner Koch <wk@gnupg.org>
Jussi Kivilinna [Tue, 22 Oct 2013 14:07:53 +0000 (17:07 +0300)]
twofish: add ARMv6 assembly implementation
* cipher/Makefile.am: Add 'twofish-armv6.S'.
* cipher/twofish-armv6.S: New.
* cipher/twofish.c (USE_ARMV6_ASM): New macro.
[USE_ARMV6_ASM] (_gcry_twofish_armv6_encrypt_block)
(_gcry_twofish_armv6_decrypt_block): New prototypes.
[USE_AMDV6_ASM] (twofish_encrypt, twofish_decrypt): Add.
[USE_AMD64_ASM] (do_twofish_encrypt, do_twofish_decrypt): Remove.
(_gcry_twofish_ctr_enc, _gcry_twofish_cfb_dec): Use 'twofish_encrypt'
instead of 'do_twofish_encrypt'.
(_gcry_twofish_cbc_dec): Use 'twofish_decrypt' instead of
'do_twofish_decrypt'.
* configure.ac [arm]: Add 'twofish-armv6.lo'.
--
Add optimized ARMv6 assembly implementation for Twofish. Implementation is tuned
for Cortex-A8. Unaligned access handling is done in assembly part.
For now, only enable this on little-endian systems as big-endian correctness
have not been tested yet.
Old (gcc-4.8) vs new (twofish-asm), Cortex-A8 (on armhf):
ECB/Stream CBC CFB OFB CTR CCM
--------------- --------------- --------------- --------------- --------------- ---------------
TWOFISH 1.23x 1.25x 1.16x 1.26x 1.16x 1.30x 1.18x 1.17x 1.23x 1.23x 1.22x 1.22x
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Jussi Kivilinna [Tue, 22 Oct 2013 14:07:53 +0000 (17:07 +0300)]
mpi: allow building with clang on ARM
* mpi/longlong.h [__arm__] (add_ssaaaa, sub_ddmmss, umul_ppmm)
(count_leading_zeros): Do not cast assembly output arguments.
[__arm__] (umul_ppmm): Remove the extra '%' ahead of assembly comment.
[_ARM_ARCH >= 4] (umul_ppmm): Use correct inputs and outputs instead of
registers.
--
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Jussi Kivilinna [Tue, 22 Oct 2013 14:07:53 +0000 (17:07 +0300)]
serpent-amd64: do not use GAS macros
* cipher/serpent-avx2-amd64.S: Remove use of GAS macros.
* cipher/serpent-sse2-amd64.S: Ditto.
* configure.ac [HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS]: Do not check
for GAS macros.
--
This way we have better portability; for example, when compiling with clang
on x86-64, the assembly implementations are now enabled and working.
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Jussi Kivilinna [Tue, 22 Oct 2013 14:07:53 +0000 (17:07 +0300)]
Add Counter with CBC-MAC mode (CCM)
* cipher/Makefile.am: Add 'cipher-ccm.c'.
* cipher/cipher-ccm.c: New.
* cipher/cipher-internal.h (gcry_cipher_handle): Add 'u_mode'.
(_gcry_cipher_ccm_encrypt, _gcry_cipher_ccm_decrypt)
(_gcry_cipher_ccm_set_nonce, _gcry_cipher_ccm_authenticate)
(_gcry_cipher_ccm_get_tag, _gcry_cipher_ccm_check_tag)
(_gcry_cipher_ccm_set_lengths): New prototypes.
* cipher/cipher.c (gcry_cipher_open, cipher_encrypt, cipher_decrypt)
(_gcry_cipher_setiv, _gcry_cipher_authenticate, _gcry_cipher_gettag)
(_gcry_cipher_checktag, gry_cipher_ctl): Add handling for CCM mode.
* doc/gcrypt.texi: Add documentation for GCRY_CIPHER_MODE_CCM.
* src/gcrypt.h.in (gcry_cipher_modes): Add 'GCRY_CIPHER_MODE_CCM'.
(gcry_ctl_cmds): Add 'GCRYCTL_SET_CCM_LENGTHS'.
(GCRY_CCM_BLOCK_LEN): New.
* tests/basic.c (check_ccm_cipher): New.
(check_cipher_modes): Call 'check_ccm_cipher'.
* tests/benchmark.c (ccm_aead_init): New.
(cipher_bench): Add handling for AEAD modes and add CCM benchmarking.
--
Patch adds CCM (Counter with CBC-MAC) mode as defined in RFC 3610 and NIST
Special Publication 800-38C.
Example for encrypting message (split in two buffers; buf1, buf2) and
authenticating additional non-encrypted data (split in two buffers; aadbuf1,
aadbuf2) with authentication tag length of eigth bytes:
size_t params[3];
taglen = 8;
gcry_cipher_setkey(h, key, len(key));
gcry_cipher_setiv(h, nonce, len(nonce));
params[0] = len(buf1) + len(buf2); /* 0: enclen */
params[1] = len(aadbuf1) + len(aadbuf2); /* 1: aadlen */
params[2] = taglen; /* 2: authtaglen */
gcry_cipher_ctl(h, GCRYCTL_SET_CCM_LENGTHS, params, sizeof(size_t) * 3);
gcry_cipher_authenticate(h, aadbuf1, len(aadbuf1));
gcry_cipher_authenticate(h, aadbuf2, len(aadbuf2));
gcry_cipher_encrypt(h, buf1, len(buf1), buf1, len(buf1));
gcry_cipher_encrypt(h, buf2, len(buf2), buf2, len(buf2));
gcry_cipher_gettag(h, tag, taglen);
Example for decrypting above message and checking authentication tag:
size_t params[3];
taglen = 8;
gcry_cipher_setkey(h, key, len(key));
gcry_cipher_setiv(h, nonce, len(nonce));
params[0] = len(buf1) + len(buf2); /* 0: enclen */
params[1] = len(aadbuf1) + len(aadbuf2); /* 1: aadlen */
params[2] = taglen; /* 2: authtaglen */
gcry_cipher_ctl(h, GCRYCTL_SET_CCM_LENGTHS, params, sizeof(size_t) * 3);
gcry_cipher_authenticate(h, aadbuf1, len(aadbuf1));
gcry_cipher_authenticate(h, aadbuf2, len(aadbuf2));
gcry_cipher_decrypt(h, buf1, len(buf1), buf1, len(buf1));
gcry_cipher_decrypt(h, buf2, len(buf2), buf2, len(buf2));
err = gcry_cipher_checktag(h, tag, taglen);
if (gpg_err_code (err) == GPG_ERR_CHECKSUM)
{ /* Authentication failed. */ }
else if (err == 0)
{ /* Authentication ok. */ }
Example for encrypting message without additional authenticated data:
size_t params[3];
taglen = 10;
gcry_cipher_setkey(h, key, len(key));
gcry_cipher_setiv(h, nonce, len(nonce));
params[0] = len(buf1); /* 0: enclen */
params[1] = 0; /* 1: aadlen */
params[2] = taglen; /* 2: authtaglen */
gcry_cipher_ctl(h, GCRYCTL_SET_CCM_LENGTHS, params, sizeof(size_t) * 3);
gcry_cipher_encrypt(h, buf1, len(buf1), buf1, len(buf1));
gcry_cipher_gettag(h, tag, taglen);
To reset CCM state for cipher handle, one can either set new nonce or use
'gcry_cipher_reset'.
This implementation reuses existing CTR mode code for encryption/decryption
and is there for able to process multiple buffers that are not multiple of
blocksize. AAD data maybe also be passed into gcry_cipher_authenticate
in non-blocksize chunks.
[v4]: GCRYCTL_SET_CCM_PARAMS => GCRY_SET_CCM_LENGTHS
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Jussi Kivilinna [Tue, 22 Oct 2013 14:07:53 +0000 (17:07 +0300)]
Add API to support AEAD cipher modes
* cipher/cipher.c (_gcry_cipher_authenticate, _gcry_cipher_checktag)
(_gcry_cipher_gettag): New.
* doc/gcrypt.texi: Add documentation for new API functions.
* src/visibility.c (gcry_cipher_authenticate, gcry_cipher_checktag)
(gcry_cipher_gettag): New.
* src/gcrypt.h.in, src/visibility.h: add declarations of these
functions.
* src/libgcrypt.defs, src/libgcrypt.vers: export functions.
--
Authenticated Encryption with Associated Data (AEAD) cipher modes
provide authentication tag that can be used to authenticate message. At
the same time it allows one to specify additional (unencrypted data)
that will be authenticated together with the message. This class of
cipher modes requires additional API present in this commit.
This patch is based on original patch by Dmitry Eremin-Solenikov.
Changes in v2:
- Change gcry_cipher_tag to gcry_cipher_checktag and gcry_cipher_gettag
for giving tag (checktag) for decryption and reading tag (gettag) after
encryption.
- Change gcry_cipher_authenticate to gcry_cipher_setaad, since
additional parameters needed for some AEAD modes (in this case CCM,
which needs the length of encrypted data and tag for MAC
initialization).
- Add some documentation.
Changes in v3:
- Change gcry_cipher_setaad back to gcry_cipher_authenticate. Additional
parameters (encrypt_len, tag_len, aad_len) for CCM will be given
through GCRY_CTL_SET_CCM_LENGTHS.
Changes in v4:
- log_fatal => log_error
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
NIIBE Yutaka [Tue, 22 Oct 2013 10:47:11 +0000 (12:47 +0200)]
ecc: Correct compliant key generation for Edwards curves.
* cipher/ecc.c: Add case for Edwards curves.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Thu, 17 Oct 2013 16:08:59 +0000 (18:08 +0200)]
tests: Add test options to keygen.
* tests/keygen.c (usage): New.
(main): Print usage info. Allow running just one algo.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Thu, 17 Oct 2013 16:07:28 +0000 (18:07 +0200)]
mpi: Do not clear the sign of the mpi_mod result.
* mpi/mpi-mod.c (_gcry_mpi_mod): Remove sign setting.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Thu, 17 Oct 2013 16:01:48 +0000 (18:01 +0200)]
ecc: Put the curve name again into the output of gcry_pk_genkey.
* cipher/ecc.c (ecc_generate): Use the correct var. Release
CURVE_FLAGS.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Thu, 17 Oct 2013 08:45:14 +0000 (10:45 +0200)]
ecc: Support Weierstrass curves in gcry_mpi_ec_curve_point.
* mpi/ec.c (_gcry_mpi_ec_curve_point): Support MPI_EC_WEIERSTRASS.
Jussi Kivilinna [Wed, 16 Oct 2013 18:23:15 +0000 (21:23 +0300)]
arcfour: more optimized version for non-i386 architectures
* cipher/arcfour.c (ARCFOUR_context): Reorder members.
(do_encrypt_stream) [!__i386__]: Faster implementation for non-i386.
(do_arcfour_setkey): Avoid modulo operations.
--
Patch adds faster arcfour implementation for non-i386 architectures. New code
is not activated on i386 as performance would regress. This is because i386
does not have enough registers hold new variables.
Speed up on Intel i5-4570 (x86_64): 1.56x
Speed up on ARM Cortex-A8: 1.18x
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Jussi Kivilinna [Wed, 16 Oct 2013 18:16:15 +0000 (21:16 +0300)]
Avoid void* pointer arithmetic
* tests/tsexp.c (check_extract_param): Cast void* pointers to char*
before doing arithmetics.
--
GCC was complaining:
tsexp.c: In function ‘check_extract_param’:
tsexp.c:938:44: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
tsexp.c:944:46: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
tsexp.c:955:44: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
tsexp.c:961:46: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Dmitry Eremin-Solenikov [Tue, 15 Oct 2013 19:56:44 +0000 (23:56 +0400)]
ecc: Add support for GOST R 34.10-2001/-2012 signatures
* src/cipher.h: define PUBKEY_FLAG_GOST
* cipher/ecc-curves.c: Add GOST2001-test and GOST2012-test curves
defined in standards. Typical applications would use either those
curves, or curves defined in RFC 4357 (will be added later).
* cipher/ecc.c (sign_gost, verify_gost): New.
(ecc_sign, ecc_verify): use sign_gost/verify_gost if PUBKEY_FLAG_GOST
is set.
(ecc_names): add "gost" for gost signatures.
* cipher/pubkey-util.c (_gcry_pk_util_parse_flaglist,
_gcry_pk_util_preparse_sigval): set PUBKEY_FLAG_GOST if gost flag
is present in s-exp.
* tests/benchmark.c (ecc_bench): also benchmark GOST signatures.
* tests/basic.c (check_pubkey): add two public keys from
GOST R 34.10-2012 standard.
(check_pubkey_sign_ecdsa): add two data sets to check gost signatures.
* tests/curves.c: correct N_CURVES as we now have 2 more curves.
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
Removed some comments from the new curve definitions in ecc-curves.c
to avoid line wrapping. Eventually we will develop a precompiler to
avoid parsing those hex strings. -wk
Dmitry Eremin-Solenikov [Tue, 15 Oct 2013 19:56:43 +0000 (23:56 +0400)]
Fix 256-bit ecdsa test key definition
* tests/basic.c (check_pubkey): fix nistp256 testing key declaration -
add missing comma.
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
Werner Koch [Wed, 16 Oct 2013 14:20:56 +0000 (16:20 +0200)]
sexp: Add function gcry_sexp_extract_param.
* src/gcrypt.h.in (_GCRY_GCC_ATTR_SENTINEL): New.
(gcry_sexp_extract_param): New.
* src/visibility.c (gcry_sexp_extract_param): New.
* src/visibility.h (gcry_sexp_extract_param): Add hack to detect
internal use.
* cipher/pubkey-util.c (_gcry_pk_util_extract_mpis): Move and split
into ...
* src/sexp.c (_gcry_sexp_vextract_param)
(_gcry_sexp_extract_param): this. Change all callers. Add support for buffer
descriptors and a path option/
* tests/tsexp.c (die, hex2buffer, hex2mpi, hex2mpiopa): New.
(cmp_mpihex, cmp_bufhex): New.
(check_extract_param): New.
Signed-off-by: Werner Koch <wk@gnupg.org>
NIIBE Yutaka [Wed, 2 Oct 2013 00:27:09 +0000 (09:27 +0900)]
mpi: mpi-pow improvement.
* mpi/mpi-pow.c (gcry_mpi_powm): New implementation of left-to-right
k-ary exponentiation.
--
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
For the Yarom/Falkner flush+reload cache side-channel attack, we
changed the code so that it always calls the multiplication routine
(even if we can skip it to get result). This results some performance
regression.
This change is for recovering performance with efficient algorithm.
Werner Koch [Tue, 15 Oct 2013 07:08:31 +0000 (09:08 +0200)]
ecc: Support use of
Ed25519 with ECDSA.
* src/cipher.h (PUBKEY_FLAG_ECDSA): New.
* cipher/pubkey-util.c (_gcry_pk_util_parse_flaglist): Add flag "ecdsa".
* cipher/ecc.c (verify_ecdsa, verify_eddsa): Remove some debug output.
(ecc_generate, ecc_sign, ecc_verify): Support
Ed25519 with ECDSA.
* tests/keygen.c (check_ecc_keys): Create such a test key.
* tests/pubkey.c (fail, info, data_from_hex, extract_cmp_data): New.
Take from dsa-6979.c
(check_ed25519ecdsa_sample_key): new.
(main): Call new test.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Mon, 14 Oct 2013 17:48:10 +0000 (19:48 +0200)]
pubkey: Support flags list in gcry_pk_genkey.
* src/cipher.h (PUBKEY_FLAG_TRANSIENT_KEY): New.
(PUBKEY_FLAG_USE_X931): New.
(PUBKEY_FLAG_USE_FIPS186): New.
(PUBKEY_FLAG_USE_FIPS186_2): New.
* cipher/pubkey-util.c (_gcry_pk_util_parse_flaglist): Rename from
parse_flags_list. Parse new flags.
* cipher/dsa.c (dsa_generate): Support flag list.
* cipher/ecc.c (ecc_generate): Ditto.
* cipher/rsa.c (rsa_generate): Ditto.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Mon, 14 Oct 2013 08:21:53 +0000 (10:21 +0200)]
pubkey: Remove duplicated flag parsing code.
* cipher/pubkey-util.c (_gcry_pk_util_preparse_encval)
(_gcry_pk_util_data_to_mpi): Factor flag parsing code out to ..
(parse_flag_list): New.
* src/cipher.h (PUBKEY_FLAG_RAW_FLAG): New.
--
A minor disadvantage of that code is that invalid flags are not
anymore detected depending on the use. According to the documentation
this is anyway the expected behavior.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Mon, 14 Oct 2013 06:09:31 +0000 (08:09 +0200)]
mpicalc: Accept lowercase hex digits.
* src/mpicalc.c (main): Test for lowercase hex digits.
--
This change is useful so to make use of Libgcrypt debug code easier.
Werner Koch [Fri, 11 Oct 2013 20:37:41 +0000 (22:37 +0200)]
pubkey: Move sexp parsing of remaining fucntions to the modules.
* cipher/pubkey.c (release_mpi_array): Remove.
(pubkey_check_secret_key): Remove.
(sexp_elements_extract): Remove.
(sexp_elements_extract_ecc): Remove.
(sexp_to_key): Remove.
(get_hash_algo): Remove.
(gcry_pk_testkey): Revamp.
(gcry_pk_get_curve): Revamp.
* cipher/rsa.c (rsa_check_secret_key): Revamp.
* cipher/elgamal.c (elg_check_secret_key): Revamp.
* cipher/dsa.c (dsa_check_secret_key): Revamp.
* cipher/ecc.c (ecc_check_secret_key): Revamp.
* cipher/ecc-curves.c: Include cipher.h and pubkey-internal.h
(_gcry_ecc_get_curve): Revamp.
* cipher/pubkey-util.c (_gcry_pk_util_extract_mpis): Set passed and
used parameters on error to NULL.
--
That is the final part of the changes modulo introduced regressions.
pubkey.c is now actually maintainable code.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Fri, 11 Oct 2013 19:13:12 +0000 (21:13 +0200)]
pubkey: Move sexp parsing for gcry_pk_decrypt to the modules.
* cipher/rsa.c (rsa_decrypt): Revamp.
* cipher/elgamal.c (elg_decrypt): Revamp.
* cipher/ecc.c (ecc_decrypt_raw): Revamp.
* cipher/pubkey.c (gcry_pk_decrypt): Simplify.
(sexp_to_enc): Remove.
* cipher/pubkey-util.c (_gcry_pk_util_preparse_encval): New.
--
Note that we do not have a regression test for ecc_decrypt_raw. Even
GnuPG does not use it. we also better check whether the interface is
really usable; for example GnuPG implements way to much low-level ECC
code. Maybe we should move the OpenPGP ECC encryption code into
Libgcrypt.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Fri, 11 Oct 2013 13:39:22 +0000 (15:39 +0200)]
pubkey: Move sexp parsing for gcry_pk_encrypt to the modules.
* cipher/rsa.c (rsa_encrypt): Revamp.
* cipher/elgamal.c (elg_encrypt): Revamp.
* cipher/ecc.c (ecc_encrypt_raw): Revamp.
* cipher/pubkey.c (gcry_pk_encrypt): Simplify.
* tests/basic.c (check_pubkey_crypt): Init plain, ciph, and data so
that they are initialized even after an encrypt failure.
--
Note that we do not have a regression test for ecc_encrypt_raw. Thus
it is possible that a bug has been introduced. Should be tested using
GnuPG master.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Fri, 11 Oct 2013 11:32:35 +0000 (13:32 +0200)]
pubkey: Move sexp parsing for gcry_pk_sign to the modules.
* cipher/rsa.c (rsa_sign): Revamp.
* cipher/dsa.c (dsa_sign): Revamp.
* cipher/elgamal.c (elg_sign): Revamp.
* cipher/ecc.c (ecc_sign): Revamp.
* cipher/pubkey.c (gcry_pk_sign): Simplify.
Signed-off-by: Werner Koch <wk@gnupg.org>
Jussi Kivilinna [Wed, 2 Oct 2013 17:47:56 +0000 (20:47 +0300)]
Prevent tail call optimization with _gcry_burn_stack
* configure.ac: New check, HAVE_GCC_ASM_VOLATILE_MEMORY.
* src/g10lib.h (_gcry_burn_stack): Rename to __gcry_burn_stack.
(__gcry_burn_stack_dummy): New.
(_gcry_burn_stack): New macro.
* src/misc.c (_gcry_burn_stack): Rename to __gcry_burn_stack.
(__gcry_burn_stack_dummy): New.
--
Tail call optimization can turn _gcry_burn_stack call in to tail jump. When
this happens, stack pointer is restored to initial state of current function.
This causes problem for _gcry_burn_stack because its callers do not count in
current function stack depth.
One solution is to prevent gcry_burn_stack being tail optimized by inserting
dummy function call behind it. Another would be to add memory barrier 'asm
volatile("":::"memory")' behind every _gcry_burn_stack call. This however
requires GCC asm support from compiler.
Patch adds detection for memory barrier support and when available uses
memory barrier to prevent when tail call optimization. If not available
dummy function call is used instead.
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Werner Koch [Wed, 9 Oct 2013 13:05:26 +0000 (15:05 +0200)]
pubkey: Move sexp parsing for gcry_pk_verify to the modules.
* cipher/rsa.c (rsa_verify): Revamp.
* cipher/dsa.c (dsa_verify): Revamp.
* cipher/elgamal.c (elg_verify): Revamp.
* cipher/ecc.c (ecc_verify): Revamp.
* cipher/pubkey.c (sexp_to_sig): Remove.
(pss_verify_cmp): Move to pubkey-util.c
(sexp_data_to_mpi): Ditto.
(init_encoding_ctx): Ditto.
(gcry_pk_verify): Simplify.
* cipher/pubkey-util.c (_gcry_pk_util_init_encoding_ctx): Add. Take
from pubkey.c
(get_hash_algo): Ditto.
(_gcry_pk_util_data_to_mpi): Ditto.
(pss_verify_cmp): Ditto.
(_gcry_pk_util_extract_mpis): New.
(_gcry_pk_util_preparse_sigval): New.
(_gcry_pk_util_free_encoding_ctx): New.
* cipher/ecc-curves.c (_gcry_ecc_fill_in_curve): Make curve init
optional.
* src/g10lib.h (GCC_ATTR_SENTINEL): New.
* tests/basic.c (check_pubkey_sign): Print the algo name.
(main): Add option --pubkey.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Tue, 8 Oct 2013 18:51:39 +0000 (20:51 +0200)]
pubkey: Move sexp parsing for gcry_pk_get_nbits to the modules.
* cipher/pubkey.c (spec_from_sexp): New.
(gcry_pk_get_nbits): Simplify.
* cipher/rsa.c (rsa_get_nbits): Take only PARMS as args and do sexp
parsing here.
* cipher/dsa.c (dsa_get_nbits): Ditto.
* cipher/elgamal.c (elg_get_nbits): Ditto.
* cipher/ecc.c (ecc_get_nbits): Ditto.
* cipher/ecc-curves.c (_gcry_ecc_fill_in_curve): Allow NULL for arg
CURVE.
--
gcry_pk_get_nbits should now also be faster for ECC because there is
no more need to copy all the parms if a curve name has been given.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Tue, 8 Oct 2013 15:59:50 +0000 (17:59 +0200)]
pubkey: Move sexp parsing for gcry_pk_getkey to the modules.
* cipher/pubkey-util.c: New.
(_gcry_pk_util_get_nbits): New. Based on code from gcry_pk_genkey.
(_gcry_pk_util_get_rsa_use_e): Ditto.
* cipher/pubkey.c (gcry_pk_genkey): Strip most code and pass.
* cipher/rsa.c (rsa_generate): Remove args ALGO, NBITS and EVALUE.
Call new fucntions to get these values.
* cipher/dsa.c (dsa_generate): Remove args ALGO, NBITS and EVALUE.
Call _gcry_pk_util_get_nbits to get nbits. Always parse genparms.
* cipher/elgamal.c (elg_generate): Ditto.
* cipher/ecc.c (ecc_generate): Ditto.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Tue, 8 Oct 2013 14:18:09 +0000 (16:18 +0200)]
cipher: Deprecate GCRY_PK_ELG_E.
* cipher/elgamal.c (_gcry_pubkey_spec_elg_e): Remove.
* cipher/pubkey.c (pubkey_list): Remove double included
_gcry_pubkey_spec_elg.
(map_algo): MAke ELG_E to ELG.
--
It does not make sense to have different algorithm identifiers for
Elgamal encryption and the obsolete Elgamal signing. Thus mark one of
them as deprecated and remove the useless extra spec info in
elgamal.c. Frankly it is questionable why at all we need the
algorithm identifiers; they are a remain from the time when Libgcrypt
was part of GnuPG. With the s-expression based API the IDs they don't
make much sense.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Sun, 6 Oct 2013 12:47:22 +0000 (14:47 +0200)]
ecc: Add OID for
Ed25519.
--
See:
From: Peter Gutmann <pgut001@cs.auckland.ac.nz>
Subject: Re: [cryptography] Curve25519 OID (was: Re: the spell is broken)
To: xxxxxxxxxxxxx
Cc: cryptography@randombit.net
Message-Id: <E1VSk4L-0004JD-AI@login01.fos.auckland.ac.nz>
Date: Sun, 06 Oct 2013 21:52:05 +1300
xxxxxxxxxxxxxxx writes:
>For completeness, Crypto++ has a factory-like method that serves curves. The
>curves are sorted by OID in the function, so Crypto++ would need an OID for
>
ed25519.
{ 1 3 6 1 4 1 3029 1 5 1 } ed209^H^H5519
You have been OIDed. Go forth and encrypt.
Peter.
Werner Koch [Wed, 2 Oct 2013 15:47:03 +0000 (17:47 +0200)]
doc: Type fix.
--
Werner Koch [Wed, 2 Oct 2013 15:45:13 +0000 (17:45 +0200)]
doc: Remove note that RC2/128 is not yet supported.
--
Werner Koch [Wed, 2 Oct 2013 14:56:46 +0000 (16:56 +0200)]
Provide Pth compatiblity for use with GnuPG 2.0.
* src/ath.c (ath_install): Call ath_init and declare Pth as
compatible.
--
GnuPG 2.0 requires GNU Pth which is a plain userland thread
implementation. Given that decent versions of GNU Pth seem to work
together with pthread, we can declare Pth as compatible. Native
pthreads in Libgcrypt are only used internal to Libgcrypt and any
internal blocking should be invisible to Pth.
Signed-off-by: Werner Koch <wk@gnupg.org>
Jussi Kivilinna [Wed, 2 Oct 2013 12:59:59 +0000 (15:59 +0300)]
sha512: fix building on ARM
* cipher/sha512.c (transform) [USE_ARM_NEON_ASM]: Fix 'hd' to 'ctx'.
--
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Werner Koch [Wed, 2 Oct 2013 12:14:57 +0000 (14:14 +0200)]
Remove deprecated control codes.
* src/gcrypt.h.in (GCRYCTL_SET_KEY): Remove.
(GCRYCTL_SET_IV): Remove.
(GCRYCTL_SET_CTR): Remove.
* cipher/md.c (gcry_md_ctl): Remove deprecated GCRYCTL_SET_KEY.
* cipher/cipher.c (gcry_cipher_ctl): Remove deprecated
GCRYCTL_SET_KEY, GCRYCTL_SET_IV, GCRYCTL_SET_CTR.
--
Real functions are available for a long time now thus there is no more
point in supporting the control code hacks. We have an ABI break
anyway thus this is a good time to get rid of them.
Signed-off-by: Werner Koch <wk@gnupg.org>
Dmitry Eremin-Solenikov [Thu, 19 Sep 2013 21:14:12 +0000 (01:14 +0400)]
Fix errors when building with Clang on PPC
* mpi/longlong.h (add_ssaaaa, sub_ddmmss, count_leading_zeros,
umul_ppmm): Do not cast asm output to USItype.
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
Werner Koch [Wed, 2 Oct 2013 11:53:07 +0000 (13:53 +0200)]
Remove last remains of the former module system.
* src/gcrypt-module.h, src/module.c: Remove.
* src/visibility.h: Do not include gcrypt-module.h.
* src/g10lib.h: Remove all prototypes from module.c
(gcry_module): Remove.
* cipher/cipher-internal.h (gcry_cipher_handle): Remove unused field.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Wed, 2 Oct 2013 11:44:46 +0000 (13:44 +0200)]
Fix missing prototype warning in visibility.c
* src/ec-context.h (_gcry_mpi_ec_new): Move prototype to mpi.h.
Werner Koch [Wed, 2 Oct 2013 11:39:47 +0000 (13:39 +0200)]
md: Simplify the message digest dispatcher md.c.
* src/gcrypt-module.h (gcry_md_spec_t): Move to ...
* src/cipher-proto.h: here. Merge with md_extra_spec_t. Add fields
ALGO and FLAGS. Set these fields in all digest modules.
* cipher/md.c: Change most code to replace the former module
system by a simpler system to gain information about the algorithms.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Tue, 1 Oct 2013 20:00:50 +0000 (22:00 +0200)]
cipher: Simplify the cipher dispatcher cipher.c.
* src/gcrypt-module.h (gcry_cipher_spec_t): Move to ...
* src/cipher-proto.h (gcry_cipher_spec_t): here. Merge with
cipher_extra_spec_t. Add fields ALGO and FLAGS. Set these fields in
all cipher modules.
* cipher/cipher.c: Change most code to replace the former module
system by a simpler system to gain information about the algorithms.
(disable_pubkey_algo): Simplified. Not anymore thread-safe, though.
* cipher/md.c (_gcry_md_selftest): Use correct structure. Not a real
problem because both define the same function as their first field.
* cipher/pubkey.c (_gcry_pk_selftest): Take care of the disabled flag.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Tue, 1 Oct 2013 15:47:27 +0000 (17:47 +0200)]
mpi: Fix gcry_mpi_neg.
* mpi/mpiutil.c (_gcry_mpi_neg): Copy U to W.
Signed-off-by: Werner Koch <wk@gnupg.org>
Peter Wu [Thu, 26 Sep 2013 21:20:32 +0000 (23:20 +0200)]
cipher: Add support for 128-bit keys in RC2
* cipher/rfc2268.c (oids_rfc2268_128): New
(_gcry_cipher_spec_rfc2268_128): New.
* cipher/cipher.c (cipher_table_entry): Add GCRY_CIPHER_RFC2268_128.
--
This patch adds support for decrypting (and encrypting) using 128-bit
keys using the RC2 algorithm.
Signed-off-by: Peter Wu <lekensteyn@gmail.com>
Actually this is merely enabling that extra ID for 128 bit RFC2268.
We should have used one id for that algorithm only, because a second
identifier merely for having the OID in the code is a bad idea. My
initial fault and thus I better apply this patch to make the id not
entirely useless. -wk
Werner Koch [Mon, 30 Sep 2013 19:14:11 +0000 (21:14 +0200)]
ecc: Use faster b parameter for
Ed25519.
* cipher/ecc-curves.c (domain_parms): Replace b.
* tests/t-mpi-point.c (test_curve): Ditto.
--
This change has been suggested by NIIBE Yutaka:
Here,
0x98412DFC9311D490018C7338BF8688861767FF8FF5B2BEBE27548A14B235EC8FEDA4
is: (121666^-1 mod q)*121665.
(121666^-1) * 121665 mod q is:
0x2DFC9311D490018C7338BF8688861767FF8FF5B2BEBE27548A14B235ECA6874A
While it works for both, I think that shorter is better.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Mon, 30 Sep 2013 18:32:20 +0000 (20:32 +0200)]
ecc: Prepare for future
Ed25519 optimization.
* mpi/ec-
ed25519.c: New but empty file.
* mpi/ec-internal.h: New.
* mpi/ec.c: Include ec-internal.h.
(ec_mod): New.
(ec_addm): Use ec_mod.
(ec_mulm): Remove commented code. Use ec_mod.
(ec_subm): Call simple sub.
(ec_pow2): Use ec_mulm.
(ec_mul2): New.
(dup_point_weierstrass): Use ec_mul2.
(dup_point_twistededwards): Add special case for a == -1. Use
ec_mul2.
(add_points_weierstrass): Use ec_mul2.
(add_points_twistededwards): Add special case for a == -1.
(_gcry_mpi_ec_curve_point): Ditto.
(ec_p_init): Add hack to test Barrett functions.
* src/ec-context.h (mpi_ec_ctx_s): Add P_BARRETT.
* mpi/mpi-mod.c (_gcry_mpi_mod_barrett): Fix sign problem.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Mon, 30 Sep 2013 18:17:05 +0000 (20:17 +0200)]
ecc: Fix recomputing of Q for
Ed25519.
* cipher/ecc-misc.c (reverse_buffer): New.
(_gcry_ecc_compute_public): Add
ED255519 specific code.
* cipher/ecc.c (sign_eddsa): Allocate DIGEST in secure memory. Get
rid of HASH_D.
* tests/t-mpi-point.c (context_param): Test recomputing of Q for
Ed25519.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Mon, 30 Sep 2013 11:20:06 +0000 (13:20 +0200)]
log: Try to print s-expressions in a more compact format.
* src/misc.c (count_closing_parens): New.
(_gcry_log_printsxp): Use new function.
* mpi/ec.c (_gcry_mpi_point_log): Take care of a NULL point.
Signed-off-by: Werner Koch <wk@gnupg.org>
Jussi Kivilinna [Sat, 21 Sep 2013 10:54:38 +0000 (13:54 +0300)]
Make Whirlpool use the _gcry_md_block_write helper
* cipher/whirlpool.c (whirlpool_context_t): Add 'bctx', remove
'buffer', 'count' and 'nblocks'.
(whirlpool_init): Initialize 'bctx'.
(whirlpool_transform): Adjust context argument type and burn stack
depth.
(whirlpool_add): Remove.
(whirlpool_write): Use _gcry_md_block_write.
(whirlpool_final, whirlpool_read): Adjust for 'bctx' usage.
--
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Jussi Kivilinna [Sat, 21 Sep 2013 10:54:38 +0000 (13:54 +0300)]
whirlpool: add stack burning after transform
* cipher/whirlpool.c (whirlpool_transform): Return burn stack depth.
(whirlpool_add): Do burn_stack.
--
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Jussi Kivilinna [Sat, 21 Sep 2013 10:54:38 +0000 (13:54 +0300)]
whirlpool: do bitcount calculation in finalization part
* cipher/whirlpool.c (whirlpool_context_t): Remove 'length', add
'nblocks'.
(whirlpool_add): Update 'nblocks' instead of 'length', and add early
return at one spot.
(whirlpool_write): Check for 'nblocks' overflow.
(whirlpool_final): Convert 'nblocks' to bit-counter, and use
whirlpool_write instead of whirlpool_add.
--
Currently Whirlpool uses large 256 bit counter that is increased in the
'write' function. However, we could to bit counter calculation as is
done in all the rest hash algorithms; use 64-bit block counter that is
converted to bit counter in finalization function. This change does
limit amount of bytes Whirlpool can process before overflowing bit counter.
With 256-bit counter, overflow happens after ~1.3e67 gigabytes. With 64-bit
block counter, overflow happens just after ~1.1e12 gigabytes. Patch keeps
the old behaviour of halting if counter overflows.
Main benefit for this patch is that after this change, we can use the
_gcry_md_block_write helper for Whirlpool too.
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Werner Koch [Mon, 30 Sep 2013 08:18:25 +0000 (10:18 +0200)]
Add logging functions to the API.
* src/gcrypt.h.in (_GCRY_GCC_ATTR_PRINTF): New.
(gcry_log_debug, gcry_log_debughex, gcry_log_debugmpi): New.
(gcry_log_debugpnt, gcry_log_debugsxp): New.
* src/visibility.c (gcry_log_debug): New.
(gcry_log_debughex, gcry_log_debugmpi, gcry_log_debugpnt): New.
(gcry_log_debugsxp): New.
* src/libgcrypt.def, src/libgcrypt.vers: Add new functions.
* src/misc.c (_gcry_logv): Make public.
(_gcry_log_printsxp): New.
* src/g10lib.h (log_printsxp): New macro.
--
For debugging applications it is often required to dump certain data
structures. Libgcrypt uses several internal functions for this. To
avoid re-implementing everything in the caller, we now provide access
to some of those functions.
Signed-off-by: Werner Koch <wk@gnupg.org>
Jussi Kivilinna [Sat, 21 Sep 2013 10:54:38 +0000 (13:54 +0300)]
Make libgcrypt build with Clang on i386
* cipher/longlong.h [__i386__] (add_ssaaaa, sub_ddmmss)
(umul_ppmm, udiv_qrnnd): Do not cast asm output to USItype.
--
Clang defines __GNUC__ even when it's not GCC compatible. As result Clang
enables GCC-only assembly code in mpi/longlong.h and fails to build.
However, since changes to make libgcrypt build with Clang are smallish, and
changes do not cause problems with GCC, patch just does them.
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
mpi: Change not yet used _gcry_mpi_set_opaque_copy.
* mpi/mpiutil.c (_gcry_mpi_set_opaque_copy): Change prototype.
(_gcry_mpi_get_opaque_copy): Take care of gcry_malloc failure.
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
sexp: Improve printing of data with a leading zero.
* src/sexp.c (suitable_encoding): Detect leading zero byte.
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
ecc: Allow the name "q@eddsa" to get/set the public key.
* cipher/ecc-curves.c (_gcry_ecc_get_mpi): Support "q@eddsa".
(_gcry_ecc_set_mpi): Support "q".
* cipher/ecc.c (eddsa_encodepoint): Rename to ...
(_gcry_ecc_eddsa_encodepoint): this and make global. Remove arg
MINLEN and take from context.
(eddsa_decodepoint): Rename to
(_gcry_ecc_eddsa_decodepoint): this and make global. Remove arg LEN
and take from context.
(sign_eddsa, verify_eddsa): Take B from context.
(ecc_sign, ecc_verify): Add hack to set DIALECT.
(_gcry_pk_ecc_get_sexp): Use _gcry_ecc_compute_public. Handle EdDSA.
* src/ec-context.h (mpi_ec_ctx_s): Add field NBITS.
* mpi/ec.c (ec_p_init): Init NBITS.
* tests/t-mpi-point.c (test_curve): Add
Ed25519.
(sample_ed25519_q): New.
(context_param): Check new sample key.
(hex2buffer, hex2mpiopa): New.
(cmp_mpihex): Take care of opaque MPIs.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
mpicalc: Add statement to compute the number of bits.
* src/mpicalc.c (do_nbits): New.
(main): Add statement 'b'.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
ecc: Refactor low-level access functions.
* mpi/ec.c (point_copy): Move to cipher/ecc-curves.c.
(ec_get_reset): Rename to _gcry_mpi_ec_get_reset and make global.
(_gcry_mpi_ec_get_mpi): Factor most code out to _gcry_ecc_get_mpi.
(_gcry_mpi_ec_get_point): Factor most code out to _gcry_ecc_get_point.
(_gcry_mpi_ec_set_mpi): Factor most code out to _gcry_ecc_set_mpi.
(_gcry_mpi_ec_set_point): Factor most code out to _gcry_ecc_set_point.
* cipher/ecc-curves.c (_gcry_ecc_get_mpi): New.
(_gcry_ecc_get_point, _gcry_ecc_set_mpi, _gcry_ecc_set_point): New.
* cipher/ecc-misc.c (_gcry_ecc_compute_public): New.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
ecc: Fix highly unlikely endless loop in sign_ecdsa.
* cipher/ecc.c (sign_ecdsa): Turn while-do into do-while loops.
--
Reported-by: Dmitry Eremin-Solenikov
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
ecc: Allow the use of an uncompressed public key.
* cipher/ecc.c (eddsa_encodepoint): Factor most code out to ...
(eddsa_encode_x_y): new fucntion.
(eddsa_decodepoint): Allow use of an uncompressed public key.
* tests/t-
ed25519.c (N_TESTS): Adjust.
* tests/t-
ed25519.inp: Add test 1025.
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
pk: Add algo id GCRY_PK_ECC and deprecate ECDSA and ECDH.
* src/gcrypt.h.in (GCRY_PK_ECC): New.
* cipher/pubkey.c (map_algo): New.
(spec_from_algo, gcry_pk_get_param, _gcry_pk_selftest): Use it.
* cipher/ecc.c (selftests_ecdsa): Report using GCRY_PK_ECC.
(run_selftests): Simplify.
(ecdh_names, ecdsa_names): Merge into a new ecc_names.
(_gcry_pubkey_spec_ecdh, _gcry_pubkey_spec_ecdsa): Merge into new
_gcry_pubkey_spec_ecc.
--
The algo ids are actually a relict from Libgcrypt's former life as
GnuPG's crypto code. They don't make much sense anymore and are often
not needed.
This patch requires some changes to the GnuPG 2.1 code (which has
still not been released). For example the secret key transfer between
gpg and gpg-agent (gpg --export and gpg --import). Fortunately this
will also require to add usage flags to the secret key storage of
gpg-agent which is is something we should have done a long time ago.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
ec: Use mpi_mulm instead of mpi_powm.
* mpi/ec.c (ec_pow2): New.
(ec_powm): Remove call to mpi_abs.
(dup_point_weierstrass, dup_point_twistededwards)
(add_points_weierstrass, add_points_twistededwards)
(_gcry_mpi_ec_curve_point): Use ec_pow2.
Signed-off-by: Werner Koch <wk@gnupg.org>
Jussi Kivilinna [Sat, 21 Sep 2013 10:54:38 +0000 (13:54 +0300)]
bufhelp: enable fast unaligned memory accesses on powerpc
* cipher/bufhelp.h [__powerpc__] (BUFHELP_FAST_UNALIGNED_ACCESS): Set
macro enabled.
[__powerpc64__] (BUFHELP_FAST_UNALIGNED_ACCESS): Ditto.
--
PowerPC can handle unaligned memory accesses fast, so enable fast
buffer handling in bufhelp.h.
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Jussi Kivilinna [Sat, 21 Sep 2013 10:54:38 +0000 (13:54 +0300)]
Remove i386 inline assembly version of rotation functions
* cipher/bithelp.h (rol, ror): Remove i386 version, change
macros to inline functions.
* src/hmac256.c (ror): Ditto.
--
(Current) compilers can optimize '(x << c) | (x >> (32-c))' to rotation
instruction. So remove i386 specific assembly for manually doing this.
Furthermore, compiler can generate faster code in case where 'c' is
constant and can use rotate with immediate value rather than rotate
with %cl register.
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Jussi Kivilinna [Sat, 21 Sep 2013 10:54:38 +0000 (13:54 +0300)]
Optimize and cleanup 32-bit and 64-bit endianess transforms
* cipher/bithelp.h (bswap32, bswap64, le_bswap32, be_bswap32)
(le_bswap64, be_bswap64): New.
* cipher/bufhelp.h (buf_get_be32, buf_get_le32, buf_put_le32)
(buf_put_be32, buf_get_be64, buf_get_le64, buf_put_be64)
(buf_put_le64): New.
* cipher/blowfish.c (do_encrypt_block, do_decrypt_block): Use new
endian conversion helpers.
(do_bf_setkey): Turn endian specific code to generic.
* cipher/camellia.c (GETU32, PUTU32): Use new endian conversion
helpers.
* cipher/cast5.c (rol): Remove, use rol from bithelp.
(F1, F2, F3): Fix to use rol from bithelp.
(do_encrypt_block, do_decrypt_block, do_cast_setkey): Use new endian
conversion helpers.
* cipher/des.c (READ_64BIT_DATA, WRITE_64BIT_DATA): Ditto.
* cipher/md4.c (transform, md4_final): Ditto.
* cipher/md5.c (transform, md5_final): Ditto.
* cipher/rmd160.c (transform, rmd160_final): Ditto.
* cipher/salsa20.c (LE_SWAP32, LE_READ_UINT32): Ditto.
* cipher/scrypt.c (READ_UINT64, LE_READ_UINT64, LE_SWAP32): Ditto.
* cipher/seed.c (GETU32, PUTU32): Ditto.
* cipher/serpent.c (byte_swap_32): Remove.
(serpent_key_prepare, serpent_encrypt_internal)
(serpent_decrypt_internal): Use new endian conversion helpers.
* cipher/sha1.c (transform, sha1_final): Ditto.
* cipher/sha256.c (transform, sha256_final): Ditto.
* cipher/sha512.c (__transform, sha512_final): Ditto.
* cipher/stribog.c (transform, stribog_final): Ditto.
* cipher/tiger.c (transform, tiger_final): Ditto.
* cipher/twofish.c (INPACK, OUTUNPACK): Ditto.
* cipher/whirlpool.c (buffer_to_block, block_to_buffer): Ditto.
* configure.ac (gcry_cv_have_builtin_bswap32): Check for compiler
provided __builtin_bswap32.
(gcry_cv_have_builtin_bswap64): Check for compiler provided
__builtin_bswap64.
--
Patch add helper functions that provide conversions to/from integers and
buffers of different endianess. Benefits are code cleanup and optimization
for architectures that have byte-swaping instructions and/or can do fast
unaligned memory accesses.
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Jussi Kivilinna [Sat, 21 Sep 2013 10:54:38 +0000 (13:54 +0300)]
gostr3411_94: set better burn stack depth estimate
* cipher/gost28147.c (_gcry_gost_enc_one): Account function stack to
burn stack depth.
* cipher/gostr3411-94.c (max): New macro.
(do_hash_step, transform): Return stack burn depth.
--
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Jussi Kivilinna [Sat, 21 Sep 2013 10:54:38 +0000 (13:54 +0300)]
Use hash transform function return type for passing burn stack depth
* cipher/gostr4311-94.c (transform): Return stack burn depth.
* cipher/hash-common.c (_gcry_md_block_write): Use stack burn depth
returned by 'hd->bwrite'.
* cipher/hash-common.h (_gcry_md_block_write_t): Change return type to
'unsigned int'.
(gry_md_block_ctx_t): Remove 'stack_burn'.
* cipher/md4.c (transform): Return stack burn depth.
(md4_final): Use stack burn depth from transform.
* cipher/md5.c (transform): Return stack burn depth.
(md5_final): Use stack burn depth from transform.
* cipher/rmd160.c (transform): Return stack burn depth.
(rmd160_final): Use stack burn depth from transform.
* cipher/sha1.c (transform): Return stack burn depth.
(sha1_final): Use stack burn depth from transform.
* cipher/sha256.c (transform): Return stack burn depth.
(sha256_final): Use stack burn depth from transform.
* cipher/sha512.c (__transform, transform): Return stack burn depth.
(sha512_final): Use stack burn depth from transform.
* cipher/stribog.c (transform64): Return stack burn depth.
* cipher/tiger.c (transform): Return stack burn depth.
(tiger_final): Use stack burn depth from transform.
--
Transform function might want different depth of stack burn depending on
detected CPU features (like in SHA-512 on ARM with NEON). So return
stack burn depth from transform functions as a request or a hint to
calling function.
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Jussi Kivilinna [Sat, 21 Sep 2013 10:54:38 +0000 (13:54 +0300)]
Make STRIBOG use the new _gcry_md_block_write helper
* cipher/stribog.c (STRIBOG_STRUCT): Add 'bctx' and remove 'buf' and
'count'.
(stribog_init_512): Initialize 'bctx'.
(transform64): New function.
(stribog_write): Remove.
(stribog_final): Use _gcry_md_block_write and bctx.
(_gcry_digest_spec_stribog_256, _gcry_digest_spec_stribog_512): Use
_gcry_md_block_write.
--
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Jussi Kivilinna [Sat, 21 Sep 2013 10:54:38 +0000 (13:54 +0300)]
Make SHA-512 use the new _gcry_md_block_write helper
* cipher/hash-common.c (_gcry_md_block_write): Check that hd->buf is
large enough.
* cipher/hash-common.h (MD_BLOCK_MAX_BLOCKSIZE, MD_NBLOCKS_TYPE): New
macros.
(gcry_md_block_ctx_t): Use above macros for 'nblocks' and 'buf'.
* cipher/sha512.c (SHA512_STATE): New struct.
(SHA512_CONTEXT): Add 'bctx' and 'state'.
(sha512_init, sha384_init): Initialize 'bctx'.
(__transform, _gcry_sha512_transform_armv7_neon): Use SHA512_STATE for
'hd'.
(transform): For now, do not return burn stack.
(sha512_write): Remove.
(sha512_final): Use _gcry_md_block_write and bctx.
(_gcry_digest_spec_sha512, _gcry_digest_spec_sha384): Use
_gcry_md_block_write.
--
Patch changes 'nblocks' counter to 64-bits when SHA-512 is enabled. This does
not cause problems with other algorithms; they are already casting 'nblocks'
to u32 variable in their finalization functions. Also move 'buf' member to
head of 'gcry_md_block_ctx_t' to ensure proper alignment; this is because some
algorithms cast buffer pointer to (u64*) in final endian conversion.
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
sexp: Change internal versions to always use gpg_err_code_t.
* src/sexp.c (gcry_sexp_new, gcry_sexp_create, gcry_sexp_build)
(gcry_sexp_build_array, gcry_sexp_canon_len): Change error return type
from gpg_error_t to gpg_err_code_t. Remove all calls to gpg_error.
* src/visibility.c (gcry_sexp_new, gcry_sexp_create, gcry_sexp_sscan)
(gcry_sexp_build, gcry_sexp_build_array, gcry_sexp_canon_len): Map
error codes via gpg_error.
* cipher/dsa.c, cipher/ecc.c, cipher/elgamal.c, cipher/rsa.c: Remove
use gpg_err_code wrappers.
--
We should do such a change for all other use of internal functions.
It just does not make sense to use gpg_error in the internal interface
because the error source is always Libgcrypt.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
pk: Move s-exp creation for gcry_pk_decrypt to the modules.
* cipher/pubkey.c (sexp_to_enc): Remove RET_MODERN arg and merge it
into FLAGS.
(gcry_pk_decrypt): Move result s-exp building into the modules.
* src/cipher-proto.h (gcry_pk_decrypt_t): Add some args.
* cipher/ecc.c (ecc_decrypt_raw): Change to return an s-exp.
* cipher/elgamal.c (elg_decrypt): Ditto.
* cipher/rsa.c (rsa_decrypt): Ditto.
(rsa_blind, rsa_unblind): Merge into rsa_decrypt. This saves several
extra MPI allocations.
--
The extra args added to gcry_pk_decrypt_t are a temporary solution
unti we move the input s-exp parsing also into the modules.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
pk: Remove unused function.
* cipher/pubkey.c (_gcry_pk_aliased_algo_name): Remove
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
Beautify debug output of the prime generator.
* cipher/primegen.c: Adjust output of log_mpidump to recently changed
log_mpidump code changes.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
pk: Move s-expr creation for genkey to the modules.
* cipher/pubkey.c (pubkey_generate): Fold into gcry_pk_genkey
(gcry_pk_genkey): Move result s-exp creation into the modules.
* cipher/dsa.c (dsa_generate): Create result as s-exp.
* cipher/elgamal.c (elg_generate): Ditto.
* cipher/rsa.c (rsa_generate): Ditto.
* cipher/ecc.c (ecc_generate): Ditto.
* src/cipher-proto.h (pk_ext_generate_t): Remove type
(gcry_pk_spec): and remove from struct.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
tests: Beautify some diagnostics.
* tests/benchmark.c (ecc_bench): Print the key sexp in very verbose
mode.
(main): Add option --pk-count.
* tests/keygen.c: Add Elgamal generation and improved diagnostics.
* tests/t-
ed25519.c (check_ed25519): Print running number of tests
done.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
sexp: Improve printing data representing a negative number.
* src/sexp.c (suitable_encoding): Detect a negative number.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
pk: Move RSA encoding functions to a new file.
* cipher/rsa-common: New.
* cipher/pubkey.c (pkcs1_encode_for_encryption): Move to rsa-common.c
and rename to _gcry_rsa_pkcs1_encode_for_enc.
(pkcs1_decode_for_encryption): Move to rsa-common.c and rename to
_gcry_rsa_pkcs1_decode_for_enc.
(pkcs1_encode_for_signature): Move to rsa-common.c and rename to
_gcry_rsa_pkcs1_encode_for_sig.
(oaep_encode): Move to rsa-common.c and rename to
_gcry_rsa_oaep_encode.
(oaep_decode): Move to rsa-common.c and rename to
_gcry_rsa_oaep_decode.
(pss_encode): Move to rsa-common.c and rename to _gcry_rsa_pss_encode.
(pss_verify): Move to rsa-common.c and rename to _gcry_rsa_pss_decode.
(octet_string_from_mpi, mgf1): Move to rsa-common.c.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
pk: Move s-expr creation for sign and encrypt to the modules.
* cipher/pubkey.c (pubkey_encrypt): Fold into gcry_pk_encrypt.
(pubkey_decrypt): Fold into gcry_pk_decrypt.
(pubkey_sign): Fold into gcry_pk_sign.
(pubkey_verify): Fold into gcry_pk_verify.
(octet_string_from_mpi): Make it a wrapper and factor code out to ...
* mpi/mpicoder.c (_gcry_mpi_to_octet_string): New function.
* src/cipher.h (PUBKEY_FLAG_FIXEDLEN): New.
* cipher/pubkey.c (sexp_data_to_mpi): Set flag for some encodings.
(gcry_pk_encrypt): Simply by moving the s-expr generation to the modules.
(gcry_pk_sign): Ditto.
* cipher/dsa.c (dsa_sign): Create s-expr.
* cipher/elgamal.c (elg_encrypt, elg_sign): Ditto.
* cipher/rsa.c (rsa_encrypt, rsa_sign): Ditto.
* cipher/ecc.c (ecc_sign, ecc_encrypt_raw): Ditto.
(ecdsa_names): Add "eddsa".
* tests/t-
ed25519.c (one_test): Expect "eddsa" token.
Signed-off-by: Werner Koch <wk@gnupg.org>
Dmitry Eremin-Solenikov [Mon, 16 Sep 2013 02:55:13 +0000 (06:55 +0400)]
Fix Stribog digest on bigendian platforms
* cipher/stribog.c (stribog_final): swap bytes in the result of digest
calculations.
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
pk: Simplify the public key dispatcher pubkey.c.
* src/cipher-proto.h (gcry_pk_spec_t): Add fields ALGO and FLAGS.
* cipher/dsa.c (_gcry_pubkey_spec_dsa): Set these fields.
* cipher/ecc.c (_gcry_pubkey_spec_ecdsa): Ditto.
(_gcry_pubkey_spec_ecdh): Ditto.
* cipher/rsa.c (_gcry_pubkey_spec_rsa): Ditto.
* cipher/elgamal.c (_gcry_pubkey_spec_elg): Ditto
(_gcry_pubkey_spec_elg_e): New.
* cipher/pubkey.c: Change most code to replace the former module
system by a simpler system to gain information about the algorithms.
(disable_pubkey_algo): SImplified. Not anymore thread-safe, though.
Signed-off-by: Werner Koch <wk@gnupg.org>
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
pk: Merge extraspecs struct with standard specs struct.
* src/gcrypt-module.h (gcry_pk_spec_t): Move this typedef and the
corresponding function typedefs to ...
* src/cipher-proto.h: here.
(pk_extra_spec_t): Remove typedef and merge fields into
gcry_pk_spec_t.
* cipher/rsa.c, cipher/dsa.c, cipher/elg.c, cipher/ecc.c: Ditto.
* cipher/pubkey.c: Change accordingly.
* src/cipher.h (_gcry_pubkey_extraspec_rsa): Remove.
(_gcry_pubkey_extraspec_dsa): Remove.
(_gcry_pubkey_extraspec_elg): Remove.
(_gcry_pubkey_extraspec_ecdsa): Remove.
--
Now that we don't have loadable modules anymore, we don't need to keep
the internal API between the modules and thus can simplify the code.
Signed-off-by: Werner Koch <wk@gnupg.org>
Jussi Kivilinna [Wed, 18 Sep 2013 14:13:53 +0000 (17:13 +0300)]
Fix encryption/decryption return type for GOST28147
* cipher/gost.h (_gcry_gost_enc_one): Change return type to
'unsigned int'.
* cipher/gost28147.c (max): New macro.
(gost_encrypt_block, gost_decrypt_block): Return burn stack depth.
(_gcry_gost_enc_one): Return burn stack depth from gost_encrypt_block.
--
Return type for block cipher functions was lately changed from 'void' to
'unsigned int' to pass burn stack depth to cipher mode code. Patch fixes
gost28147 to return stack burn value.
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Werner Koch [Sat, 7 Sep 2013 08:06:46 +0000 (10:06 +0200)]
Rename the GOST algorithm identifiers.
--
Dots and dashes in the names are probably not a good idea. I also
renamed the identifiers to names which are easier to remember.
Signed-off-by: Werner Koch <wk@gnupg.org>
Dmitry Eremin-Solenikov [Mon, 2 Sep 2013 09:28:52 +0000 (13:28 +0400)]
doc: fix building of ps and pdf documentation
* doc/gcrypt.texi, doc/gpl.texi, doc/lgpl.texi: fix texinfo errors.
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
Dmitry Eremin-Solenikov [Wed, 18 Sep 2013 12:34:18 +0000 (14:34 +0200)]
Add GOST R 34.11-2012 implementation (Stribog)
* src/gcrypt.h.in (GCRY_MD_GOSTR3411_12_256)
(GCRY_MD_GOSTR3411_12_512): New.
* cipher/stribog.c: New.
* configure.ac (available_digests_64): Add stribog.
* src/cipher.h: Declare Stribog declarations.
* cipher/md.c: Register Stribog digest.
* tests/basic.c (check_digests) Add 4 testcases for Stribog from
standard.
* doc/gcrypt.texi: Document new constants.
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
Dmitry Eremin-Solenikov [Wed, 18 Sep 2013 12:21:13 +0000 (14:21 +0200)]
Add basic implementation of GOST R 34.11-94 message digest
* src/gcrypt.h.in (GCRY_MD_GOSTR3411_94): New.
* cipher/gostr3411-94.c: New.
* configure.ac (available_digests): Add gostr3411-94.
* src/cipher.h: Add gostr3411-94 definitions.
* cipher/md.c: Register GOST R 34.11-94.
* tests/basic.c (check_digests): Add 4 tests for GOST R 34.11-94
hash algo. Two are defined in the standard itself, two other are
more or less common tests - an empty string an exclamation mark.
* doc/gcrypt.texi: Add an entry describing GOST R 34.11-94 to the MD
algorithms table.
--
Add simple implementation of GOST R 34.11-94 hash function. Currently
there is no way to specify hash parameters (it always uses GOST R 34.11-94
test parameters).
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
Stack burn value in gost3411_init added by wk.