From: jsing Date: Thu, 1 May 2014 16:06:24 +0000 (+0000) Subject: Provide an EVP implementation for ChaCha. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=42e84a6d47332afff76bf07135da7687afc2ee55;p=openbsd Provide an EVP implementation for ChaCha. ok miod@ --- diff --git a/lib/libcrypto/chacha/chacha.c b/lib/libcrypto/chacha/chacha.c index d76d64de4ab..1bc95f502d4 100644 --- a/lib/libcrypto/chacha/chacha.c +++ b/lib/libcrypto/chacha/chacha.c @@ -14,8 +14,28 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include "chacha.h" #include "chacha-merged.c" +void +ChaCha_set_key(ChaCha_ctx *ctx, const unsigned char *key, uint32_t keybits) +{ + chacha_keysetup((chacha_ctx *)ctx, key, keybits); +} + +void +ChaCha_set_iv(ChaCha_ctx *ctx, const unsigned char *iv, + const unsigned char *counter) +{ + chacha_ivsetup((chacha_ctx *)ctx, iv, counter); +} + +void +ChaCha(ChaCha_ctx *ctx, unsigned char *out, const unsigned char *in, size_t len) +{ + chacha_encrypt_bytes((chacha_ctx *)ctx, in, out, (uint32_t)len); +} + void CRYPTO_chacha_20(unsigned char *out, const unsigned char *in, size_t len, const unsigned char key[32], const unsigned char iv[8], size_t counter) diff --git a/lib/libcrypto/chacha/chacha.h b/lib/libcrypto/chacha/chacha.h index d66a719ae43..456d960ed9b 100644 --- a/lib/libcrypto/chacha/chacha.h +++ b/lib/libcrypto/chacha/chacha.h @@ -29,6 +29,17 @@ extern "C" { #endif +typedef struct { + unsigned int input[16]; +} ChaCha_ctx; + +void ChaCha_set_key(ChaCha_ctx *ctx, const unsigned char *key, + unsigned int keybits); +void ChaCha_set_iv(ChaCha_ctx *ctx, const unsigned char *iv, + const unsigned char *counter); +void ChaCha(ChaCha_ctx *ctx, unsigned char *out, const unsigned char *in, + size_t len); + void CRYPTO_chacha_20(unsigned char *out, const unsigned char *in, size_t len, const unsigned char key[32], const unsigned char iv[8], size_t counter); diff --git a/lib/libcrypto/evp/c_allc.c b/lib/libcrypto/evp/c_allc.c index 2a45d435e58..2047b6cd61c 100644 --- a/lib/libcrypto/evp/c_allc.c +++ b/lib/libcrypto/evp/c_allc.c @@ -227,4 +227,8 @@ void OpenSSL_add_all_ciphers(void) EVP_add_cipher_alias(SN_camellia_256_cbc,"CAMELLIA256"); EVP_add_cipher_alias(SN_camellia_256_cbc,"camellia256"); #endif + +#ifndef OPENSSL_NO_CHACHA + EVP_add_cipher(EVP_chacha20()); +#endif } diff --git a/lib/libcrypto/evp/e_chacha.c b/lib/libcrypto/evp/e_chacha.c new file mode 100644 index 00000000000..4a20186006a --- /dev/null +++ b/lib/libcrypto/evp/e_chacha.c @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2014 Joel Sing + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef OPENSSL_NO_CHACHA + +#include +#include +#include + +#include "evp_locl.h" + +static int chacha_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len); +static int chacha_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc); + +static const EVP_CIPHER chacha20_cipher = { + .nid = NID_chacha20, + .block_size = 1, + .key_len = 32, + .iv_len = 8, + .flags = EVP_CIPH_STREAM_CIPHER, + .init = chacha_init, + .do_cipher = chacha_cipher, + .ctx_size = sizeof(ChaCha_ctx) +}; + +const EVP_CIPHER * +EVP_chacha20(void) +{ + return(&chacha20_cipher); +} + +static int +chacha_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + ChaCha_set_key((ChaCha_ctx *)ctx->cipher_data, key, + EVP_CIPHER_CTX_key_length(ctx) * 8); + ChaCha_set_iv((ChaCha_ctx *)ctx->cipher_data, iv, NULL); + return 1; +} + +static int +chacha_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, + size_t len) +{ + ChaCha((ChaCha_ctx *)ctx->cipher_data, out, in, len); + return 1; +} + +#endif diff --git a/lib/libcrypto/evp/evp.h b/lib/libcrypto/evp/evp.h index f43fe33b47d..fa98d4d93c7 100644 --- a/lib/libcrypto/evp/evp.h +++ b/lib/libcrypto/evp/evp.h @@ -838,6 +838,10 @@ const EVP_CIPHER *EVP_seed_cfb128(void); const EVP_CIPHER *EVP_seed_ofb(void); #endif +#ifndef OPENSSL_NO_CHACHA +const EVP_CIPHER *EVP_chacha20(void); +#endif + void OPENSSL_add_all_algorithms_noconf(void); void OPENSSL_add_all_algorithms_conf(void); diff --git a/lib/libcrypto/objects/objects.txt b/lib/libcrypto/objects/objects.txt index 487e0792553..bb44aa77a32 100644 --- a/lib/libcrypto/objects/objects.txt +++ b/lib/libcrypto/objects/objects.txt @@ -1309,3 +1309,7 @@ brainpool 1 13 : brainpoolP512r1 brainpool 1 14 : brainpoolP512t1 1 2 250 1 223 101 256 1 : FRP256v1 + +# ChaCha Stream Cipher +!Cname chacha20 + : ChaCha : chacha diff --git a/lib/libssl/src/crypto/chacha/chacha.c b/lib/libssl/src/crypto/chacha/chacha.c index d76d64de4ab..1bc95f502d4 100644 --- a/lib/libssl/src/crypto/chacha/chacha.c +++ b/lib/libssl/src/crypto/chacha/chacha.c @@ -14,8 +14,28 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include "chacha.h" #include "chacha-merged.c" +void +ChaCha_set_key(ChaCha_ctx *ctx, const unsigned char *key, uint32_t keybits) +{ + chacha_keysetup((chacha_ctx *)ctx, key, keybits); +} + +void +ChaCha_set_iv(ChaCha_ctx *ctx, const unsigned char *iv, + const unsigned char *counter) +{ + chacha_ivsetup((chacha_ctx *)ctx, iv, counter); +} + +void +ChaCha(ChaCha_ctx *ctx, unsigned char *out, const unsigned char *in, size_t len) +{ + chacha_encrypt_bytes((chacha_ctx *)ctx, in, out, (uint32_t)len); +} + void CRYPTO_chacha_20(unsigned char *out, const unsigned char *in, size_t len, const unsigned char key[32], const unsigned char iv[8], size_t counter) diff --git a/lib/libssl/src/crypto/chacha/chacha.h b/lib/libssl/src/crypto/chacha/chacha.h index d66a719ae43..456d960ed9b 100644 --- a/lib/libssl/src/crypto/chacha/chacha.h +++ b/lib/libssl/src/crypto/chacha/chacha.h @@ -29,6 +29,17 @@ extern "C" { #endif +typedef struct { + unsigned int input[16]; +} ChaCha_ctx; + +void ChaCha_set_key(ChaCha_ctx *ctx, const unsigned char *key, + unsigned int keybits); +void ChaCha_set_iv(ChaCha_ctx *ctx, const unsigned char *iv, + const unsigned char *counter); +void ChaCha(ChaCha_ctx *ctx, unsigned char *out, const unsigned char *in, + size_t len); + void CRYPTO_chacha_20(unsigned char *out, const unsigned char *in, size_t len, const unsigned char key[32], const unsigned char iv[8], size_t counter); diff --git a/lib/libssl/src/crypto/evp/c_allc.c b/lib/libssl/src/crypto/evp/c_allc.c index 2a45d435e58..2047b6cd61c 100644 --- a/lib/libssl/src/crypto/evp/c_allc.c +++ b/lib/libssl/src/crypto/evp/c_allc.c @@ -227,4 +227,8 @@ void OpenSSL_add_all_ciphers(void) EVP_add_cipher_alias(SN_camellia_256_cbc,"CAMELLIA256"); EVP_add_cipher_alias(SN_camellia_256_cbc,"camellia256"); #endif + +#ifndef OPENSSL_NO_CHACHA + EVP_add_cipher(EVP_chacha20()); +#endif } diff --git a/lib/libssl/src/crypto/evp/e_chacha.c b/lib/libssl/src/crypto/evp/e_chacha.c new file mode 100644 index 00000000000..4a20186006a --- /dev/null +++ b/lib/libssl/src/crypto/evp/e_chacha.c @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2014 Joel Sing + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef OPENSSL_NO_CHACHA + +#include +#include +#include + +#include "evp_locl.h" + +static int chacha_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len); +static int chacha_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc); + +static const EVP_CIPHER chacha20_cipher = { + .nid = NID_chacha20, + .block_size = 1, + .key_len = 32, + .iv_len = 8, + .flags = EVP_CIPH_STREAM_CIPHER, + .init = chacha_init, + .do_cipher = chacha_cipher, + .ctx_size = sizeof(ChaCha_ctx) +}; + +const EVP_CIPHER * +EVP_chacha20(void) +{ + return(&chacha20_cipher); +} + +static int +chacha_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + ChaCha_set_key((ChaCha_ctx *)ctx->cipher_data, key, + EVP_CIPHER_CTX_key_length(ctx) * 8); + ChaCha_set_iv((ChaCha_ctx *)ctx->cipher_data, iv, NULL); + return 1; +} + +static int +chacha_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, + size_t len) +{ + ChaCha((ChaCha_ctx *)ctx->cipher_data, out, in, len); + return 1; +} + +#endif diff --git a/lib/libssl/src/crypto/evp/evp.h b/lib/libssl/src/crypto/evp/evp.h index f43fe33b47d..fa98d4d93c7 100644 --- a/lib/libssl/src/crypto/evp/evp.h +++ b/lib/libssl/src/crypto/evp/evp.h @@ -838,6 +838,10 @@ const EVP_CIPHER *EVP_seed_cfb128(void); const EVP_CIPHER *EVP_seed_ofb(void); #endif +#ifndef OPENSSL_NO_CHACHA +const EVP_CIPHER *EVP_chacha20(void); +#endif + void OPENSSL_add_all_algorithms_noconf(void); void OPENSSL_add_all_algorithms_conf(void); diff --git a/lib/libssl/src/crypto/objects/objects.txt b/lib/libssl/src/crypto/objects/objects.txt index 487e0792553..bb44aa77a32 100644 --- a/lib/libssl/src/crypto/objects/objects.txt +++ b/lib/libssl/src/crypto/objects/objects.txt @@ -1309,3 +1309,7 @@ brainpool 1 13 : brainpoolP512r1 brainpool 1 14 : brainpoolP512t1 1 2 250 1 223 101 256 1 : FRP256v1 + +# ChaCha Stream Cipher +!Cname chacha20 + : ChaCha : chacha diff --git a/regress/lib/libcrypto/evp/evptest.c b/regress/lib/libcrypto/evp/evptest.c index 164d8f3e019..de1bcce41d1 100644 --- a/regress/lib/libcrypto/evp/evptest.c +++ b/regress/lib/libcrypto/evp/evptest.c @@ -428,6 +428,13 @@ int main(int argc,char **argv) fprintf(stdout, "Cipher disabled, skipping %s\n", cipher); continue; } +#endif +#ifdef OPENSSL_NO_CHACHA + if (strstr(cipher, "ChaCha") == cipher) + { + fprintf(stdout, "Cipher disabled, skipping %s\n", cipher); + continue; + } #endif fprintf(stderr,"Can't find %s\n",cipher); exit(3); diff --git a/regress/lib/libcrypto/evp/evptests.txt b/regress/lib/libcrypto/evp/evptests.txt index c273707c144..bdee5a88f7e 100644 --- a/regress/lib/libcrypto/evp/evptests.txt +++ b/regress/lib/libcrypto/evp/evptests.txt @@ -332,3 +332,13 @@ SEED-ECB:00000000000000000000000000000000::000102030405060708090A0B0C0D0E0F:5EBA SEED-ECB:000102030405060708090A0B0C0D0E0F::00000000000000000000000000000000:C11F22F20140505084483597E4370F43:1 SEED-ECB:4706480851E61BE85D74BFB3FD956185::83A2F8A288641FB9A4E9A5CC2F131C7D:EE54D13EBCAE706D226BC3142CD40D4A:1 SEED-ECB:28DBC3BC49FFD87DCFA509B11D422BE7::B41E6BE2EBA84A148E2EED84593C5EC7:9B9B7BFCD1813CB95D0B3618F40F5122:1 + +# ChaCha test vectors +ChaCha:0000000000000000000000000000000000000000000000000000000000000000:0000000000000000:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000:76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586:1 +ChaCha:0100000000000000000000000000000000000000000000000000000000000000:0000000000000000:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000:c5d30a7ce1ec119378c84f487d775a8542f13ece238a9455e8229e888de85bbd29eb63d0a17a5b999b52da22be4023eb07620a54f6fa6ad8737b71eb0464dac0:1 +ChaCha:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff:ffffffffffffffff:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000:d9bf3f6bce6ed0b54254557767fb57443dd4778911b606055c39cc25e674b8363feabc57fde54f790c52c8ae43240b79d49042b777bfd6cb80e931270b7f50eb:1 +ChaCha:5555555555555555555555555555555555555555555555555555555555555555:aaaaaaaaaaaaaaaa:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000:aff7418293f3a553894b1e7484bd1e8ede196eced5a1d6814de37091e07e076e34bbba8107a686c982850f0a7353940d40db1ab0b5765b78b4cf473d9485a3dd:1 +ChaCha:5555555555555555555555555555555555555555555555555555555555555555:5555555555555555:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000:bea9411aa453c5434a5ae8c92862f564396855a9ea6e22d6d3b50ae1b3663311a4a3606c671d605ce16c3aece8e61ea145c59775017bee2fa6f88afc758069f7:1 +ChaCha:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:aaaaaaaaaaaaaaaa:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000:9aa2a9f656efde5aa7591c5fed4b35aea2895dec7cb4543b9e9f21f5e7bcbcf3c43c748a970888f8248393a09d43e0b7e164bc4d0b0fb240a2d72115c4808906:1 +ChaCha:00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100:0f1e2d3c4b5a6978:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000:9fadf409c00811d00431d67efbd88fba59218d5d6708b1d685863fabbb0e961eea480fd6fb532bfd494b2151015057423ab60a63fe4f55f7a212e2167ccab931:1 +ChaCha:c46ec1b18ce8a878725a37e780dfb7351f68ed2e194c79fbc6aebee1a667975d:1ada31d5cf688221:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000:f63a89b75c2271f9368816542ba52f06ed49241792302b00b5e8f80ae9a473afc25b218f519af0fdd406362e8d69de7f54c604a6e00f353f110f771bdca8ab92:1