Add 'no padding' mode
diff --git a/library/cipher.c b/library/cipher.c
index bbde944..70eef69 100644
--- a/library/cipher.c
+++ b/library/cipher.c
@@ -657,6 +657,23 @@
return 0;
}
+/*
+ * No padding: don't pad :)
+ *
+ * There is no add_padding function (check for NULL in cipher_finish)
+ * but a trivial get_padding function
+ */
+static int get_no_padding( unsigned char *input, size_t input_len,
+ size_t *data_len )
+{
+ if( NULL == input || NULL == data_len )
+ return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
+
+ *data_len = input_len;
+
+ return 0;
+}
+
int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
{
int ret = 0;
@@ -677,12 +694,27 @@
{
if( POLARSSL_ENCRYPT == ctx->operation )
{
+ /* check for 'no padding' mode */
+ if( NULL == ctx->add_padding )
+ {
+ if( 0 != ctx->unprocessed_len )
+ return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
+
+ return 0;
+ }
+
ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
ctx->unprocessed_len );
}
else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
{
- /* For decrypt operations, expect a full block */
+ /*
+ * For decrypt operations, expect a full block,
+ * or an empty block if no padding
+ */
+ if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
+ return 0;
+
return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
}
@@ -743,6 +775,13 @@
return 0;
}
+ if( POLARSSL_PADDING_NONE == mode )
+ {
+ ctx->add_padding = NULL;
+ ctx->get_padding = get_no_padding;
+ return 0;
+ }
+
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
}