seccure-0.5/0000755000175000017500000000000012372135411012660 5ustar bertrambertramseccure-0.5/numtheory.h0000644000175000017500000000272412372135467015103 0ustar bertrambertram/* * seccure - Copyright 2014 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program. If not, see * . */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which * is part of the GnuPG project). Use the included Makefile to build * the binary. * * Report bugs to: seccure AT point-at-infinity.org * */ #ifndef INC_NUMTHEORY_H #define INC_NUMTHEORY_H #include int mod_issquare(const gcry_mpi_t a, const gcry_mpi_t p); int mod_root(gcry_mpi_t x, const gcry_mpi_t a, const gcry_mpi_t p); #endif /* INC_NUMTHEORY_H */ seccure-0.5/ecc.h0000644000175000017500000000615612372135467013606 0ustar bertrambertram/* * seccure - Copyright 2014 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program. If not, see * . */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which * is part of the GnuPG project). Use the included Makefile to build * the binary. * * Report bugs to: seccure AT point-at-infinity.org * */ #ifndef INC_ECC_H #define INC_ECC_H #include struct affine_point { gcry_mpi_t x, y; }; struct jacobian_point { gcry_mpi_t x, y, z; }; struct domain_params { gcry_mpi_t a, b, m, order; struct affine_point base; int cofactor; }; struct affine_point point_new(void); void point_release(struct affine_point *p); void point_set(struct affine_point *p1, const struct affine_point *p2); void point_load_zero(struct affine_point *p); int point_is_zero(const struct affine_point *p); int point_on_curve(const struct affine_point *p, const struct domain_params *dp); int point_compress(const struct affine_point *p); int point_decompress(struct affine_point *p, const gcry_mpi_t x, int yflag, const struct domain_params *dp); void point_double(struct affine_point *p, const struct domain_params *dp); void point_add(struct affine_point *p1, const struct affine_point *p2, const struct domain_params *dp); struct jacobian_point jacobian_new(void); void jacobian_release(struct jacobian_point *p); void jacobian_load_affine(struct jacobian_point *p1, const struct affine_point *p2); void jacobian_load_zero(struct jacobian_point *p); int jacobian_is_zero(const struct jacobian_point *p); void jacobian_double(struct jacobian_point *p, const struct domain_params *dp); void jacobian_affine_point_add(struct jacobian_point *p1, const struct affine_point *p2, const struct domain_params *dp); struct affine_point jacobian_to_affine(const struct jacobian_point *p, const struct domain_params *dp); struct affine_point pointmul(const struct affine_point *p, const gcry_mpi_t exp, const struct domain_params *dp); int embedded_key_validation(const struct affine_point *p, const struct domain_params *dp); int full_key_validation(const struct affine_point *p, const struct domain_params *dp); #endif /* INC_ECC_H */ seccure-0.5/serialize.h0000644000175000017500000000331312372135467015033 0ustar bertrambertram/* * seccure - Copyright 2014 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program. If not, see * . */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which * is part of the GnuPG project). Use the included Makefile to build * the binary. * * Report bugs to: seccure AT point-at-infinity.org * */ #ifndef INC_SERIALIZE_H #define INC_SERIALIZE_H #include enum disp_format { DF_BIN, DF_COMPACT }; #define COMPACT_DIGITS_COUNT 90 extern const char compact_digits[]; size_t get_serialization_len(const gcry_mpi_t x, enum disp_format df); void serialize_mpi(char *outbuf, size_t outlen, enum disp_format df, const gcry_mpi_t x); int deserialize_mpi(gcry_mpi_t *x, enum disp_format ds, const char *buf, size_t inlen); #endif /* INC_SERIALIZE_H */ seccure-0.5/test/0000755000175000017500000000000012372135467013652 5ustar bertrambertramseccure-0.5/test/Makefile0000644000175000017500000000340712372135467015316 0ustar bertrambertramSECCURE-PATH = "../" SECCURE-KEY = $(SECCURE-PATH)"seccure-key" SECCURE-ENCRYPT = $(SECCURE-PATH)"seccure-encrypt" SECCURE-DECRYPT = $(SECCURE-PATH)"seccure-decrypt" SECCURE-SIGN = $(SECCURE-PATH)"seccure-sign" SECCURE-VERIFY = $(SECCURE-PATH)"seccure-verify" SECCURE-SIGNCRYPT = $(SECCURE-PATH)"seccure-signcrypt" SECCURE-VERIDEC = $(SECCURE-PATH)"seccure-veridec" ENCCURVE = "brainpoolp192r1" SIGCURVE = "brainpoolp512r1" MACLEN = "64" default: encdec-test signveri-test signcrypt-test clean: rm -f public-encryption-key public-signature-key message.enc message.aux message.sig rebuild: clean default public-encryption-key: secret-encryption-key $(SECCURE-KEY) -c $(ENCCURVE) -F secret-encryption-key -q > public-encryption-key public-signature-key: secret-signature-key $(SECCURE-KEY) -c $(SIGCURVE) -F secret-signature-key -q > public-signature-key encdec-test: public-encryption-key $(SECCURE-ENCRYPT) -m $(MACLEN) -c $(ENCCURVE) -i message.txt -o message.enc -- `cat public-encryption-key` $(SECCURE-DECRYPT) -m $(MACLEN) -c $(ENCCURVE) -i message.enc -o message.aux -F secret-encryption-key cmp message.txt message.aux rm -f message.enc message.aux signveri-test: public-signature-key $(SECCURE-SIGN) -c $(SIGCURVE) -s message.sig -i message.txt -F secret-signature-key $(SECCURE-VERIFY) -c $(SIGCURVE) -s message.sig -i message.txt -- `cat public-signature-key` rm -f message.sig signcrypt-test: public-encryption-key public-signature-key $(SECCURE-SIGNCRYPT) -c $(SIGCURVE) -c $(ENCCURVE) -i message.txt -o message.enc -F secret-signature-key -- `cat public-encryption-key` $(SECCURE-VERIDEC) -c $(ENCCURVE) -c $(SIGCURVE) -i message.enc -o message.aux -F secret-encryption-key -- `cat public-signature-key` cmp message.txt message.aux rm -f message.enc message.aux seccure-0.5/test/secret-encryption-key0000644000175000017500000000000412372135467020032 0ustar bertrambertramfoo seccure-0.5/test/secret-signature-key0000644000175000017500000000000412372135467017641 0ustar bertrambertrambar seccure-0.5/test/message.txt0000644000175000017500000000005412372135467016036 0ustar bertrambertramThe quick brown fox jumps over the lazy dog seccure-0.5/Makefile0000644000175000017500000000447512372135467014345 0ustar bertrambertramCFLAGS = -O2 -Wall -Wextra -std=c89 -pedantic -D_POSIX_C_SOURCE=200112 # -D NOBEEP LIBS = -lgcrypt default: binaries # doc binaries: seccure-key seccure-encrypt seccure-decrypt seccure-sign \ seccure-verify seccure-signcrypt seccure-veridec seccure-dh doc: seccure.1 seccure.1.html install: default install -m0755 seccure-key $(DESTDIR)/usr/bin ln -f $(DESTDIR)/usr/bin/seccure-key $(DESTDIR)/usr/bin/seccure-encrypt ln -f $(DESTDIR)/usr/bin/seccure-key $(DESTDIR)/usr/bin/seccure-decrypt ln -f $(DESTDIR)/usr/bin/seccure-key $(DESTDIR)/usr/bin/seccure-sign ln -f $(DESTDIR)/usr/bin/seccure-key $(DESTDIR)/usr/bin/seccure-verify ln -f $(DESTDIR)/usr/bin/seccure-key $(DESTDIR)/usr/bin/seccure-signcrypt ln -f $(DESTDIR)/usr/bin/seccure-key $(DESTDIR)/usr/bin/seccure-veridec ln -f $(DESTDIR)/usr/bin/seccure-key $(DESTDIR)/usr/bin/seccure-dh install -m0644 seccure.1 $(DESTDIR)/usr/share/man/man1 clean: rm -f *.o *~ seccure-key seccure-encrypt seccure-decrypt seccure-sign \ seccure-verify seccure-signcrypt seccure-veridec \ seccure-dh # seccure.1 seccure.1.html rebuild: clean default seccure-key: seccure.o numtheory.o ecc.o serialize.o protocol.o curves.o aes256ctr.o $(CC) $(CFLAGS) -o seccure-key seccure.o numtheory.o ecc.o \ curves.o serialize.o protocol.o aes256ctr.o $(LIBS) strip seccure-key seccure-encrypt: seccure-key ln -f seccure-key seccure-encrypt seccure-decrypt: seccure-key ln -f seccure-key seccure-decrypt seccure-sign: seccure-key ln -f seccure-key seccure-sign seccure-verify: seccure-key ln -f seccure-key seccure-verify seccure-signcrypt: seccure-key ln -f seccure-key seccure-signcrypt seccure-veridec: seccure-key ln -f seccure-key seccure-veridec seccure-dh: seccure-key ln -f seccure-key seccure-dh seccure.o: seccure.c $(CC) $(CFLAGS) -c seccure.c numtheory.o: numtheory.c numtheory.h $(CC) $(CFLAGS) -c numtheory.c ecc.o: ecc.c ecc.h $(CC) $(CFLAGS) -c ecc.c curves.o: curves.c curves.h $(CC) $(CFLAGS) -c curves.c serialize.o: serialize.c serialize.h $(CC) $(CFLAGS) -c serialize.c protocol.o: protocol.c protocol.h $(CC) $(CFLAGS) -c protocol.c aes256ctr.o: aes256ctr.c aes256ctr.h $(CC) $(CFLAGS) -c aes256ctr.c seccure.1: seccure.manpage.xml xmltoman seccure.manpage.xml > seccure.1 seccure.1.html: seccure.manpage.xml xmlmantohtml seccure.manpage.xml > seccure.1.html seccure-0.5/PATENTS0000644000175000017500000000163212372135467013736 0ustar bertrambertram SECCURE Elliptic Curve Crypto Utility for Reliable Encryption http://point-at-infinity.org/seccure/ seccure builds on a bunch of cryptographic algorithms and techniques (a comprehensive list is presented below). To my best knowledge none of these and in particular no part of seccure is covered by patents. - ECIES, ECDSA, ECDH - AES256, SHA256, SHA512, CTR Mode, HMAC Mode - Point compression on elliptic curves over prime fields - Point multiplication using Double-And-Add-Algorithm (may be replaced by a NAF-Algorithm in the future) Note: Diffie-Hellman key agreement actually has been patented in 1977. The patent expired regularly in 1997. Note: some of the supported elliptic curves are defined over prime fields with moduli of "Crandall type" (which may be patented). Seccure doesn't make use of the special structure these primes offer and therefore doesn't touch the Crandall patents. seccure-0.5/protocol.h0000644000175000017500000000503712372135467014712 0ustar bertrambertram/* * seccure - Copyright 2014 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program. If not, see * . */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which * is part of the GnuPG project). Use the included Makefile to build * the binary. * * Report bugs to: seccure AT point-at-infinity.org * */ #ifndef INC_PROTOCOL_H #define INC_PROTOCOL_H #include "curves.h" #include "serialize.h" gcry_mpi_t buf_to_exponent(const char *buf, size_t buflen, const struct curve_params *cp); gcry_mpi_t hash_to_exponent(const char *hash, const struct curve_params *cp); gcry_mpi_t get_random_exponent(const struct curve_params *cp); void compress_to_string(char *buf, enum disp_format df, const struct affine_point *P, const struct curve_params *cp); int decompress_from_string(struct affine_point *P, const char *buf, enum disp_format df, const struct curve_params *cp); gcry_mpi_t ECDSA_sign(const char *msg, const gcry_mpi_t d, const struct curve_params *cp); int ECDSA_verify(const char *msg, const struct affine_point *Q, const gcry_mpi_t sig, const struct curve_params *cp); struct affine_point ECIES_encapsulation(char *key, const struct affine_point *Q, const struct curve_params *cp); int ECIES_decapsulation(char *key, const struct affine_point *R, const gcry_mpi_t d, const struct curve_params *cp); gcry_mpi_t DH_step1(struct affine_point *A, const struct curve_params *cp); int DH_step2(char *key, const struct affine_point *B, const gcry_mpi_t exp, const struct curve_params *cp); #endif /* INC_PROTOCOL_H */ seccure-0.5/aes256ctr.h0000644000175000017500000000370412372135467014566 0ustar bertrambertram/* * seccure - Copyright 2014 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program. If not, see * . */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which * is part of the GnuPG project). Use the included Makefile to build * the binary. * * Report bugs to: seccure AT point-at-infinity.org * */ #ifndef INC_AES256CTR_H #define INC_AES256CTR_H #include #define CIPHER_BLOCK_SIZE 16 #define CIPHER_KEY_SIZE 32 #define HMAC_KEY_SIZE 32 struct aes256ctr { gcry_cipher_hd_t ch; int idx; char buf[CIPHER_BLOCK_SIZE]; }; struct aes256ctr* aes256ctr_init(const char *key); void aes256ctr_enc(struct aes256ctr *ac, char *buf, size_t len); #define aes256ctr_dec aes256ctr_enc void aes256ctr_done(struct aes256ctr *ac); int hmacsha256_init(gcry_md_hd_t *mh, const char *key, size_t len); #define aes256cprng aes256ctr #define aes256cprng_init aes256ctr_init void aes256cprng_fillbuf(struct aes256cprng *cprng, char *buf, size_t len); #define aes256cprng_done aes256ctr_done #endif /* INC_AES256CTR_H */ seccure-0.5/seccure.1.html0000664000175000017500000002131112372135467015351 0ustar bertrambertram

seccure

SECCURE Elliptic Curve Crypto Utility for Reliable Encryption

Synopsis

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

Description

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

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

Commands

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

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

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

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

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

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

seccure-veridec: Counterpart to signcryption.

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

Options

-c curve

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

Note: If a public key is given on the command line, for all SECP and NIST curves seccure can determine the corresponding curve on its own. It is then unnecessary to specify the curve explicitly. Brainpool curves cannot be recognized automatically.

-F pwfile

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

-m maclen

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

-i infile

Read from infile instead of STDIN.

-o outfile

Write to outfile instead of STDOUT.

-s sigfile

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

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

-f

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

-b

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

-a

Append mode:

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

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

-d

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

-v

Verbose mode: Print some extra information.

-q

Quiet mode: Disable all unnecessary output.

Exit Status

All commands in the seccure software suite exit with a status of zero if the desired operation could be completed successfully. Any error leads to a nonzero exit code.

Example

Given the passphrase 'seccure is secure', run

seccure-key

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

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

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

The message can be recovered with

seccure-decrypt -i document.enc

To sign the file run

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

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

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

Key establishment

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

How to choose the curve

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

Algorithms

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

Author

This software (v0.5) was written by B. Poettering (seccure AT point-at-infinity.org) in 2006-2014. It is released under the terms of the GNU Lesser General Public License (LGPLv3). Find the latest version of seccure on the project's homepage: http://point-at-infinity.org/seccure/.
seccure-0.5/README0000644000175000017500000000112312372135467013550 0ustar bertrambertram SECCURE Elliptic Curve Crypto Utility for Reliable Encryption http://point-at-infinity.org/seccure/ The SECCURE toolset implements a selection of asymmetric algorithms based on elliptic curve cryptography (ECC). See the manpage or the project's homepage for further details. SECCURE is released under the GNU Lesser General Public License (LGPLv3). The code links against the GNU gcrypt library "libgcrypt" (which is part of the GnuPG project). Use the included Makefile to build the binary. Report bugs to: seccure AT point-at-infinity.org Copyright 2014 B. Poettering seccure-0.5/curves.c0000644000175000017500000003055012372135467014351 0ustar bertrambertram/* * seccure - Copyright 2014 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program. If not, see * . */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which * is part of the GnuPG project). Use the included Makefile to build * the binary. * * Report bugs to: seccure AT point-at-infinity.org * */ #include #include #include #include #include "curves.h" #include "ecc.h" #include "serialize.h" /******************************************************************************/ #define CURVE_NUM 15 struct curve { const char *name, *a, *b, *m, *base_x, *base_y, *order; int cofactor; size_t pk_len_compact; }; static const struct curve curves[CURVE_NUM] = { { "secp112r1", "db7c2abf62e35e668076bead2088", "659ef8ba043916eede8911702b22", "db7c2abf62e35e668076bead208b", "09487239995a5ee76b55f9c2f098", "a89ce5af8724c0a23e0e0ff77500", "db7c2abf62e35e7628dfac6561c5", 1, 18 }, { "secp128r1", "fffffffdfffffffffffffffffffffffc", "e87579c11079f43dd824993c2cee5ed3", "fffffffdffffffffffffffffffffffff", "161ff7528b899b2d0c28607ca52c5b86", "cf5ac8395bafeb13c02da292dded7a83", "fffffffe0000000075a30d1b9038a115", 1, 20 }, { "secp160r1", "ffffffffffffffffffffffffffffffff7ffffffc", "1c97befc54bd7a8b65acf89f81d4d4adc565fa45", "ffffffffffffffffffffffffffffffff7fffffff", "4a96b5688ef573284664698968c38bb913cbfc82", "23a628553168947d59dcc912042351377ac5fb32", "0100000000000000000001f4c8f927aed3ca752257", 1, 25 }, { "secp192r1/nistp192", "fffffffffffffffffffffffffffffffefffffffffffffffc", "64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1", "fffffffffffffffffffffffffffffffeffffffffffffffff", "188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012", "07192b95ffc8da78631011ed6b24cdd573f977a11e794811", "ffffffffffffffffffffffff99def836146bc9b1b4d22831", 1, 30 }, { "secp224r1/nistp224", "fffffffffffffffffffffffffffffffefffffffffffffffffffffffe", "b4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4", "ffffffffffffffffffffffffffffffff000000000000000000000001", "b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21", "bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34", "ffffffffffffffffffffffffffff16a2e0b8f03e13dd29455c5c2a3d", 1, 35 }, { "secp256r1/nistp256", "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc", "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b", "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff", "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296", "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5", "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551", 1, 40 }, { "secp384r1/nistp384", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000fffffffc", "b3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff", "aa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7", "3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f", "ffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973", 1, 60 }, { "secp521r1/nistp521", "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc", "051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00", "1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66", "11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650", "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409", 1, 81 }, { "brainpoolp160r1", "340e7be2a280eb74e2be61bada745d97e8f7c300", "1e589a8595423412134faa2dbdec95c8d8675e58", "e95e4a5f737059dc60dfc7ad95b3d8139515620f", "bed5af16ea3f6a4f62938c4631eb5af7bdbcdbc3", "1667cb477a1a8ec338f94741669c976316da6321", "e95e4a5f737059dc60df5991d45029409e60fc09", 1, 25 }, { "brainpoolp192r1", "6a91174076b1e0e19c39c031fe8685c1cae040e5c69a28ef", "469a28ef7c28cca3dc721d044f4496bcca7ef4146fbf25c9", "c302f41d932a36cda7a3463093d18db78fce476de1a86297", "c0a0647eaab6a48753b033c56cb0f0900a2f5c4853375fd6", "14b690866abd5bb88b5f4828c1490002e6773fa2fa299b8f", "c302f41d932a36cda7a3462f9e9e916b5be8f1029ac4acc1", 1, 30 }, { "brainpoolp224r1", "68a5e62ca9ce6c1c299803a6c1530b514e182ad8b0042a59cad29f43", "2580f63ccfe44138870713b1a92369e33e2135d266dbb372386c400b", "d7c134aa264366862a18302575d1d787b09f075797da89f57ec8c0ff", "0d9029ad2c7e5cf4340823b2a87dc68c9e4ce3174c1e6efdee12c07d", "58aa56f772c0726f24c6b89e4ecdac24354b9e99caa3f6d3761402cd", "d7c134aa264366862a18302575d0fb98d116bc4b6ddebca3a5a7939f", 1, 35 }, { "brainpoolp256r1", "7d5a0975fc2c3057eef67530417affe7fb8055c126dc5c6ce94a4b44f330b5d9", "26dc5c6ce94a4b44f330b5d9bbd77cbf958416295cf7e1ce6bccdc18ff8c07b6", "a9fb57dba1eea9bc3e660a909d838d726e3bf623d52620282013481d1f6e5377", "8bd2aeb9cb7e57cb2c4b482ffc81b7afb9de27e1e3bd23c23a4453bd9ace3262", "547ef835c3dac4fd97f8461a14611dc9c27745132ded8e545c1d54c72f046997", "a9fb57dba1eea9bc3e660a909d838d718c397aa3b561a6f7901e0e82974856a7", 1, 40 }, { "brainpoolp320r1", "3ee30b568fbab0f883ccebd46d3f3bb8a2a73513f5eb79da66190eb085ffa9f492f375a97d860eb4", "520883949dfdbc42d3ad198640688a6fe13f41349554b49acc31dccd884539816f5eb4ac8fb1f1a6", "d35e472036bc4fb7e13c785ed201e065f98fcfa6f6f40def4f92b9ec7893ec28fcd412b1f1b32e27", "43bd7e9afb53d8b85289bcc48ee5bfe6f20137d10a087eb6e7871e2a10a599c710af8d0d39e20611", "14fdd05545ec1cc8ab4093247f77275e0743ffed117182eaa9c77877aaac6ac7d35245d1692e8ee1", "d35e472036bc4fb7e13c785ed201e065f98fcfa5b68f12a32d482ec7ee8658e98691555b44c59311", 1, 50 }, { "brainpoolp384r1", "7bc382c63d8c150c3c72080ace05afa0c2bea28e4fb22787139165efba91f90f8aa5814a503ad4eb04a8c7dd22ce2826", "04a8c7dd22ce28268b39b55416f0447c2fb77de107dcd2a62e880ea53eeb62d57cb4390295dbc9943ab78696fa504c11", "8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b412b1da197fb71123acd3a729901d1a71874700133107ec53", "1d1c64f068cf45ffa2a63a81b7c13f6b8847a3e77ef14fe3db7fcafe0cbd10e8e826e03436d646aaef87b2e247d4af1e", "8abe1d7520f9c2a45cb1eb8e95cfd55262b70b29feec5864e19c054ff99129280e4646217791811142820341263c5315", "8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b31f166e6cac0425a7cf3ab6af6b7fc3103b883202e9046565", 1, 60 }, { "brainpoolp512r1", "7830a3318b603b89e2327145ac234cc594cbdd8d3df91610a83441caea9863bc2ded5d5aa8253aa10a2ef1c98b9ac8b57f1117a72bf2c7b9e7c1ac4d77fc94ca", "3df91610a83441caea9863bc2ded5d5aa8253aa10a2ef1c98b9ac8b57f1117a72bf2c7b9e7c1ac4d77fc94cadc083e67984050b75ebae5dd2809bd638016f723", "aadd9db8dbe9c48b3fd4e6ae33c9fc07cb308db3b3c9d20ed6639cca703308717d4d9b009bc66842aecda12ae6a380e62881ff2f2d82c68528aa6056583a48f3", "81aee4bdd82ed9645a21322e9c4c6a9385ed9f70b5d916c1b43b62eef4d0098eff3b1f78e2d0d48d50d1687b93b97d5f7c6d5047406a5e688b352209bcb9f822", "7dde385d566332ecc0eabfa9cf7822fdf209f70024a57b1aa000c55b881f8111b2dcde494a5f485e5bca4bd88a2763aed1ca2b2fa8f0540678cd1e0f3ad80892", "aadd9db8dbe9c48b3fd4e6ae33c9fc07cb308db3b3c9d20ed6639cca70330870553e5c414ca92619418661197fac10471db1d381085ddaddb58796829ca90069", 1, 79 }, }; /******************************************************************************/ #define SCAN(x, s) do { \ assert(! gcry_mpi_scan(x, GCRYMPI_FMT_HEX, s, 0, NULL)); \ gcry_mpi_set_flag(*x, GCRYMPI_FLAG_SECURE); \ } while(0) static struct curve_params* load_curve(const struct curve *c) { gcry_mpi_t h; struct curve_params *cp; struct domain_params *dp; if (! (cp = malloc(sizeof(struct curve_params)))) return NULL; cp->name = c->name; dp = &cp->dp; SCAN(&dp->a, c->a); SCAN(&dp->b, c->b); SCAN(&dp->m, c->m); SCAN(&dp->order, c->order); SCAN(&dp->base.x, c->base_x); SCAN(&dp->base.y, c->base_y); dp->cofactor = c->cofactor; h = gcry_mpi_new(0); gcry_mpi_add(h, dp->m, dp->m); gcry_mpi_sub_ui(h, h, 1); cp->pk_len_bin = get_serialization_len(h, DF_BIN); cp->pk_len_compact = get_serialization_len(h, DF_COMPACT); gcry_mpi_mul(h, dp->order, dp->order); gcry_mpi_sub_ui(h, h, 1); cp->sig_len_bin = get_serialization_len(h, DF_BIN); cp->sig_len_compact = get_serialization_len(h, DF_COMPACT); cp->dh_len_bin = (gcry_mpi_get_nbits(dp->order) / 2 + 7) / 8; if (cp->dh_len_bin > 32) cp->dh_len_bin = 32; gcry_mpi_set_ui(h, 0); gcry_mpi_set_bit(h, 8 * cp->dh_len_bin); gcry_mpi_sub_ui(h, h, 1); cp->dh_len_compact = get_serialization_len(h, DF_COMPACT); cp->elem_len_bin = get_serialization_len(dp->m, DF_BIN); cp->order_len_bin = get_serialization_len(dp->order, DF_BIN); #if 0 /* enable this when adding a new curve to do some sanity checks */ do { struct affine_point p; if (! gcry_mpi_cmp_ui(dp->b, 0)) { fprintf(stderr, "FATAL: b == 0\n"); exit(1); } if (cp->pk_len_compact != c->pk_len_compact) { fprintf(stderr, "FATAL: c->pk_len_compact != %zd\n", cp->pk_len_compact); exit(1); } if (! point_on_curve(&dp->base, dp)) { fprintf(stderr, "FATAL: base point not on curve!\n"); exit(1); } p = pointmul(&dp->base, dp->order, dp); if (! point_is_zero(&p)) { fprintf(stderr, "FATAL: wrong point order!\n"); exit(1); } point_release(&p); gcry_mpi_mul_ui(h, dp->order, dp->cofactor); gcry_mpi_sub(h, h, dp->m); gcry_mpi_sub_ui(h, h, 1); gcry_mpi_mul(h, h, h); gcry_mpi_rshift(h, h, 2); if (gcry_mpi_cmp(h, dp->m) > 0) { fprintf(stderr, "FATAL: invalid cofactor!\n"); exit(1); } } while(0); #endif assert(cp->pk_len_bin <= MAX_PK_LEN_BIN); assert(cp->pk_len_compact <= MAX_PK_LEN_COMPACT); assert(cp->sig_len_bin <= MAX_SIG_LEN_BIN); assert(cp->sig_len_compact <= MAX_SIG_LEN_COMPACT); assert(cp->dh_len_bin <= MAX_DH_LEN_BIN); assert(cp->dh_len_compact <= MAX_DH_LEN_COMPACT); assert(cp->elem_len_bin <= MAX_ELEM_LEN_BIN); assert(cp->order_len_bin <= MAX_ORDER_LEN_BIN); gcry_mpi_release(h); return cp; } struct curve_params* curve_by_name(const char *name) { const struct curve *c = curves; int i; for(i = 0; i < CURVE_NUM; i++, c++) if (strstr(c->name, name)) return load_curve(c); return NULL; } struct curve_params* curve_by_pk_len_compact(size_t len) { const struct curve *c = curves; int i; for(i = 0; i < CURVE_NUM; i++, c++) if (c->pk_len_compact == len) return load_curve(c); return NULL; } void curve_release(struct curve_params *cp) { struct domain_params *dp = &cp->dp; gcry_mpi_release(dp->a); gcry_mpi_release(dp->b); gcry_mpi_release(dp->m); gcry_mpi_release(dp->order); gcry_mpi_release(dp->base.x); gcry_mpi_release(dp->base.y); free(cp); } seccure-0.5/ecc.c0000644000175000017500000002540612372135467013600 0ustar bertrambertram/* * seccure - Copyright 2014 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program. If not, see * . */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which * is part of the GnuPG project). Use the included Makefile to build * the binary. * * Report bugs to: seccure AT point-at-infinity.org * */ #include #include #include "ecc.h" #include "numtheory.h" /******************************************************************************/ /* Chapter 3.1.2 in the "Guide to Elliptic Curve Cryptography" */ struct affine_point point_new(void) { struct affine_point r; r.x = gcry_mpi_snew(0); r.y = gcry_mpi_snew(0); return r; } void point_release(struct affine_point *p) { gcry_mpi_release(p->x); gcry_mpi_release(p->y); } void point_set(struct affine_point *p1, const struct affine_point *p2) { gcry_mpi_set(p1->x, p2->x); gcry_mpi_set(p1->y, p2->y); } void point_load_zero(struct affine_point *p) { gcry_mpi_set_ui(p->x, 0); gcry_mpi_set_ui(p->y, 0); } int point_is_zero(const struct affine_point *p) { return ! gcry_mpi_cmp_ui(p->x, 0) && ! gcry_mpi_cmp_ui(p->y, 0); } int point_on_curve(const struct affine_point *p, const struct domain_params *dp) { int res; if (! (res = point_is_zero(p))) { gcry_mpi_t h1, h2; h1 = gcry_mpi_snew(0); h2 = gcry_mpi_snew(0); gcry_mpi_mulm(h1, p->x, p->x, dp->m); gcry_mpi_addm(h1, h1, dp->a, dp->m); gcry_mpi_mulm(h1, h1, p->x, dp->m); gcry_mpi_addm(h1, h1, dp->b, dp->m); gcry_mpi_mulm(h2, p->y, p->y, dp->m); res = ! gcry_mpi_cmp(h1, h2); gcry_mpi_release(h1); gcry_mpi_release(h2); } return res; } int point_compress(const struct affine_point *p) { return gcry_mpi_test_bit(p->y, 0); } int point_decompress(struct affine_point *p, const gcry_mpi_t x, int yflag, const struct domain_params *dp) { gcry_mpi_t h, y; int res; h = gcry_mpi_snew(0); y = gcry_mpi_snew(0); gcry_mpi_mulm(h, x, x, dp->m); gcry_mpi_addm(h, h, dp->a, dp->m); gcry_mpi_mulm(h, h, x, dp->m); gcry_mpi_addm(h, h, dp->b, dp->m); if ((res = mod_root(y, h, dp->m))) if ((res = (gcry_mpi_cmp_ui(y, 0) || ! yflag))) { p->x = gcry_mpi_snew(0); p->y = gcry_mpi_snew(0); gcry_mpi_set(p->x, x); if (gcry_mpi_test_bit(y, 0) == yflag) gcry_mpi_set(p->y, y); else gcry_mpi_sub(p->y, dp->m, y); assert(point_on_curve(p, dp)); } gcry_mpi_release(h); gcry_mpi_release(y); return res; } void point_double(struct affine_point *p, const struct domain_params *dp) { if (gcry_mpi_cmp_ui(p->y, 0)) { gcry_mpi_t t1, t2; t1 = gcry_mpi_snew(0); t2 = gcry_mpi_snew(0); gcry_mpi_mulm(t2, p->x, p->x, dp->m); gcry_mpi_addm(t1, t2, t2, dp->m); gcry_mpi_addm(t1, t1, t2, dp->m); gcry_mpi_addm(t1, t1, dp->a, dp->m); gcry_mpi_addm(t2, p->y, p->y, dp->m); gcry_mpi_invm(t2, t2, dp->m); gcry_mpi_mulm(t1, t1, t2, dp->m); gcry_mpi_mulm(t2, t1, t1, dp->m); gcry_mpi_subm(t2, t2, p->x, dp->m); gcry_mpi_subm(t2, t2, p->x, dp->m); gcry_mpi_subm(p->x, p->x, t2, dp->m); gcry_mpi_mulm(t1, t1, p->x, dp->m); gcry_mpi_subm(p->y, t1, p->y, dp->m); gcry_mpi_set(p->x, t2); gcry_mpi_release(t1); gcry_mpi_release(t2); } else gcry_mpi_set_ui(p->x, 0); } void point_add(struct affine_point *p1, const struct affine_point *p2, const struct domain_params *dp) { if (! point_is_zero(p2)) { if (! point_is_zero(p1)) { if (! gcry_mpi_cmp(p1->x, p2->x)) { if (! gcry_mpi_cmp(p1->y, p2->y)) point_double(p1, dp); else point_load_zero(p1); } else { gcry_mpi_t t; t = gcry_mpi_snew(0); gcry_mpi_subm(t, p1->y, p2->y, dp->m); gcry_mpi_subm(p1->y, p1->x, p2->x, dp->m); gcry_mpi_invm(p1->y, p1->y, dp->m); gcry_mpi_mulm(p1->y, t, p1->y, dp->m); gcry_mpi_mulm(t, p1->y, p1->y, dp->m); gcry_mpi_addm(p1->x, p1->x, p2->x, dp->m); gcry_mpi_subm(p1->x, t, p1->x, dp->m); gcry_mpi_subm(t, p2->x, p1->x, dp->m); gcry_mpi_mulm(p1->y, p1->y, t, dp->m); gcry_mpi_subm(p1->y, p1->y, p2->y, dp->m); gcry_mpi_release(t); } } else point_set(p1, p2); } } /******************************************************************************/ /* Chapter 3.2.2 in the "Guide to Elliptic Curve Cryptography" */ struct jacobian_point jacobian_new(void) { struct jacobian_point r; r.x = gcry_mpi_snew(0); r.y = gcry_mpi_snew(0); r.z = gcry_mpi_snew(0); return r; } void jacobian_release(struct jacobian_point *p) { gcry_mpi_release(p->x); gcry_mpi_release(p->y); gcry_mpi_release(p->z); } void jacobian_load_affine(struct jacobian_point *p1, const struct affine_point *p2) { if (! point_is_zero(p2)) { gcry_mpi_set(p1->x, p2->x); gcry_mpi_set(p1->y, p2->y); gcry_mpi_set_ui(p1->z, 1); } else gcry_mpi_set_ui(p1->z, 0); } void jacobian_load_zero(struct jacobian_point *p) { gcry_mpi_set_ui(p->z, 0); } int jacobian_is_zero(const struct jacobian_point *p) { return ! gcry_mpi_cmp_ui(p->z, 0); } void jacobian_double(struct jacobian_point *p, const struct domain_params *dp) { if (gcry_mpi_cmp_ui(p->z, 0)) { if (gcry_mpi_cmp_ui(p->y, 0)) { gcry_mpi_t t1, t2; t1 = gcry_mpi_snew(0); t2 = gcry_mpi_snew(0); gcry_mpi_mulm(t1, p->x, p->x, dp->m); gcry_mpi_addm(t2, t1, t1, dp->m); gcry_mpi_addm(t2, t2, t1, dp->m); gcry_mpi_mulm(t1, p->z, p->z, dp->m); gcry_mpi_mulm(t1, t1, t1, dp->m); gcry_mpi_mulm(t1, t1, dp->a, dp->m); gcry_mpi_addm(t1, t1, t2, dp->m); gcry_mpi_mulm(p->z, p->z, p->y, dp->m); gcry_mpi_addm(p->z, p->z, p->z, dp->m); gcry_mpi_mulm(p->y, p->y, p->y, dp->m); gcry_mpi_addm(p->y, p->y, p->y, dp->m); gcry_mpi_mulm(t2, p->x, p->y, dp->m); gcry_mpi_addm(t2, t2, t2, dp->m); gcry_mpi_mulm(p->x, t1, t1, dp->m); gcry_mpi_subm(p->x, p->x, t2, dp->m); gcry_mpi_subm(p->x, p->x, t2, dp->m); gcry_mpi_subm(t2, t2, p->x, dp->m); gcry_mpi_mulm(t1, t1, t2, dp->m); gcry_mpi_mulm(t2, p->y, p->y, dp->m); gcry_mpi_addm(t2, t2, t2, dp->m); gcry_mpi_subm(p->y, t1, t2, dp->m); gcry_mpi_release(t1); gcry_mpi_release(t2); } else gcry_mpi_set_ui(p->z, 0); } } void jacobian_affine_point_add(struct jacobian_point *p1, const struct affine_point *p2, const struct domain_params *dp) { if (! point_is_zero(p2)) { if (gcry_mpi_cmp_ui(p1->z, 0)) { gcry_mpi_t t1, t2, t3; t1 = gcry_mpi_snew(0); t2 = gcry_mpi_snew(0); gcry_mpi_mulm(t1, p1->z, p1->z, dp->m); gcry_mpi_mulm(t2, t1, p2->x, dp->m); gcry_mpi_mulm(t1, t1, p1->z, dp->m); gcry_mpi_mulm(t1, t1, p2->y, dp->m); if (! gcry_mpi_cmp(p1->x, t2)) { if (! gcry_mpi_cmp(p1->y, t1)) jacobian_double(p1, dp); else jacobian_load_zero(p1); } else { t3 = gcry_mpi_snew(0); gcry_mpi_subm(p1->x, p1->x, t2, dp->m); gcry_mpi_subm(p1->y, p1->y, t1, dp->m); gcry_mpi_mulm(p1->z, p1->z, p1->x, dp->m); gcry_mpi_mulm(t3, p1->x, p1->x, dp->m); gcry_mpi_mulm(t2, t2, t3, dp->m); gcry_mpi_mulm(t3, t3, p1->x, dp->m); gcry_mpi_mulm(t1, t1, t3, dp->m); gcry_mpi_mulm(p1->x, p1->y, p1->y, dp->m); gcry_mpi_subm(p1->x, p1->x, t3, dp->m); gcry_mpi_subm(p1->x, p1->x, t2, dp->m); gcry_mpi_subm(p1->x, p1->x, t2, dp->m); gcry_mpi_subm(t2, t2, p1->x, dp->m); gcry_mpi_mulm(p1->y, p1->y, t2, dp->m); gcry_mpi_subm(p1->y, p1->y, t1, dp->m); gcry_mpi_release(t3); } gcry_mpi_release(t1); gcry_mpi_release(t2); } else jacobian_load_affine(p1, p2); } } struct affine_point jacobian_to_affine(const struct jacobian_point *p, const struct domain_params *dp) { struct affine_point r = point_new(); if (gcry_mpi_cmp_ui(p->z, 0)) { gcry_mpi_t h; h = gcry_mpi_snew(0); gcry_mpi_invm(h, p->z, dp->m); gcry_mpi_mulm(r.y, h, h, dp->m); gcry_mpi_mulm(r.x, p->x, r.y, dp->m); gcry_mpi_mulm(r.y, r.y, h, dp->m); gcry_mpi_mulm(r.y, r.y, p->y, dp->m); gcry_mpi_release(h); } return r; } /******************************************************************************/ /* Algorithm 3.27 in the "Guide to Elliptic Curve Cryptography" */ #if 0 struct affine_point pointmul(const struct affine_point *p, const gcry_mpi_t exp, const struct domain_params *dp) { struct affine_point r = point_new(); int n = gcry_mpi_get_nbits(exp); while (n) { point_double(&r, dp); if (gcry_mpi_test_bit(exp, --n)) point_add(&r, p, dp); } assert(point_on_curve(&r, dp)); return r; } #else struct affine_point pointmul(const struct affine_point *p, const gcry_mpi_t exp, const struct domain_params *dp) { struct jacobian_point r = jacobian_new(); struct affine_point R; int n = gcry_mpi_get_nbits(exp); while (n) { jacobian_double(&r, dp); if (gcry_mpi_test_bit(exp, --n)) jacobian_affine_point_add(&r, p, dp); } R = jacobian_to_affine(&r, dp); jacobian_release(&r); assert(point_on_curve(&R, dp)); return R; } #endif /******************************************************************************/ /* Algorithm 4.26 in the "Guide to Elliptic Curve Cryptography" */ int embedded_key_validation(const struct affine_point *p, const struct domain_params *dp) { if (gcry_mpi_cmp_ui(p->x, 0) < 0 || gcry_mpi_cmp(p->x, dp->m) >= 0 || gcry_mpi_cmp_ui(p->y, 0) < 0 || gcry_mpi_cmp(p->y, dp->m) >= 0) return 0; return ! point_is_zero(p) && point_on_curve(p, dp); } /* Algorithm 4.25 in the "Guide to Elliptic Curve Cryptography" */ int full_key_validation(const struct affine_point *p, const struct domain_params *dp) { if (! embedded_key_validation(p, dp)) return 0; if (dp->cofactor != 1) { struct affine_point bp; int res; bp = pointmul(p, dp->order, dp); res = point_is_zero(&bp); point_release(&bp); return res; } else return 1; } seccure-0.5/THANKS0000644000175000017500000000016612372135467013611 0ustar bertrambertramI would like to thank the following people for comments and code: James Westby (comments, ideas, debian packaging) seccure-0.5/LICENSE0000444000175000017500000001674311352247751013706 0ustar bertrambertram GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. seccure-0.5/seccure.manpage.xml0000644000175000017500000002455712372135467016472 0ustar bertrambertram seccure-key [-c curve] [-F pwfile] [-d] [-v] [-q] seccure-encrypt [-m maclen] [-c curve] [-i infile] [-o outfile] [-v] [-q] key seccure-decrypt [-m maclen] [-c curve] [-i infile] [-o outfile] [-F pwfile] [-d] [-v] [-q] seccure-sign [-f] [-b] [-a] [-c curve] [-s sigfile] [-i infile] [-o outfile] [-F pwfile] [-d] [-v] [-q] seccure-verify [-f] [-b] [-a] [-c curve] [-s sigfile] [-i infile] [-o outfile] [-v] [-q] key [sig] seccure-signcrypt [-c sig_curve [-c enc_curve]] [-i infile] [-o outfile] [-F pwfile] [-d] [-v] [-q] key seccure-veridec [-c enc_curve [-c sig_curve]] [-i infile] [-o outfile] [-F pwfile] [-d] [-v] [-q] key seccure-dh [-c curve] [-v] [-q]

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

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

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

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

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

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

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

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

seccure-veridec: Counterpart to signcryption.

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

All commands in the seccure software suite exit with a status of zero if the desired operation could be completed successfully. Any error leads to a nonzero exit code.

Given the passphrase 'seccure is secure', run

seccure-key

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

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

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

The message can be recovered with

seccure-decrypt -i document.enc

To sign the file run

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

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

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

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

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

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

This software (v0.5) was written by B. Poettering (seccure AT point-at-infinity.org) in 2006-2014. It is released under the terms of the GNU Lesser General Public License (LGPLv3). Find the latest version of seccure on the project's homepage: .
seccure-0.5/numtheory.c0000644000175000017500000000607512372135467015101 0ustar bertrambertram/* * seccure - Copyright 2014 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program. If not, see * . */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which * is part of the GnuPG project). Use the included Makefile to build * the binary. * * Report bugs to: seccure AT point-at-infinity.org * */ #include #include "numtheory.h" /******************************************************************************/ /* Fact 2.146(i) in the "Handbook of Applied Cryptography" */ int mod_issquare(const gcry_mpi_t a, const gcry_mpi_t p) { if (gcry_mpi_cmp_ui(a, 0)) { gcry_mpi_t p1, p2; int res; p1 = gcry_mpi_snew(0); p2 = gcry_mpi_snew(0); gcry_mpi_rshift(p1, p, 1); gcry_mpi_powm(p2, a, p1, p); res = ! gcry_mpi_cmp_ui(p2, 1); gcry_mpi_release(p1); gcry_mpi_release(p2); return res; } else return 1; } /* Algorithm II.8 in "Elliptic Curves in Cryptography" */ int mod_root(gcry_mpi_t x, const gcry_mpi_t a, const gcry_mpi_t p) { gcry_mpi_t h, n, q, y, b, t; int r, m; if (! gcry_mpi_cmp_ui(a, 0)) { gcry_mpi_set_ui(x, 0); return 1; } if (! mod_issquare(a, p)) return 0; h = gcry_mpi_snew(0); n = gcry_mpi_snew(0); gcry_mpi_set_ui(n, 2); while (mod_issquare(n, p)) gcry_mpi_add_ui(n, n, 1); q = gcry_mpi_snew(0); gcry_mpi_sub_ui(q, p, 1); for(r = 0; ! gcry_mpi_test_bit(q, r); r++); gcry_mpi_rshift(q, q, r); y = gcry_mpi_snew(0); gcry_mpi_powm(y, n, q, p); b = gcry_mpi_snew(0); gcry_mpi_rshift(h, q, 1); gcry_mpi_powm(b, a, h, p); gcry_mpi_mulm(x, a, b, p); gcry_mpi_mulm(b, b, x, p); t = gcry_mpi_snew(0); while (gcry_mpi_cmp_ui(b, 1)) { gcry_mpi_mulm(h, b, b, p); for(m = 1; gcry_mpi_cmp_ui(h, 1); m++) gcry_mpi_mulm(h, h, h, p); gcry_mpi_set_ui(h, 0); gcry_mpi_set_bit(h, r - m - 1); gcry_mpi_powm(t, y, h, p); gcry_mpi_mulm(y, t, t, p); r = m; gcry_mpi_mulm(x, x, t, p); gcry_mpi_mulm(b, b, y, p); } gcry_mpi_release(h); gcry_mpi_release(n); gcry_mpi_release(q); gcry_mpi_release(y); gcry_mpi_release(b); gcry_mpi_release(t); return 1; } seccure-0.5/seccure.c0000644000175000017500000010162312372135467014473 0ustar bertrambertram/* * seccure - Copyright 2014 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program. If not, see * . */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which * is part of the GnuPG project). Use the included Makefile to build * the binary. * * Report bugs to: seccure AT point-at-infinity.org * */ #include #include #include #include #include #include #include #include #include #include #include "curves.h" #include "protocol.h" #include "serialize.h" #include "aes256ctr.h" #define ANSI_CLEAR_LINE "\033[1K\r" #define VERSION "0.5" #define COPYBUF_SIZE (1 << 20) #define DEFAULT_CURVE "p160" #define DEFAULT_MAC_LEN 10 #define MAX_MACLEN 32 int opt_help = 0; int opt_verbose = 0; int opt_quiet = 0; int opt_sigcopy = 0; int opt_sigbin = 0; int opt_sigappend = 0; int opt_maclen = -1; int opt_dblprompt = 0; char *opt_infile = NULL; char *opt_outfile = NULL; char *opt_curve = NULL; char *opt_curve2 = NULL; char *opt_pwfile = NULL; char *opt_sigfile = NULL; int opt_fdin = STDIN_FILENO; int opt_fdout = STDOUT_FILENO; int opt_fdpw = STDIN_FILENO; /******************************************************************************/ void beep_on_terminal(FILE *term) { #if ! NOBEEP if (isatty(fileno(term))) fputc('\a', term); #endif } #if 0 void warning(const char *msg) { beep_on_terminal(stderr); fprintf(stderr, "WARNING: %s.\n", msg); } void warning_errno(const char *msg, int err) { beep_on_terminal(stderr); fprintf(stderr, "WARNING: %s: %s.\n", msg, strerror(err)); } #endif void warning_gcrypt(const char *msg, gcry_error_t err) { beep_on_terminal(stderr); fprintf(stderr, "WARNING: %s: %s.\n", msg, gcry_strerror(err)); } void fatal(const char *msg) { beep_on_terminal(stderr); fprintf(stderr, "FATAL: %s.\n", msg); exit(1); } void fatal_errno(const char *msg, int err) { beep_on_terminal(stderr); fprintf(stderr, "FATAL: %s: %s.\n", msg, strerror(err)); exit(1); } void fatal_gcrypt(const char *msg, gcry_error_t err) { beep_on_terminal(stderr); fprintf(stderr, "FATAL: %s: %s.\n", msg, gcry_strerror(err)); exit(1); } void print_quiet(const char *msg, int beep) { if (! opt_quiet) { if (beep) beep_on_terminal(stderr); fprintf(stderr, "%s", msg); } } /******************************************************************************/ void write_block(int fd, const char *buf, size_t len) { ssize_t c; while(len) { if ((c = write(fd, buf, len)) < 0) fatal_errno("Write error", errno); buf += c; len -= c; } } int read_block(int fd, char *buf, size_t len) { ssize_t c; while(len) { if ((c = read(fd, buf, len)) < 0) fatal_errno("Read error", errno); if (c == 0) return 0; buf += c; len -= c; } return 1; } void encryption_loop(int fdin, int fdout, struct aes256ctr *ac, gcry_md_hd_t *mh_pre, gcry_md_hd_t *mh_post) { char buf[COPYBUF_SIZE]; ssize_t c; while ((c = read(fdin, buf, COPYBUF_SIZE)) > 0) { if (mh_pre) gcry_md_write(*mh_pre, buf, c); aes256ctr_enc(ac, buf, c); if (mh_post) gcry_md_write(*mh_post, buf, c); write_block(fdout, buf, c); } if (c < 0) fatal_errno("Read error", errno); } void decryption_loop(int fdin, int fdout, struct aes256ctr *ac, gcry_md_hd_t *mh_pre, gcry_md_hd_t *mh_post, char *tail, size_t taillen) { char buf[COPYBUF_SIZE]; ssize_t c; if (! read_block(fdin, buf, taillen)) fatal("Input too short"); while ((c = read(fdin, buf + taillen, COPYBUF_SIZE - taillen)) > 0) { if (mh_pre) gcry_md_write(*mh_pre, buf, c); aes256ctr_dec(ac, buf, c); if (mh_post) gcry_md_write(*mh_post, buf, c); write_block(fdout, buf, c); memmove(buf, buf + c, taillen); } if (c < 0) fatal_errno("Read error", errno); memcpy(tail, buf, taillen); } void verisign_loop(int fdin, int fdout, gcry_md_hd_t *mh, char *tail, size_t taillen, int copyflag) { char buf[COPYBUF_SIZE]; ssize_t c; if (! read_block(fdin, buf, taillen)) fatal("Input too short"); while((c = read(fdin, buf + taillen, COPYBUF_SIZE - taillen)) > 0) { gcry_md_write(*mh, buf, c); if (copyflag) write_block(fdout, buf, c); memmove(buf, buf + c, taillen); } if (c < 0) fatal_errno("Read error", errno); memcpy(tail, buf, taillen); } /******************************************************************************/ void do_read_passphrase(char *hash, const struct termios *err_tios) { gcry_error_t err; gcry_md_hd_t mh; char *md, ch; ssize_t r; err = gcry_md_open(&mh, GCRY_MD_SHA256, GCRY_MD_FLAG_SECURE); if (gcry_err_code(err)) { if (err_tios) tcsetattr(opt_fdpw, TCSANOW, err_tios); fatal_gcrypt("Cannot initialize SHA256", err); } while (((r = read(opt_fdpw, &ch, 1)) > 0) && (ch != '\n')) if (ch != '\r') gcry_md_putc(mh, ch); if (r < 0) { int err = errno; if (err_tios) tcsetattr(opt_fdpw, TCSANOW, err_tios); fatal_errno("Cannot read text line", err); } gcry_md_final(mh); md = (char*)gcry_md_read(mh, 0); memcpy(hash, md, 32); gcry_md_close(mh); } void read_passphrase(char *hash, const char *name) { if (isatty(opt_fdpw)) { struct termios echo_orig, echo_off; tcgetattr(opt_fdpw, &echo_orig); echo_off = echo_orig; echo_off.c_lflag &= ~ECHO; tcsetattr(opt_fdpw, TCSANOW, &echo_off); if (! opt_quiet) fprintf(stderr, "Enter %s: ", name); do_read_passphrase(hash, &echo_orig); if (! opt_quiet) fprintf(stderr, ANSI_CLEAR_LINE); if (opt_dblprompt) { char *hash2; if (! (hash2 = gcry_malloc_secure(32))) { tcsetattr(opt_fdpw, TCSANOW, &echo_orig); fatal("Out of secure memory"); } if (! opt_quiet) fprintf(stderr, "Reenter %s: ", name); do_read_passphrase(hash2, &echo_orig); if (! opt_quiet) fprintf(stderr, ANSI_CLEAR_LINE); if (memcmp(hash, hash2, 32)) { tcsetattr(opt_fdpw, TCSANOW, &echo_orig); fatal("The two inputs differ"); } gcry_free(hash2); } tcsetattr(opt_fdpw, TCSANOW, &echo_orig); } else { if (opt_dblprompt) print_quiet("Ignoring -d flag.\n", 0); do_read_passphrase(hash, NULL); } } /******************************************************************************/ void app_print_public_key(void) { struct curve_params *cp; if (! opt_curve) { opt_curve = DEFAULT_CURVE; fprintf(stderr, "Assuming curve " DEFAULT_CURVE ".\n"); } if ((cp = curve_by_name(opt_curve))) { char pubkey[MAX_PK_LEN_COMPACT + 1]; char *privkey; struct affine_point P; gcry_mpi_t d; if (opt_verbose) { print_quiet("VERSION: ", 0); fprintf(stderr, VERSION "\n"); print_quiet("CURVE: ", 0); fprintf(stderr, "%s\n", cp->name); } if (! (privkey = gcry_malloc_secure(32))) fatal("Out of secure memory"); read_passphrase(privkey, "private key"); d = hash_to_exponent(privkey, cp); gcry_free(privkey); P = pointmul(&cp->dp.base, d, &cp->dp); gcry_mpi_release(d); compress_to_string(pubkey, DF_COMPACT, &P, cp); pubkey[cp->pk_len_compact] = 0; if (! opt_quiet) printf("The public key is: "); printf("%s\n", pubkey); point_release(&P); curve_release(cp); } else fatal("Invalid curve name"); } void app_encrypt(const char *pubkey) { struct affine_point P, R; struct curve_params *cp; if (opt_maclen < 0) { opt_maclen = DEFAULT_MAC_LEN; fprintf(stderr, "Assuming MAC length of %d bits.\n", 8 * DEFAULT_MAC_LEN); } if (opt_curve) { if (! (cp = curve_by_name(opt_curve))) fatal("Invalid curve name"); } else if (! (cp = curve_by_pk_len_compact(strlen(pubkey)))) fatal("Invalid encryption key (wrong length)"); if (opt_verbose) { print_quiet("VERSION: ", 0); fprintf(stderr, VERSION "\n"); print_quiet("CURVE: ", 0); fprintf(stderr, "%s\n", cp->name); print_quiet("MACLEN: ", 0); fprintf(stderr, "%d\n", 8 * opt_maclen); } if (strlen(pubkey) != cp->pk_len_compact) fatal("Invalid encryption key (wrong length)"); if (decompress_from_string(&P, pubkey, DF_COMPACT, cp)) { char rbuf[MAX_PK_LEN_BIN]; struct aes256ctr *ac; char *keybuf, *md; gcry_md_hd_t mh; if (! (keybuf = gcry_malloc_secure(64))) fatal("Out of secure memory"); R = ECIES_encapsulation(keybuf, &P, cp); compress_to_string(rbuf, DF_BIN, &R, cp); point_release(&P); point_release(&R); if (opt_verbose) { int i; print_quiet("K_ENC: ", 0); for(i = 0; i < 32; i++) fprintf(stderr, "%02x", (unsigned char)keybuf[i]); fprintf(stderr, "\n"); print_quiet("K_MAC: ", 0); for(i = 32; i < 64; i++) fprintf(stderr, "%02x", (unsigned char)keybuf[i]); fprintf(stderr, "\n"); } if (! (ac = aes256ctr_init(keybuf))) fatal("Cannot initialize AES256-CTR"); if (opt_maclen && ! hmacsha256_init(&mh, keybuf + 32, HMAC_KEY_SIZE)) fatal("Cannot initialize HMAC-SHA256"); gcry_free(keybuf); if (isatty(opt_fdin)) print_quiet("Go ahead and type your message ...\n", 0); write_block(opt_fdout, rbuf, cp->pk_len_bin); encryption_loop(opt_fdin, opt_fdout, ac, NULL, opt_maclen ? &mh : NULL); aes256ctr_done(ac); if (opt_maclen) { gcry_md_final(mh); md = (char*)gcry_md_read(mh, 0); if (opt_verbose) { int i; print_quiet("HMAC: ", 0); for(i = 0; i < opt_maclen; i++) fprintf(stderr, "%02x", (unsigned char)md[i]); fprintf(stderr, "\n"); } write_block(opt_fdout, md, opt_maclen); gcry_md_close(mh); } } else fatal("Invalid encryption key"); curve_release(cp); } int app_decrypt(void) { struct curve_params *cp; struct affine_point R; int res = 0; if (opt_maclen < 0) { opt_maclen = DEFAULT_MAC_LEN; fprintf(stderr, "Assuming MAC length of %d bits.\n", 8 * DEFAULT_MAC_LEN); } if (! opt_curve) { opt_curve = DEFAULT_CURVE; fprintf(stderr, "Assuming curve " DEFAULT_CURVE ".\n"); } if ((cp = curve_by_name(opt_curve))) { char *keybuf, *privkey; char rbuf[MAX_PK_LEN_BIN]; char mdbuf[MAX_MACLEN], *md; struct aes256ctr *ac; gcry_md_hd_t mh; gcry_mpi_t d; if (opt_verbose) { print_quiet("VERSION: ", 0); fprintf(stderr, VERSION "\n"); print_quiet("CURVE: ", 0); fprintf(stderr, "%s\n", cp->name); print_quiet("MACLEN: ", 0); fprintf(stderr, "%d\n", 8 * opt_maclen); } if (! (privkey = gcry_malloc_secure(32))) fatal("Out of secure memory"); read_passphrase(privkey, "private key"); d = hash_to_exponent(privkey, cp); gcry_free(privkey); if (isatty(opt_fdin)) print_quiet("Go ahead and enter the ciphertext ...\n", 0); if (read_block(opt_fdin, rbuf, cp->pk_len_bin)) { if (decompress_from_string(&R, rbuf, DF_BIN, cp)) { if (! (keybuf = gcry_malloc_secure(64))) fatal("Out of secure memory"); if (ECIES_decapsulation(keybuf, &R, d, cp)) { if (opt_verbose) { int i; print_quiet("K_ENC: ", 0); for(i = 0; i < 32; i++) fprintf(stderr, "%02x", (unsigned char)keybuf[i]); fprintf(stderr, "\n"); print_quiet("K_MAC: ", 0); for(i = 32; i < 64; i++) fprintf(stderr, "%02x", (unsigned char)keybuf[i]); fprintf(stderr, "\n"); } if (! (ac = aes256ctr_init(keybuf))) fatal("Cannot initialize AES256-CTR"); if (opt_maclen && ! hmacsha256_init(&mh, keybuf + 32, HMAC_KEY_SIZE)) fatal("Cannot initialize HMAC-SHA256"); memset(keybuf, 0x00, 64); decryption_loop(opt_fdin, opt_fdout, ac, opt_maclen ? &mh : NULL, NULL, mdbuf, opt_maclen); aes256ctr_done(ac); if (opt_maclen) { gcry_md_final(mh); md = (char*)gcry_md_read(mh, 0); if (opt_verbose) { int i; print_quiet("HMAC1: ", 0); for(i = 0; i < opt_maclen; i++) fprintf(stderr, "%02x", (unsigned char)md[i]); fprintf(stderr, "\n"); print_quiet("HMAC2: ", 0); for(i = 0; i < opt_maclen; i++) fprintf(stderr, "%02x", (unsigned char)mdbuf[i]); fprintf(stderr, "\n"); } if ((res = ! memcmp(mdbuf, md, opt_maclen))) print_quiet("Integrity check successful, message unforged!\n", 0); else print_quiet("Integrity check failed, message forged!\n", 1); gcry_md_close(mh); } else { res = 1; print_quiet("Warning: No MAC available, message integrity cannot " "be verified!\n", 0); } } else print_quiet("Abort: Inconsistent header.\n", 1); gcry_free(keybuf); point_release(&R); } else print_quiet("Abort: Inconsistent header.\n", 1); } else print_quiet("Abort: Inconsistent header (too short).\n", 1); gcry_mpi_release(d); curve_release(cp); } else fatal("Invalid curve name"); return ! res; } void app_sign(void) { struct curve_params *cp; char *privkey, *md; gcry_md_hd_t mh; gcry_error_t err; gcry_mpi_t d, sig; FILE *sigfile; if (opt_sigappend) { opt_sigcopy = 1; if (opt_sigfile) fatal("The options -s and -a may not be combined"); } if (! opt_curve) { opt_curve = DEFAULT_CURVE; fprintf(stderr, "Assuming curve " DEFAULT_CURVE ".\n"); } if ((cp = curve_by_name(opt_curve))) { if (opt_verbose) { print_quiet("VERSION: ", 0); fprintf(stderr, VERSION "\n"); print_quiet("CURVE: ", 0); fprintf(stderr, "%s\n", cp->name); } if (! (privkey = gcry_malloc_secure(32))) fatal("Out of secure memory"); read_passphrase(privkey, "private key"); d = hash_to_exponent(privkey, cp); gcry_free(privkey); err = gcry_md_open(&mh, GCRY_MD_SHA512, 0); if (gcry_err_code(err)) fatal_gcrypt("Cannot initialize SHA512", err); if (isatty(opt_fdin)) print_quiet("Go ahead and type your message ...\n", 0); verisign_loop(opt_fdin, opt_fdout, &mh, NULL, 0, opt_sigcopy); gcry_md_final(mh); md = (char*)gcry_md_read(mh, 0); if (opt_verbose) { int i; print_quiet("SHA512: ", 0); for(i = 0; i < 64; i++) fprintf(stderr, "%02x", (unsigned char)md[i]); fprintf(stderr, "\n"); } sig = ECDSA_sign(md, d, cp); gcry_mpi_release(d); gcry_md_close(mh); if (opt_sigfile) { if (! (sigfile = fopen(opt_sigfile, "w"))) fatal_errno("Cannot open signature file", errno); } else sigfile = stderr; if (opt_sigbin) { char sigbuf[MAX_SIG_LEN_BIN]; serialize_mpi(sigbuf, cp->sig_len_bin, DF_BIN, sig); if (opt_sigappend) write_block(opt_fdout, sigbuf, cp->sig_len_bin); else if (fwrite(sigbuf, cp->sig_len_bin, 1, sigfile) != 1) fatal_errno("Cannot write signature", errno); } else { char sigbuf[MAX_SIG_LEN_COMPACT + 1]; serialize_mpi(sigbuf, cp->sig_len_compact, DF_COMPACT, sig); if (opt_sigappend) write_block(opt_fdout, sigbuf, cp->sig_len_compact); else { sigbuf[cp->sig_len_compact] = 0; if (sigfile == stderr) print_quiet("Signature: ", 0); if (fprintf(sigfile, "%s\n", sigbuf) < 0) fatal_errno("Cannot write signature", errno); } } if (opt_sigfile && fclose(sigfile)) fatal_errno("Cannot close signature file", errno); gcry_mpi_release(sig); curve_release(cp); } else fatal("Invalid curve name"); } int app_verify(const char *pubkey, const char *sig) { struct curve_params *cp; struct affine_point Q; gcry_mpi_t s; gcry_md_hd_t mh; gcry_error_t err; char *md; int res = 0; if (!! sig + !! opt_sigfile + !! opt_sigappend != 1) fatal("Exactly one signature has to be specified"); if (sig && opt_sigbin) fatal("Binary signature is not accepted here"); if (opt_curve) { if (! (cp = curve_by_name(opt_curve))) fatal("Invalid curve name"); } else if (! (cp = curve_by_pk_len_compact(strlen(pubkey)))) fatal("Invalid verification key (wrong length)"); if (opt_verbose) { print_quiet("VERSION: ", 0); fprintf(stderr, VERSION "\n"); print_quiet("CURVE: ", 0); fprintf(stderr, "%s\n", cp->name); } if (strlen(pubkey) != cp->pk_len_compact) fatal("Invalid verification key (wrong length)"); if (decompress_from_string(&Q, pubkey, DF_COMPACT, cp)) { union { char compact[MAX_SIG_LEN_COMPACT + 2]; char bin[MAX_SIG_LEN_BIN]; } sigbuf; err = gcry_md_open(&mh, GCRY_MD_SHA512, 0); if (gcry_err_code(err)) fatal_gcrypt("Cannot initialize SHA512", err); if (opt_sigfile) { FILE *sigfile; if (! (sigfile = fopen(opt_sigfile, "r"))) fatal_errno("Cannot open signature file", errno); if (opt_sigbin) { if (fread(sigbuf.bin, cp->sig_len_bin, 1, sigfile) != 1) { if (ferror(sigfile)) fatal_errno("Cannot read signature", errno); else { print_quiet("Invalid signature (wrong length)!\n", 1); fclose(sigfile); goto error; } } } else { sigbuf.compact[0] = 0; if (! fgets(sigbuf.compact, cp->sig_len_compact + 2, sigfile) && ferror(sigfile)) fatal_errno("Cannot read signature", errno); sigbuf.compact[strcspn(sigbuf.compact, " \r\n")] = '\0'; } if (fclose(sigfile)) fatal_errno("Cannot close signature file", errno); } if (isatty(opt_fdin)) print_quiet("Go ahead and type your message ...\n", 0); if (opt_sigappend) { if (opt_sigbin) verisign_loop(opt_fdin, opt_fdout, &mh, sigbuf.bin, cp->sig_len_bin, opt_sigcopy); else { verisign_loop(opt_fdin, opt_fdout, &mh, sigbuf.compact, cp->sig_len_compact, opt_sigcopy); sigbuf.compact[cp->sig_len_compact] = 0; } } else verisign_loop(opt_fdin, opt_fdout, &mh, NULL, 0, opt_sigcopy); gcry_md_final(mh); md = (char*)gcry_md_read(mh, 0); if (opt_verbose) { int i; print_quiet("SHA512: ", 0); for(i = 0; i < 64; i++) fprintf(stderr, "%02x", (unsigned char)md[i]); fprintf(stderr, "\n"); } if (! opt_sigbin) { if (! sig) sig = sigbuf.compact; if (strlen(sig) != cp->sig_len_compact) { print_quiet("Invalid signature (wrong length)!\n", 1); goto error; } else if (! deserialize_mpi(&s, DF_COMPACT, sig, cp->sig_len_compact)) { print_quiet("Invalid signature (inconsistent structure)!\n", 1); goto error; } } else assert(deserialize_mpi(&s, DF_BIN, sigbuf.bin, cp->sig_len_bin)); if ((res = ECDSA_verify(md, &Q, s, cp))) print_quiet("Signature successfully verified!\n", 0); else print_quiet("Invalid signature, message forged!\n", 1); gcry_mpi_release(s); error: gcry_md_close(mh); point_release(&Q); } else fatal("Invalid verification key"); curve_release(cp); return ! res; } void app_signcrypt(const char *pubkey) { struct curve_params *cp_enc, *cp_sig; struct affine_point P, R; if (! opt_curve) { opt_curve = DEFAULT_CURVE; fprintf(stderr, "Assuming signature curve " DEFAULT_CURVE ".\n"); } if (! (cp_sig = curve_by_name(opt_curve))) fatal("Invalid curve name"); if (opt_curve2) { if (! (cp_enc = curve_by_name(opt_curve2))) fatal("Invalid curve name"); } else if (! (cp_enc = curve_by_pk_len_compact(strlen(pubkey)))) fatal("Invalid encryption key (wrong length)"); if (opt_verbose) { print_quiet("VERSION: ", 0); fprintf(stderr, VERSION "\n"); print_quiet("SIGNATURE CURVE: ", 0); fprintf(stderr, "%s\n", cp_sig->name); print_quiet("ENCRYPTION CURVE: ", 0); fprintf(stderr, "%s\n", cp_enc->name); } if (strlen(pubkey) != cp_enc->pk_len_compact) fatal("Invalid encryption key (wrong length)"); if (decompress_from_string(&P, pubkey, DF_COMPACT, cp_enc)) { char rbuf[MAX_PK_LEN_BIN]; char sigbuf[MAX_SIG_LEN_BIN]; char *privkey, *keybuf, *md; struct aes256ctr *ac; gcry_mpi_t d, sig; gcry_md_hd_t mh; gcry_error_t err; if (! (privkey = gcry_malloc_secure(32))) fatal("Out of secure memory"); read_passphrase(privkey, "private signing key"); d = hash_to_exponent(privkey, cp_sig); gcry_free(privkey); if (! (keybuf = gcry_malloc_secure(64))) fatal("Out of secure memory"); R = ECIES_encapsulation(keybuf, &P, cp_enc); compress_to_string(rbuf, DF_BIN, &R, cp_enc); point_release(&P); point_release(&R); if (opt_verbose) { int i; print_quiet("K_ENC: ", 0); for(i = 0; i < 32; i++) fprintf(stderr, "%02x", (unsigned char)keybuf[i]); fprintf(stderr, "\n"); } if (! (ac = aes256ctr_init(keybuf))) fatal("Cannot initialize AES256-CTR"); gcry_free(keybuf); err = gcry_md_open(&mh, GCRY_MD_SHA512, GCRY_MD_FLAG_SECURE); if (gcry_err_code(err)) fatal_gcrypt("Cannot initialize SHA512", err); if (isatty(opt_fdin)) print_quiet("Go ahead and type your message ...\n", 0); write_block(opt_fdout, rbuf, cp_enc->pk_len_bin); encryption_loop(opt_fdin, opt_fdout, ac, &mh, NULL); gcry_md_final(mh); md = (char*)gcry_md_read(mh, 0); if (opt_verbose) { int i; print_quiet("SHA512: ", 0); for(i = 0; i < 64; i++) fprintf(stderr, "%02x", (unsigned char)md[i]); fprintf(stderr, "\n"); } sig = ECDSA_sign(md, d, cp_sig); gcry_mpi_release(d); gcry_md_close(mh); serialize_mpi(sigbuf, cp_sig->sig_len_bin, DF_BIN, sig); aes256ctr_enc(ac, sigbuf, cp_sig->sig_len_bin); write_block(opt_fdout, sigbuf, cp_sig->sig_len_bin); gcry_mpi_release(sig); aes256ctr_done(ac); } else fatal("Invalid encryption key"); curve_release(cp_enc); curve_release(cp_sig); } int app_veridec(const char *pubkey) { struct curve_params *cp_enc, *cp_sig; struct affine_point Q, R; int res = 0; if (! opt_curve) { opt_curve = DEFAULT_CURVE; fprintf(stderr, "Assuming encryption curve " DEFAULT_CURVE ".\n"); } if (! (cp_enc = curve_by_name(opt_curve))) fatal("Invalid curve name"); if (opt_curve2) { if (! (cp_sig = curve_by_name(opt_curve2))) fatal("Invalid curve name"); } else if (! (cp_sig = curve_by_pk_len_compact(strlen(pubkey)))) fatal("Invalid verification key (wrong length)"); if (opt_verbose) { print_quiet("VERSION: ", 0); fprintf(stderr, VERSION "\n"); print_quiet("SIGNATURE CURVE: ", 0); fprintf(stderr, "%s\n", cp_sig->name); print_quiet("ENCRYPTION CURVE: ", 0); fprintf(stderr, "%s\n", cp_enc->name); } if (strlen(pubkey) != cp_sig->pk_len_compact) fatal("Invalid verification key (wrong length)"); if (decompress_from_string(&Q, pubkey, DF_COMPACT, cp_sig)) { char rbuf[MAX_PK_LEN_BIN]; char sigbuf[MAX_SIG_LEN_BIN]; char *privkey, *keybuf, *md; gcry_mpi_t d, sig; struct aes256ctr *ac; gcry_md_hd_t mh; gcry_error_t err; if (! (privkey = gcry_malloc_secure(32))) fatal("Out of secure memory"); read_passphrase(privkey, "private decryption key"); d = hash_to_exponent(privkey, cp_enc); gcry_free(privkey); if (isatty(opt_fdin)) print_quiet("Go ahead and enter the ciphertext ...\n", 0); if (read_block(opt_fdin, rbuf, cp_enc->pk_len_bin)) { if (decompress_from_string(&R, rbuf, DF_BIN, cp_enc)) { if (! (keybuf = gcry_malloc_secure(64))) fatal("Out of secure memory"); if (ECIES_decapsulation(keybuf, &R, d, cp_enc)) { if (opt_verbose) { int i; print_quiet("K_ENC: ", 0); for(i = 0; i < 32; i++) fprintf(stderr, "%02x", (unsigned char)keybuf[i]); fprintf(stderr, "\n"); } if (! (ac = aes256ctr_init(keybuf))) fatal("Cannot initialize AES256-CTR"); memset(keybuf, 0xff, 64); err = gcry_md_open(&mh, GCRY_MD_SHA512, GCRY_MD_FLAG_SECURE); if (gcry_err_code(err)) fatal_gcrypt("Cannot initialize SHA512", err); decryption_loop(opt_fdin, opt_fdout, ac, NULL, &mh, sigbuf, cp_sig->sig_len_bin); gcry_md_final(mh); md = (char*)gcry_md_read(mh, 0); if (opt_verbose) { int i; print_quiet("SHA512: ", 0); for(i = 0; i < 64; i++) fprintf(stderr, "%02x", (unsigned char)md[i]); fprintf(stderr, "\n"); } aes256ctr_dec(ac, sigbuf, cp_sig->sig_len_bin); assert(deserialize_mpi(&sig, DF_BIN, sigbuf, cp_sig->sig_len_bin)); if ((res = ECDSA_verify(md, &Q, sig, cp_sig))) print_quiet("Signature successfully verified!\n", 0); else print_quiet("WARNING: Invalid signature, message forged!\n", 1); gcry_mpi_release(sig); gcry_md_close(mh); aes256ctr_done(ac); } else print_quiet("Abort: Inconsistent header.\n", 1); gcry_free(keybuf); point_release(&R); } else print_quiet("Abort: Inconsistent header.\n", 1); } else print_quiet("Abort: Inconsistent header (too short).\n", 1); gcry_mpi_release(d); point_release(&Q); } else fatal("Invalid verification key"); curve_release(cp_enc); curve_release(cp_sig); return ! res; } void app_dh(void) { struct curve_params *cp; if (! opt_curve) { opt_curve = DEFAULT_CURVE; fprintf(stderr, "Assuming curve " DEFAULT_CURVE ".\n"); } if ((cp = curve_by_name(opt_curve))) { char keyA[MAX_PK_LEN_COMPACT + 1]; char keyB[MAX_PK_LEN_COMPACT + 2]; char *keybuf, *outbuf; struct affine_point A, B; gcry_mpi_t exp, h; if (opt_verbose) { print_quiet("VERSION: ", 0); fprintf(stderr, VERSION "\n"); print_quiet("CURVE: ", 0); fprintf(stderr, "%s\n", cp->name); } exp = DH_step1(&A, cp); compress_to_string(keyA, DF_COMPACT, &A, cp); point_release(&A); keyA[cp->pk_len_compact] = 0; print_quiet("Pass this key to your peer: ", 0); fprintf(stderr, "%s\n", keyA); print_quiet("Enter your peer's key: ", 0); keyB[0] = 0; if (! fgets(keyB, cp->pk_len_compact + 2, stdin) && ferror(stdin)) fatal_errno("Cannot read text line", errno); keyB[strcspn(keyB, "\r\n")] = 0; if (strlen(keyB) != cp->pk_len_compact) fatal("Invalid key (wrong length)"); if (decompress_from_string(&B, keyB, DF_COMPACT, cp)) { if (! (keybuf = gcry_malloc_secure(64))) fatal("Out of secure memory"); if (DH_step2(keybuf, &B, exp, cp)) { assert(cp->dh_len_bin <= 32); if (opt_verbose) { size_t i; print_quiet("K_ESTABLISHED: ", 0); for(i = 0; i < cp->dh_len_bin; i++) fprintf(stderr, "%02x", (unsigned char)keybuf[i]); fprintf(stderr, "\n"); print_quiet("K_VERIFICATION: ", 0); for(i = 0; i < cp->dh_len_bin; i++) fprintf(stderr, "%02x", (unsigned char)keybuf[32 + i]); fprintf(stderr, "\n"); } if (! (outbuf = gcry_malloc_secure(cp->dh_len_compact + 1))) fatal("Out of secure memory"); assert(deserialize_mpi(&h, DF_BIN, keybuf, cp->dh_len_bin)); serialize_mpi(outbuf, cp->dh_len_compact, DF_COMPACT, h); outbuf[cp->dh_len_compact] = 0; gcry_mpi_release(h); if (! opt_quiet) printf("Established key: "); printf("%s\n", outbuf); assert(deserialize_mpi(&h, DF_BIN, keybuf + 32, cp->dh_len_bin)); serialize_mpi(outbuf, cp->dh_len_compact, DF_COMPACT, h); outbuf[cp->dh_len_compact] = 0; gcry_mpi_release(h); if (! opt_quiet) printf("Verification key: "); printf("%s\n", outbuf); gcry_free(outbuf); } else fatal("Invalid key"); gcry_free(keybuf); point_release(&B); } else fatal("Invalid key"); gcry_mpi_release(exp); curve_release(cp); } else fatal("Invalid curve name"); } /******************************************************************************/ int main(int argc, char **argv) { gcry_error_t err; char *progname; int res = 0, i; assert(gcry_check_version("1.4.1")); err = gcry_control(GCRYCTL_INIT_SECMEM, 1); if (gcry_err_code(err)) warning_gcrypt("Cannot enable gcrypt's secure memory management", err); err = gcry_control(GCRYCTL_USE_SECURE_RNDPOOL, 1); if (gcry_err_code(err)) warning_gcrypt("Cannot enable gcrypt's secure " "random number generator", err); if (getuid() != geteuid()) if (seteuid(getuid())) fatal("Cannot set effective user id"); if ((progname = strrchr(argv[0], '/')) == NULL) progname = argv[0]; while((i = getopt(argc, argv, "fbadm:i:o:F:s:c:hvq")) != -1) switch(i) { case 'f': opt_sigcopy = 1; break; case 'b': opt_sigbin = 1; break; case 'a': opt_sigappend = 1; break; case 'd': opt_dblprompt = 1; break; case 'm': opt_maclen = atoi(optarg); if (opt_maclen < 0 || opt_maclen > 8 * MAX_MACLEN || opt_maclen % 8) fatal("Invalid MAC length"); opt_maclen /= 8; break; case 'i': opt_infile = optarg; break; case 'o': opt_outfile = optarg; break; case 'F': opt_pwfile = optarg; break; case 's': opt_sigfile = optarg; break; case 'c': if (! opt_curve) opt_curve = optarg; else opt_curve2 = optarg; break; case 'h': opt_help = 1; break; case 'v': opt_verbose = 1; break; case 'q': opt_quiet = 1; break; default: exit(1); } if (opt_infile) if ((opt_fdin = open(opt_infile, O_RDONLY)) < 0) fatal_errno("Cannot open input file", errno); if (opt_outfile) { int decmode = strstr(progname, "decrypt") || strstr(progname, "veridec"); if ((opt_fdout = open(opt_outfile, O_WRONLY | O_CREAT | O_TRUNC, decmode ? 0600 : 0644)) < 0) fatal_errno("Cannot open output file", errno); } if (opt_pwfile) { if ((opt_fdpw = open(opt_pwfile, O_RDONLY)) < 0) fatal_errno("Cannot open password file", errno); } else if (! opt_infile && ! isatty(STDIN_FILENO)) if ((opt_fdpw = open("/dev/tty", O_RDONLY)) < 0) fatal_errno("Cannot open tty", errno); if (strstr(progname, "key")) { if (opt_help || optind != argc) puts("Generate public key from private key (seccure version " VERSION ").\n" "\n" "seccure-key [-c curve] [-F pwfile] [-d]"); else app_print_public_key(); } else if (strstr(progname, "encrypt")) { if (opt_help || optind != argc - 1) puts("Encrypt a message with a public key (seccure version" VERSION ").\n" "\n" "seccure-encrypt [-m maclen] [-c curve] [-i infile] [-o outfile] key"); else app_encrypt(argv[optind]); } else if (strstr(progname, "decrypt")) { if (opt_help || optind != argc) puts("Decrypt a message using a secret key (seccure version " VERSION ").\n" "\n" "seccure-decrypt [-m maclen] [-c curve] [-i infile] [-o outfile]\n" " [-F pwfile] [-d]"); else res = app_decrypt(); } else if (strstr(progname, "signcrypt")) { if (opt_help || optind != argc - 1) puts("Signcrypt a message (seccure version " VERSION ").\n" "\n" "seccure-signcrypt [-c sig_curve [-c enc_curve]] [-i infile] [-o outfile]\n" " [-F pwfile] [-d] key"); else app_signcrypt(argv[optind]); } else if (strstr(progname, "veridec")) { if (opt_help || optind != argc - 1) puts("Decrypt and verify a signcrypted message (seccure version " VERSION ").\n" "\n" "seccure-veridec [-c enc_curve [-c sig_curve]] [-i infile] [-o outfile]\n" " [-F pwfile] [-d] key"); else res = app_veridec(argv[optind]); } else if (strstr(progname, "sign")) { if (opt_help || optind != argc) puts("Generate a signature (seccure version " VERSION ").\n" "\n" "seccure-sign [-f] [-b] [-a] [-c curve] [-s sigfile] [-i infile]\n" " [-o outfile] [-F pwfile] [-d]"); else app_sign(); } else if (strstr(progname, "verify")) { if (opt_help || (optind != argc - 2 && optind != argc - 1)) puts("Verify the signature of a message (seccure version " VERSION ").\n" "\n" "seccure-verify [-f] [-b] [-a] [-c curve] [-s sigfile] [-i infile]\n" " [-o outfile] key [signature]"); else res = app_verify(argv[optind], argv[optind + 1]); } else if (strstr(progname, "dh")) { if (opt_help || optind != argc) puts("Perform an interactive Diffie-Hellman key exchange (seccure version " VERSION ").\n" "\n" "seccure-dh [-c curve]"); else app_dh(); } else fatal("Unknown command"); if (opt_infile) close(opt_fdin); if (opt_outfile) close(opt_fdout); if (opt_fdpw != opt_fdin) close(opt_fdpw); gcry_control(GCRYCTL_TERM_SECMEM, 1); exit(res); } seccure-0.5/curves.h0000644000175000017500000000365512372135467014364 0ustar bertrambertram/* * seccure - Copyright 2014 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program. If not, see * . */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which * is part of the GnuPG project). Use the included Makefile to build * the binary. * * Report bugs to: seccure AT point-at-infinity.org * */ #ifndef INC_CURVES_H #define INC_CURVES_H #include "ecc.h" #define MAX_PK_LEN_BIN 66 #define MAX_PK_LEN_COMPACT 81 #define MAX_SIG_LEN_BIN 131 #define MAX_SIG_LEN_COMPACT 161 #define MAX_DH_LEN_BIN 32 #define MAX_DH_LEN_COMPACT 40 #define MAX_ELEM_LEN_BIN 66 #define MAX_ORDER_LEN_BIN 66 struct curve_params { const char *name; struct domain_params dp; size_t pk_len_bin, pk_len_compact; size_t sig_len_bin, sig_len_compact; size_t dh_len_bin, dh_len_compact; size_t elem_len_bin, order_len_bin; }; struct curve_params* curve_by_name(const char *name); struct curve_params* curve_by_pk_len_compact(size_t len); void curve_release(struct curve_params *cp); #endif /* INC_CURVES_H */ seccure-0.5/HISTORY0000644000175000017500000000100312372135467013751 0ustar bertrambertramv0.5: (Aug 2014) - support for brainpool curves added - compiles now with -std=c89 -pedantic - switch from GPLv2 to LGPLv3 license - minor bugfixes v0.4: (Apr 2009) - signature generation is deterministic now - fix in the allocation routines for secure memory - minor bugfixes v0.3: (Aug 2006) - added signcryption as shortcut for signing and subsequent encryption - added -a mode (inline signature) v0.2: (Aug 2006) - added Diffie-Hellman key exchange v0.1: (July 2006) - initial release seccure-0.5/aes256ctr.c0000644000175000017500000000633312372135467014562 0ustar bertrambertram/* * seccure - Copyright 2014 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program. If not, see * . */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which * is part of the GnuPG project). Use the included Makefile to build * the binary. * * Report bugs to: seccure AT point-at-infinity.org * */ #include #include #include #include "aes256ctr.h" /******************************************************************************/ struct aes256ctr* aes256ctr_init(const char *key) { struct aes256ctr *ac; gcry_error_t err; if (! (ac = gcry_malloc_secure(sizeof(struct aes256ctr)))) return NULL; err = gcry_cipher_open(&ac->ch, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CTR, GCRY_CIPHER_SECURE); if (gcry_err_code(err)) goto error; err = gcry_cipher_setkey(ac->ch, key, CIPHER_KEY_SIZE); if (gcry_err_code(err)) goto error; err = gcry_cipher_setctr(ac->ch, NULL, 0); if (gcry_err_code(err)) goto error; ac->idx = CIPHER_BLOCK_SIZE; return ac; error: gcry_free(ac); return NULL; } void aes256ctr_enc(struct aes256ctr *ac, char *buf, size_t len) { gcry_error_t err; size_t full_blocks; for(; len && (ac->idx < CIPHER_BLOCK_SIZE); len--) *buf++ ^= ac->buf[ac->idx++]; full_blocks = (len / CIPHER_BLOCK_SIZE) * CIPHER_BLOCK_SIZE; err = gcry_cipher_encrypt(ac->ch, buf, full_blocks, NULL, 0); assert(! gcry_err_code(err)); len -= full_blocks; buf += full_blocks; if (len) { memset(ac->buf, 0, CIPHER_BLOCK_SIZE); err = gcry_cipher_encrypt(ac->ch, ac->buf, CIPHER_BLOCK_SIZE, NULL, 0); assert(! gcry_err_code(err)); ac->idx = 0; for(; len && (ac->idx < CIPHER_BLOCK_SIZE); len--) *buf++ ^= ac->buf[ac->idx++]; } } void aes256ctr_done(struct aes256ctr *ac) { gcry_cipher_close(ac->ch); memset(ac->buf, 0, CIPHER_BLOCK_SIZE); gcry_free(ac); } int hmacsha256_init(gcry_md_hd_t *mh, const char *key, size_t len) { gcry_error_t err; err = gcry_md_open(mh, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC | GCRY_MD_FLAG_SECURE); if (gcry_err_code(err)) return 0; err = gcry_md_setkey(*mh, key, len); return ! gcry_err_code(err); } void aes256cprng_fillbuf(struct aes256cprng *cprng, char *buf, size_t len) { memset(buf, 0, len); aes256ctr_enc(cprng, buf, len); } seccure-0.5/protocol.c0000644000175000017500000002256612372135467014713 0ustar bertrambertram/* * seccure - Copyright 2014 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program. If not, see * . */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which * is part of the GnuPG project). Use the included Makefile to build * the binary. * * Report bugs to: seccure AT point-at-infinity.org * */ #define ECDSA_DETERMINISTIC 1 #include #include #include "ecc.h" #include "curves.h" #include "serialize.h" #include "aes256ctr.h" #include "protocol.h" /******************************************************************************/ gcry_mpi_t buf_to_exponent(const char *buf, size_t buflen, const struct curve_params *cp) { gcry_mpi_t a, b; gcry_mpi_scan(&a, GCRYMPI_FMT_USG, buf, buflen, NULL); gcry_mpi_set_flag(a, GCRYMPI_FLAG_SECURE); b = gcry_mpi_new(0); gcry_mpi_sub_ui(b, cp->dp.order, 1); gcry_mpi_mod(a, a, b); gcry_mpi_add_ui(a, a, 1); gcry_mpi_release(b); return a; } gcry_mpi_t hash_to_exponent(const char *hash, const struct curve_params *cp) { size_t len = cp->order_len_bin; struct aes256cprng *cprng; gcry_mpi_t a; char *buf; assert((buf = gcry_malloc_secure(len))); assert((cprng = aes256cprng_init(hash))); aes256cprng_fillbuf(cprng, buf, len); aes256cprng_done(cprng); a = buf_to_exponent(buf, len, cp); gcry_free(buf); return a; } gcry_mpi_t get_random_exponent(const struct curve_params *cp) { int bits = gcry_mpi_get_nbits(cp->dp.order); gcry_mpi_t a; a = gcry_mpi_snew(0); do { gcry_mpi_randomize(a, bits, GCRY_STRONG_RANDOM); gcry_mpi_clear_highbit(a, bits); } while (! gcry_mpi_cmp_ui(a, 0) || gcry_mpi_cmp(a, cp->dp.order) >= 0); return a; } /******************************************************************************/ void compress_to_string(char *buf, enum disp_format df, const struct affine_point *P, const struct curve_params *cp) { size_t outlen = (df == DF_COMPACT) ? cp->pk_len_compact : cp->pk_len_bin; if (point_compress(P)) { gcry_mpi_t x; x = gcry_mpi_snew(0); gcry_mpi_add(x, P->x, cp->dp.m); serialize_mpi(buf, outlen, df, x); gcry_mpi_release(x); } else serialize_mpi(buf, outlen, df, P->x); } int decompress_from_string(struct affine_point *P, const char *buf, enum disp_format df, const struct curve_params *cp) { gcry_mpi_t x; size_t inlen = (df == DF_COMPACT) ? cp->pk_len_compact : cp->pk_len_bin; int res; assert(! (df == DF_COMPACT && strlen(buf) != inlen)); if ((res = deserialize_mpi(&x, df, buf, inlen))) { int yflag; if ((yflag = (gcry_mpi_cmp(x, cp->dp.m) >= 0))) gcry_mpi_sub(x, x, cp->dp.m); res = gcry_mpi_cmp_ui(x, 0) >= 0 && gcry_mpi_cmp(x, cp->dp.m) < 0 && point_decompress(P, x, yflag, &cp->dp); gcry_mpi_release(x); } return res; } /******************************************************************************/ #if ECDSA_DETERMINISTIC static struct aes256cprng* ecdsa_cprng_init(const char *msg, const gcry_mpi_t d, const struct curve_params *cp) { size_t len = cp->order_len_bin; struct aes256cprng *cprng; gcry_md_hd_t mh; char *buf; assert((buf = gcry_malloc_secure(len))); serialize_mpi(buf, len, DF_BIN, d); assert(hmacsha256_init(&mh, buf, len)); gcry_free(buf); gcry_md_write(mh, msg, 64); gcry_md_final(mh); cprng = aes256cprng_init((const char*)gcry_md_read(mh, 0)); gcry_md_close(mh); return cprng; } static gcry_mpi_t ecdsa_cprng_get_exponent(struct aes256cprng *cprng, const struct curve_params *cp) { size_t len = cp->order_len_bin; gcry_mpi_t a; char *buf; assert((buf = gcry_malloc_secure(len))); aes256cprng_fillbuf(cprng, buf, len); a = buf_to_exponent(buf, len, cp); gcry_free(buf); return a; } #define ecdsa_cprng_done aes256cprng_done #endif /******************************************************************************/ /* Algorithms 4.29 and 4.30 in the "Guide to Elliptic Curve Cryptography" */ gcry_mpi_t ECDSA_sign(const char *msg, const gcry_mpi_t d, const struct curve_params *cp) { struct affine_point p1; gcry_mpi_t e, k, r, s; #if ECDSA_DETERMINISTIC struct aes256cprng *cprng; cprng = ecdsa_cprng_init(msg, d, cp); #endif r = gcry_mpi_snew(0); s = gcry_mpi_snew(0); Step1: #if ECDSA_DETERMINISTIC k = ecdsa_cprng_get_exponent(cprng, cp); #else k = get_random_exponent(cp); #endif p1 = pointmul(&cp->dp.base, k, &cp->dp); gcry_mpi_mod(r, p1.x, cp->dp.order); point_release(&p1); if (! gcry_mpi_cmp_ui(r, 0)) { gcry_mpi_release(k); goto Step1; } gcry_mpi_scan(&e, GCRYMPI_FMT_USG, msg, 64, NULL); gcry_mpi_set_flag(e, GCRYMPI_FLAG_SECURE); gcry_mpi_mod(e, e, cp->dp.order); gcry_mpi_mulm(s, d, r, cp->dp.order); gcry_mpi_addm(s, s, e, cp->dp.order); gcry_mpi_invm(e, k, cp->dp.order); gcry_mpi_mulm(s, s, e, cp->dp.order); gcry_mpi_release(e); gcry_mpi_release(k); if (! gcry_mpi_cmp_ui(s, 0)) goto Step1; gcry_mpi_mul(s, s, cp->dp.order); gcry_mpi_add(s, s, r); gcry_mpi_release(r); #if ECDSA_DETERMINISTIC ecdsa_cprng_done(cprng); #endif return s; } int ECDSA_verify(const char *msg, const struct affine_point *Q, const gcry_mpi_t sig, const struct curve_params *cp) { gcry_mpi_t e, r, s; struct affine_point X1, X2; int res = 0; r = gcry_mpi_new(0); s = gcry_mpi_new(0); gcry_mpi_div(s, r, sig, cp->dp.order, 0); if (gcry_mpi_cmp_ui(s, 0) <= 0 || gcry_mpi_cmp(s, cp->dp.order) >= 0 || gcry_mpi_cmp_ui(r, 0) <= 0 || gcry_mpi_cmp(r, cp->dp.order) >= 0) goto end; gcry_mpi_scan(&e, GCRYMPI_FMT_USG, msg, 64, NULL); gcry_mpi_mod(e, e, cp->dp.order); gcry_mpi_invm(s, s, cp->dp.order); gcry_mpi_mulm(e, e, s, cp->dp.order); X1 = pointmul(&cp->dp.base, e, &cp->dp); gcry_mpi_mulm(e, r, s, cp->dp.order); X2 = pointmul(Q, e, &cp->dp); point_add(&X1, &X2, &cp->dp); gcry_mpi_release(e); if (! point_is_zero(&X1)) { gcry_mpi_mod(s, X1.x, cp->dp.order); res = ! gcry_mpi_cmp(s, r); } point_release(&X1); point_release(&X2); end: gcry_mpi_release(r); gcry_mpi_release(s); return res; } /******************************************************************************/ /* Algorithms 4.42 and 4.43 in the "Guide to Elliptic Curve Cryptography" */ static void ECIES_KDF(char *key, const gcry_mpi_t Zx, const struct affine_point *R, size_t elemlen) { char *buf; assert((buf = gcry_malloc_secure(3 * elemlen))); serialize_mpi(buf + 0 * elemlen, elemlen, DF_BIN, Zx); serialize_mpi(buf + 1 * elemlen, elemlen, DF_BIN, R->x); serialize_mpi(buf + 2 * elemlen, elemlen, DF_BIN, R->y); gcry_md_hash_buffer(GCRY_MD_SHA512, key, buf, 3 * elemlen); gcry_free(buf); } struct affine_point ECIES_encapsulation(char *key, const struct affine_point *Q, const struct curve_params *cp) { struct affine_point Z, R; gcry_mpi_t k; Step1: k = get_random_exponent(cp); R = pointmul(&cp->dp.base, k, &cp->dp); gcry_mpi_mul_ui(k, k, cp->dp.cofactor); Z = pointmul(Q, k, &cp->dp); gcry_mpi_release(k); if (point_is_zero(&Z)) { point_release(&R); point_release(&Z); goto Step1; } ECIES_KDF(key, Z.x, &R, cp->elem_len_bin); point_release(&Z); return R; } int ECIES_decapsulation(char *key, const struct affine_point *R, const gcry_mpi_t d, const struct curve_params *cp) { struct affine_point Z; gcry_mpi_t e; int res = 0; if (! embedded_key_validation(R, &cp->dp)) return 0; e = gcry_mpi_snew(0); gcry_mpi_mul_ui(e, d, cp->dp.cofactor); Z = pointmul(R, e, &cp->dp); gcry_mpi_release(e); if ((res = ! point_is_zero(&Z))) ECIES_KDF(key, Z.x, R, cp->elem_len_bin); point_release(&Z); return res; } /******************************************************************************/ static void DH_KDF(char *key, const struct affine_point *P, size_t elemlen) { char *buf; assert((buf = gcry_malloc_secure(2 * elemlen))); serialize_mpi(buf + 0 * elemlen, elemlen, DF_BIN, P->x); serialize_mpi(buf + 1 * elemlen, elemlen, DF_BIN, P->y); gcry_md_hash_buffer(GCRY_MD_SHA512, key, buf, 2 * elemlen); gcry_free(buf); } gcry_mpi_t DH_step1(struct affine_point *A, const struct curve_params *cp) { gcry_mpi_t a; a = get_random_exponent(cp); *A = pointmul(&cp->dp.base, a, &cp->dp); return a; } int DH_step2(char *key, const struct affine_point *B, const gcry_mpi_t exp, const struct curve_params *cp) { struct affine_point P; if (! full_key_validation(B, &cp->dp)) return 0; P = pointmul(B, exp, &cp->dp); DH_KDF(key, &P, cp->elem_len_bin); point_release(&P); return 1; } seccure-0.5/seccure.10000664000175000017500000001706212372135467014416 0ustar bertrambertram.TH seccure 1 User Manuals .SH NAME seccure \- SECCURE Elliptic Curve Crypto Utility for Reliable Encryption .SH SYNOPSIS \fBseccure-key [-c \fIcurve\fB] [-F \fIpwfile\fB] [-d] [-v] [-q] seccure-encrypt [-m \fImaclen\fB] [-c \fIcurve\fB] [-i \fIinfile\fB] [-o \fIoutfile\fB] [-v] [-q] \fIkey\fB seccure-decrypt [-m \fImaclen\fB] [-c \fIcurve\fB] [-i \fIinfile\fB] [-o \fIoutfile\fB] [-F \fIpwfile\fB] [-d] [-v] [-q] seccure-sign [-f] [-b] [-a] [-c \fIcurve\fB] [-s \fIsigfile\fB] [-i \fIinfile\fB] [-o \fIoutfile\fB] [-F \fIpwfile\fB] [-d] [-v] [-q] seccure-verify [-f] [-b] [-a] [-c \fIcurve\fB] [-s \fIsigfile\fB] [-i \fIinfile\fB] [-o \fIoutfile\fB] [-v] [-q] \fIkey\fB [\fIsig\fB] seccure-signcrypt [-c \fIsig_curve\fB [-c \fIenc_curve\fB]] [-i \fIinfile\fB] [-o \fIoutfile\fB] [-F \fIpwfile\fB] [-d] [-v] [-q] \fIkey\fB seccure-veridec [-c \fIenc_curve\fB [-c \fIsig_curve\fB]] [-i \fIinfile\fB] [-o \fIoutfile\fB] [-F \fIpwfile\fB] [-d] [-v] [-q] \fIkey\fB seccure-dh [-c \fIcurve\fB] [-v] [-q] \f1 .SH DESCRIPTION The \fBseccure\f1 toolset implements a selection of asymmetric algorithms based on elliptic curve cryptography (ECC). In particular it offers public key encryption / decryption, signature generation / verification and basic key establishment. ECC schemes offer a much better key size to security ratio than classical cryptosystems (RSA, DSA). Keys are short enough to make direct specification of keys on the command line possible (sometimes this is more convenient than the management of PGP-like key rings). \fBseccure\f1 builds on this feature and therefore is the tool of choice whenever lightweight but nevertheless strong asymmetric cryptography -- independent of key servers, revocation certificates, the Web of Trust or even configuration files -- is required. .SH COMMANDS \fBseccure-key\f1: Prompt for a passphrase and calculate the corresponding public key. \fBseccure-encrypt\f1: Encrypt a message with public key \fIkey\f1. \fBseccure-decrypt\f1: Prompt for a passphrase and decrypt a \fBseccure-encrypt\f1ed message. \fBseccure-sign\f1: Prompt for a passphrase and digitally sign a message. \fBseccure-verify\f1: Verify signature \fIsig\f1 with public key \fIkey\f1. \fBseccure-signcrypt\f1: Sign a message first, encrypt it subsequently (in \fB-b -a\f1 and \fB-m 0\f1 mode, respectively). This is basically a shortcut for two separate \fBseccure\f1 invocations. \fBseccure-veridec\f1: Counterpart to signcryption. \fBseccure-dh\f1: Perform a Diffie-Hellman key exchange. .SH OPTIONS .TP \fB-c \fIcurve\fB\f1 Use elliptic curve \fIcurve\f1. Available are: \fIsecp112r1\f1, \fIsecp128r1\f1, \fIsecp160r1\f1, \fIsecp192r1/nistp192\f1, \fIsecp224r1/nistp224\f1, \fIsecp256r1/nistp256\f1, \fIsecp384r1/nistp384\f1, \fIsecp521r1/nistp521\f1, \fIbrainpoolp160r1\f1, \fIbrainpoolp192r1\f1, \fIbrainpoolp224r1\f1, \fIbrainpoolp256r1\f1, \fIbrainpoolp320r1\f1, \fIbrainpoolp384r1\f1, and \fIbrainpoolp512r1\f1. The curve name may be abbreviated by any non-ambiguous substring (for instance it is suggested to specify \fIp224\f1 for the \fIsecp224r1/nistp224\f1 curve). The default curve is \fIp160\f1, which provides reasonable security for everyday use. (See also \fBHOW TO CHOOSE THE CURVE\f1.) Note: If a public key is given on the command line, for all SECP and NIST curves \fBseccure\f1 can determine the corresponding curve on its own. It is then unnecessary to specify the curve explicitly. Brainpool curves cannot be recognized automatically. .TP \fB-F \fIpwfile\fB\f1 Don't prompt for a passphrase; instead, take the first text line of \fIpwfile\f1. .TP \fB-m \fImaclen\fB\f1 Set the MAC length to \fImaclen\f1 bits. Only multiples of 8 in the range from 0 to 256 are allowed. The default MAC length is 80 bits, which provides a reasonable level of integrity protection for everyday use. .TP \fB-i \fIinfile\fB\f1 Read from \fIinfile\f1 instead of STDIN. .TP \fB-o \fIoutfile\fB\f1 Write to \fIoutfile\f1 instead of STDOUT. .TP \fB-s \fIsigfile\fB\f1 For \fBseccure-sign\f1: Write signature to \fIsigfile\f1 instead of STDERR. For \fBseccure-verify\f1: Read signature from \fIsigfile\f1 instead of using \fIsig\f1. .TP \fB-f\f1 Filter mode: Copy all data read from STDIN verbatim to STDOUT (eventually attaching or detaching a signature in \fB-a\f1 mode). .TP \fB-b\f1 Binary mode: Read/write signatures as binary strings. This leads to very compact signatures. .TP \fB-a\f1 Append mode: For \fBseccure-sign\f1: Append signature to the end of the document. This enforces \fB-f\f1 mode. For \fBseccure-verify\f1: Detach signature from the end of the document. .TP \fB-d\f1 Double prompt mode: When reading a passphrase from the console: prompt twice and assure the phrases are the same. .TP \fB-v\f1 Verbose mode: Print some extra information. .TP \fB-q\f1 Quiet mode: Disable all unnecessary output. .SH EXIT STATUS All commands in the \fBseccure\f1 software suite exit with a status of zero if the desired operation could be completed successfully. Any error leads to a nonzero exit code. .SH EXAMPLE Given the passphrase 'seccure is secure', run \fBseccure-key\f1 to determine the corresponding public key (which is '2@DupCaCKykHBe-QHpAP%d%B[' on curve \fIp160\f1). To encrypt the file 'document.msg' with that key run \fBseccure-encrypt -i document.msg -o document.enc '2@DupCaCKykHBe-QHpAP%d%B['\f1 The message can be recovered with \fBseccure-decrypt -i document.enc\f1 To sign the file run \fBseccure-sign -i document.msg -s document.sig\f1 and enter the passphrase. The signature is stored in 'document.sig' and can be verified with \fBseccure-verify -i document.msg -s document.sig '2@DupCaCKykHBe-QHpAP%d%B['\f1 .SH KEY ESTABLISHMENT \fBseccure-dh\f1 performs an interactive Diffie-Hellman key exchange. Two instances have to be run in parallel; the token generated by the first instance is the input for the second one and vice versa. The output consists of two shared keys: it is guaranteed that no attacker can ever find out (more precisely, distinguished from random) the established key as soon as the two parties can confirm that both have the same verification key. The authentic comparision of the verification keys can, for example, be realized via signed messages or via telephone (using 'voice authentication'). .SH HOW TO CHOOSE THE CURVE The number in the name of a curve measures its security level. Rule of thumb: the workload to 'break' a k-bit curve is 2^(k/2) approximately (example: it takes about 2^112 steps to break \fIsecp224r1\f1). If the 80 bit security of the default curve doesn't seem sufficient, choosing a stronger curve (\fIp192\f1 and upwards) may, of course, be considered. But the suggestion remains: \fIp160\f1 offers reasonable security for everyday use. \fBWarning:\f1 the curves \fIp112\f1 and \fIp128\f1 do not satisfy demands for long-time security. .SH ALGORITHMS \fBseccure\f1 uses derivated versions of ECIES (Elliptic Curve Integrated Encryption Scheme), ECDSA (Elliptic Curve Digital Signature Algorithm) and ECDH (Elliptic Curve Diffie-Hellman) as encryption, signature and key establishment scheme, respectively. For the symmetric parts (bulk encryption, hashing, key derivation, HMAC calculation) \fBseccure\f1 builds on AES256 (in CTR mode), SHA256 and SHA512. To my best knowledge no part of \fBseccure\f1 is covered by patents. See the file PATENTS for an explicit patent statement. .SH AUTHOR This software (v0.5) was written by B. Poettering (seccure AT point-at-infinity.org) in 2006-2014. It is released under the terms of the GNU Lesser General Public License (LGPLv3). Find the latest version of \fBseccure\f1 on the project's homepage: \fBhttp://point-at-infinity.org/seccure/\f1. seccure-0.5/serialize.c0000644000175000017500000001013312372135467015024 0ustar bertrambertram/* * seccure - Copyright 2014 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program. If not, see * . */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which * is part of the GnuPG project). Use the included Makefile to build * the binary. * * Report bugs to: seccure AT point-at-infinity.org * */ #include #include #include #include "serialize.h" /******************************************************************************/ /* All ASCII characters in the range 33..126 excluding '\' and all quotes: */ const char compact_digits[COMPACT_DIGITS_COUNT] = { '!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', ']', '^', '_', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~' }; size_t get_serialization_len(const gcry_mpi_t x, enum disp_format df) { size_t res; switch(df) { case DF_BIN: res = (gcry_mpi_get_nbits(x) + 7) / 8; break; case DF_COMPACT: do { gcry_mpi_t base, Q; base = gcry_mpi_set_ui(NULL, COMPACT_DIGITS_COUNT); Q = gcry_mpi_copy(x); for(res = 0; gcry_mpi_cmp_ui(Q, 0); res++) gcry_mpi_div(Q, NULL, Q, base, 0); gcry_mpi_release(base); gcry_mpi_release(Q); } while (0); break; default: assert(0); } return res; } void serialize_mpi(char *outbuf, size_t outlen, enum disp_format df, const gcry_mpi_t x) { switch(df) { case DF_BIN: do { size_t len = (gcry_mpi_get_nbits(x) + 7) / 8; assert(len <= outlen); memset(outbuf, 0, outlen - len); gcry_mpi_print(GCRYMPI_FMT_USG, (unsigned char*)outbuf + (outlen - len), len, NULL, x); } while (0); break; case DF_COMPACT: do { gcry_mpi_t base, Q, R; int i; base = gcry_mpi_set_ui(NULL, COMPACT_DIGITS_COUNT); Q = gcry_mpi_copy(x); R = gcry_mpi_snew(0); for(i = outlen - 1; i >= 0; i--) { unsigned char digit = 0; gcry_mpi_div(Q, R, Q, base, 0); gcry_mpi_print(GCRYMPI_FMT_USG, &digit, 1, NULL, R); assert(digit < COMPACT_DIGITS_COUNT); outbuf[i] = compact_digits[digit]; } assert(! gcry_mpi_cmp_ui(Q, 0)); gcry_mpi_release(base); gcry_mpi_release(Q); gcry_mpi_release(R); } while (0); break; default: assert(0); } } int deserialize_mpi(gcry_mpi_t *x, enum disp_format df, const char *buf, size_t inlen) { switch(df) { case DF_BIN: gcry_mpi_scan(x, GCRYMPI_FMT_USG, buf, inlen, NULL); gcry_mpi_set_flag(*x, GCRYMPI_FLAG_SECURE); break; case DF_COMPACT: do { char *d; size_t i; *x = gcry_mpi_snew(0); for(i = 0; i < inlen; i++) { if (! (d = memchr(compact_digits, buf[i], COMPACT_DIGITS_COUNT))) { gcry_mpi_release(*x); return 0; } gcry_mpi_mul_ui(*x, *x, COMPACT_DIGITS_COUNT); gcry_mpi_add_ui(*x, *x, d - compact_digits); } } while (0); break; default: assert(0); } return 1; }