You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@subversion.apache.org by cm...@apache.org on 2012/04/02 21:10:27 UTC
svn commit: r1308470 - in /subversion/trunk/subversion: libsvn_subr/crypto.c
tests/libsvn_subr/crypto-test.c
Author: cmpilato
Date: Mon Apr 2 19:10:26 2012
New Revision: 1308470
URL: http://svn.apache.org/viewvc?rev=1308470&view=rev
Log:
Finish up the initial password encryption and decryption routines, and
stare in amazement as the tests now pass. For me. On my box.
* subversion/libsvn_subr/crypto.c
(svn_crypto__encrypt_password): Per our plan, prepend the prefix to
the password and pad up to the specified block size before
encrypting. Lose a seemingly unfounded assertion (that the size
of the input will match that of the output).
(svn_crypto__decrypt_password): Implement this function.
* subversion/tests/libsvn_subr/crypto-test.c
(encrypt_decrypt): New function, abstracted out of ...
(test_encrypt_decrypt_password): ...this, which now calls the helper
function in a loop so different password lengths can be tested.
(test_funcs): Now expect test_encrypt_decrypt_password() to pass.
Modified:
subversion/trunk/subversion/libsvn_subr/crypto.c
subversion/trunk/subversion/tests/libsvn_subr/crypto-test.c
Modified: subversion/trunk/subversion/libsvn_subr/crypto.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_subr/crypto.c?rev=1308470&r1=1308469&r2=1308470&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_subr/crypto.c (original)
+++ subversion/trunk/subversion/libsvn_subr/crypto.c Mon Apr 2 19:10:26 2012
@@ -238,7 +238,8 @@ svn_crypto__encrypt_password(const svn_s
const unsigned char *prefix;
apr_crypto_block_t *block_ctx = NULL;
apr_size_t block_size;
- svn_string_t *assembled;
+ unsigned char *assembled;
+ apr_size_t password_len, assembled_len = 0;
apr_size_t result_len;
unsigned char *result;
apr_size_t ignored_result_len = 0;
@@ -265,10 +266,6 @@ svn_crypto__encrypt_password(const svn_s
/* Generate the proper length IV. */
SVN_ERR(get_random_bytes(&iv_vector, ctx, iv_len, result_pool));
- /* Generate a 4-byte prefix. */
-#define PREFIX_LEN 4
- SVN_ERR(get_random_bytes(&prefix, ctx, PREFIX_LEN, scratch_pool));
-
/* Initialize block encryption. */
apr_err = apr_crypto_block_encrypt_init(&block_ctx, &iv_vector, key,
&block_size, scratch_pool);
@@ -277,14 +274,40 @@ svn_crypto__encrypt_password(const svn_s
ctx, apr_err,
_("Error initializing block encryption")));
- /* ### We need to actually use the prefix, and add appropriate pad */
- assembled = svn_string_create(password, scratch_pool);
+ /* Generate a 4-byte prefix. */
+#define PREFIX_LEN 4
+ SVN_ERR(get_random_bytes(&prefix, ctx, PREFIX_LEN, scratch_pool));
+ /* Combine our prefix, original password, and appropriate padding.
+ We won't bother padding if the prefix and password combined
+ perfectly align on the block boundary. If they don't,
+ however, we'll drop a NUL byte after the password and pad with
+ random stuff after that to the block boundary. */
+ password_len = strlen(password);
+ assembled_len = PREFIX_LEN + password_len;
+ if ((assembled_len % block_size) == 0)
+ {
+ assembled = apr_palloc(scratch_pool, assembled_len);
+ memcpy(assembled, prefix, PREFIX_LEN);
+ memcpy(assembled + PREFIX_LEN, password, password_len);
+ }
+ else
+ {
+ const unsigned char *padding;
+ apr_size_t pad_len = block_size - (assembled_len % block_size) - 1;
+
+ SVN_ERR(get_random_bytes(&padding, ctx, pad_len, scratch_pool));
+ assembled_len = assembled_len + 1 + pad_len;
+ assembled = apr_palloc(scratch_pool, assembled_len);
+ memcpy(assembled, prefix, PREFIX_LEN);
+ memcpy(assembled + PREFIX_LEN, password, password_len);
+ *(assembled + PREFIX_LEN + password_len) = '\0';
+ memcpy(assembled + PREFIX_LEN + password_len + 1, padding, pad_len);
+ }
+
/* Get the length that we need to allocate. */
- apr_err = apr_crypto_block_encrypt(NULL, &result_len,
- (unsigned char *)assembled->data,
- assembled->len,
- block_ctx);
+ apr_err = apr_crypto_block_encrypt(NULL, &result_len, assembled,
+ assembled_len, block_ctx);
if (apr_err != APR_SUCCESS)
{
err = crypto_error_create(ctx, apr_err,
@@ -292,18 +315,12 @@ svn_crypto__encrypt_password(const svn_s
goto cleanup;
}
- /* The result length should precisely match our padded input. If not,
- then something weird is going on with the crypto libraries. */
- SVN_ERR_ASSERT(result_len == assembled->len);
-
/* Allocate our result buffer. */
result = apr_palloc(result_pool, result_len);
/* Encrypt the block. */
- apr_err = apr_crypto_block_encrypt(&result, &result_len,
- (unsigned char *)assembled->data,
- assembled->len,
- block_ctx);
+ apr_err = apr_crypto_block_encrypt(&result, &result_len, assembled,
+ assembled_len, block_ctx);
if (apr_err != APR_SUCCESS)
{
err = crypto_error_create(ctx, apr_err,
@@ -343,7 +360,71 @@ svn_crypto__decrypt_password(const char
apr_pool_t *result_pool,
apr_pool_t *scratch_pool)
{
- return svn_error_create(SVN_ERR_UNSUPPORTED_FEATURE, NULL, NULL);
+ svn_error_t *err;
+ apr_status_t apr_err;
+ apr_crypto_block_t *block_ctx = NULL;
+ apr_size_t block_size, iv_len;
+ apr_crypto_key_t *key = NULL;
+ unsigned char *result;
+ apr_size_t result_len = 0, final_len = 0;
+
+ /* Initialize the passphrase. */
+ apr_err = apr_crypto_passphrase(&key, &iv_len,
+ master->data, master->len,
+ (unsigned char *)salt->data, salt->len,
+ APR_KEY_AES_256, APR_MODE_CBC,
+ FALSE /* doPad */, NUM_ITERATIONS,
+ ctx->crypto, scratch_pool);
+ if (apr_err != APR_SUCCESS)
+ return svn_error_trace(crypto_error_create(
+ ctx, apr_err, _("Error creating derived key")));
+
+ apr_err = apr_crypto_block_decrypt_init(&block_ctx, &block_size,
+ (unsigned char *)iv->data,
+ key, scratch_pool);
+ if ((apr_err != APR_SUCCESS) || (! block_ctx))
+ return svn_error_trace(crypto_error_create(
+ ctx, apr_err,
+ _("Error initializing block decryption")));
+
+ apr_err = apr_crypto_block_decrypt(NULL, &result_len,
+ (unsigned char *)ciphertext->data,
+ ciphertext->len, block_ctx);
+ if (apr_err != APR_SUCCESS)
+ {
+ err = crypto_error_create(ctx, apr_err,
+ _("Error fetching result length"));
+ goto cleanup;
+ }
+
+ result = apr_palloc(scratch_pool, result_len);
+ apr_err = apr_crypto_block_decrypt(&result, &result_len,
+ (unsigned char *)ciphertext->data,
+ ciphertext->len, block_ctx);
+ if (apr_err != APR_SUCCESS)
+ {
+ err = crypto_error_create(ctx, apr_err,
+ _("Error during block decryption"));
+ goto cleanup;
+ }
+
+ apr_err = apr_crypto_block_decrypt_finish(result + result_len, &final_len,
+ block_ctx);
+ if (apr_err != APR_SUCCESS)
+ {
+ err = crypto_error_create(ctx, apr_err,
+ _("Error finalizing block decryption"));
+ goto cleanup;
+ }
+
+ /* Copy the non-random bits of the resulting plaintext, skipping the
+ prefix and ignoring any trailing padding. */
+ *plaintext = apr_pstrndup(result_pool, (const char *)(result + PREFIX_LEN),
+ result_len + final_len - PREFIX_LEN);
+
+ cleanup:
+ apr_crypto_block_cleanup(block_ctx);
+ return err;
}
#endif /* APU_HAVE_CRYPTO */
Modified: subversion/trunk/subversion/tests/libsvn_subr/crypto-test.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/tests/libsvn_subr/crypto-test.c?rev=1308470&r1=1308469&r2=1308470&view=diff
==============================================================================
--- subversion/trunk/subversion/tests/libsvn_subr/crypto-test.c (original)
+++ subversion/trunk/subversion/tests/libsvn_subr/crypto-test.c Mon Apr 2 19:10:26 2012
@@ -23,23 +23,25 @@
#include <stdio.h>
#include <string.h>
+#include <svn_pools.h>
#include "../svn_test.h"
#include "../../libsvn_subr/crypto.h"
#if APU_HAVE_CRYPTO
+/* Helper function: encrypt PASSWORD within CTX using MASTER, then
+ decrypt those results and ensure the original PASSWORD comes out
+ the other end. */
static svn_error_t *
-test_encrypt_decrypt_password(apr_pool_t *pool)
+encrypt_decrypt(svn_crypto__ctx_t *ctx,
+ const svn_string_t *master,
+ const char *password,
+ apr_pool_t *pool)
{
- const svn_string_t *ciphertext, *iv, *salt, *master;
- const char *password, *password_again;
- svn_crypto__ctx_t *ctx;
-
- master = svn_string_create("Pastor Massword", pool);
- password = "3ncryptm3!";
+ const svn_string_t *ciphertext, *iv, *salt;
+ const char *password_again;
- SVN_ERR(svn_crypto__context_create(&ctx, pool));
SVN_ERR(svn_crypto__encrypt_password(&ciphertext, &iv, &salt, ctx,
password, master, pool, pool));
if (! ciphertext)
@@ -67,6 +69,35 @@ test_encrypt_decrypt_password(apr_pool_t
" orig (%s)\n"
" new (%s)\n",
password, password_again);
+
+ return SVN_NO_ERROR;
+}
+
+
+static svn_error_t *
+test_encrypt_decrypt_password(apr_pool_t *pool)
+{
+ svn_crypto__ctx_t *ctx;
+ const svn_string_t *master = svn_string_create("Pastor Massword", pool);
+ int i;
+ apr_pool_t *iterpool;
+ const char *passwords[] = {
+ "3ncryptm!3", /* fits in one block */
+ "this is a particularly long password", /* spans blocks */
+ "mypassphrase", /* with 4-byte padding, should align on block boundary */
+ };
+
+
+ SVN_ERR(svn_crypto__context_create(&ctx, pool));
+
+ iterpool = svn_pool_create(pool);
+ for (i = 0; i < (sizeof(passwords) / sizeof(const char *)); i++)
+ {
+ svn_pool_clear(iterpool);
+ SVN_ERR(encrypt_decrypt(ctx, master, passwords[i], iterpool));
+ }
+
+ svn_pool_destroy(iterpool);
return SVN_NO_ERROR;
}
@@ -81,7 +112,7 @@ struct svn_test_descriptor_t test_funcs[
{
SVN_TEST_NULL,
#if APU_HAVE_CRYPTO
- SVN_TEST_XFAIL2(test_encrypt_decrypt_password,
+ SVN_TEST_PASS2(test_encrypt_decrypt_password,
"basic password encryption/decryption test"),
#endif /* APU_HAVE_CRYPTO */
SVN_TEST_NULL
Re: svn commit: r1308470 - in /subversion/trunk/subversion:
libsvn_subr/crypto.c tests/libsvn_subr/crypto-test.c
Posted by Daniel Shahaf <da...@elego.de>.
C. Michael Pilato wrote on Mon, Apr 02, 2012 at 16:10:43 -0400:
> On 04/02/2012 03:40 PM, Greg Stein wrote:
> > On Mon, Apr 2, 2012 at 15:10, <cm...@apache.org> wrote:
> >> Author: cmpilato
> >> Date: Mon Apr 2 19:10:26 2012
> >> New Revision: 1308470
> >>
> >> URL: http://svn.apache.org/viewvc?rev=1308470&view=rev
> >> Log:
> >> Finish up the initial password encryption and decryption routines, and
> >> stare in amazement as the tests now pass. For me. On my box.
> >>
> >> * subversion/libsvn_subr/crypto.c
> >> (svn_crypto__encrypt_password): Per our plan, prepend the prefix to
> >> the password and pad up to the specified block size before
> >> encrypting. Lose a seemingly unfounded assertion (that the size
> >> of the input will match that of the output).
> >
> > What were you seeing? The encryption result *should* match the amount
> > of data we passed. N blocks of input data for N blocks of output data.
> > If they don't match, then something *is* wrong, as noted in the
> > assertion's comment.
>
> I saw 16 bytes go in, and 48 come out. And then I saw that when I removed
> the assertion and completed the encryption and decryption functions, my
> round-robin tests passed. :-)
>
> Out of curiosity, why must the input block size match the output block size?
> I mean, it sounds great and all, but wouldn't that make the whole "let's
> calculate the maximum possible result size" step sorta superfluous?
>
> Or maybe that's what's going on here: the docstring for
> apr_crypto_block_encrypt() sez "If out is NULL, outlen will contain the
> maximum size of the buffer needed to hold the data". Not the exact size --
> just a good, safe, maximum size. Maybe the implementation is being
> extremely conservative?
Maybe the implementation is prepending the IV to the ciphertext?
Re: svn commit: r1308470 - in /subversion/trunk/subversion: libsvn_subr/crypto.c
tests/libsvn_subr/crypto-test.c
Posted by "C. Michael Pilato" <cm...@collab.net>.
On 04/02/2012 03:40 PM, Greg Stein wrote:
> On Mon, Apr 2, 2012 at 15:10, <cm...@apache.org> wrote:
>> Author: cmpilato
>> Date: Mon Apr 2 19:10:26 2012
>> New Revision: 1308470
>>
>> URL: http://svn.apache.org/viewvc?rev=1308470&view=rev
>> Log:
>> Finish up the initial password encryption and decryption routines, and
>> stare in amazement as the tests now pass. For me. On my box.
>>
>> * subversion/libsvn_subr/crypto.c
>> (svn_crypto__encrypt_password): Per our plan, prepend the prefix to
>> the password and pad up to the specified block size before
>> encrypting. Lose a seemingly unfounded assertion (that the size
>> of the input will match that of the output).
>
> What were you seeing? The encryption result *should* match the amount
> of data we passed. N blocks of input data for N blocks of output data.
> If they don't match, then something *is* wrong, as noted in the
> assertion's comment.
I saw 16 bytes go in, and 48 come out. And then I saw that when I removed
the assertion and completed the encryption and decryption functions, my
round-robin tests passed. :-)
Out of curiosity, why must the input block size match the output block size?
I mean, it sounds great and all, but wouldn't that make the whole "let's
calculate the maximum possible result size" step sorta superfluous?
Or maybe that's what's going on here: the docstring for
apr_crypto_block_encrypt() sez "If out is NULL, outlen will contain the
maximum size of the buffer needed to hold the data". Not the exact size --
just a good, safe, maximum size. Maybe the implementation is being
extremely conservative?
--
C. Michael Pilato <cm...@collab.net>
CollabNet <> www.collab.net <> Distributed Development On Demand
Re: svn commit: r1308470 - in /subversion/trunk/subversion:
libsvn_subr/crypto.c tests/libsvn_subr/crypto-test.c
Posted by Greg Stein <gs...@gmail.com>.
On Mon, Apr 2, 2012 at 15:10, <cm...@apache.org> wrote:
> Author: cmpilato
> Date: Mon Apr 2 19:10:26 2012
> New Revision: 1308470
>
> URL: http://svn.apache.org/viewvc?rev=1308470&view=rev
> Log:
> Finish up the initial password encryption and decryption routines, and
> stare in amazement as the tests now pass. For me. On my box.
>
> * subversion/libsvn_subr/crypto.c
> (svn_crypto__encrypt_password): Per our plan, prepend the prefix to
> the password and pad up to the specified block size before
> encrypting. Lose a seemingly unfounded assertion (that the size
> of the input will match that of the output).
What were you seeing? The encryption result *should* match the amount
of data we passed. N blocks of input data for N blocks of output data.
If they don't match, then something *is* wrong, as noted in the
assertion's comment.
>...
Cheers,
-g