Add possibility to restore default encrypt/decrypt padding. 30/85530/4
authorDariusz Michaluk <d.michaluk@samsung.com>
Thu, 25 Aug 2016 14:09:11 +0000 (16:09 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 29 Aug 2016 13:20:56 +0000 (06:20 -0700)
Introduce new padding type: YACA_PADDING_PKCS7.

Change-Id: I841444c63bfca7a523a2a8df302c8aa38b81e59a

api/yaca/yaca_types.h
src/encrypt.c
src/internal.h

index 8bc8b0f..2032936 100755 (executable)
@@ -451,7 +451,7 @@ typedef enum {
         * ECB block cipher mode.
         * Initialization Vector is not used.
         *
-        * By default the input data is padded using standard block padding (aka PKCS#5 padding).
+        * By default the input data is padded using standard block padding (#YACA_PADDING_PKCS7).
         * Padding can be disabled using yaca_context_set_property() and
         * #YACA_PROPERTY_PADDING,#YACA_PADDING_NONE,
         * then the total length of data passed until *_finalize() MUST be a multiple of block size.
@@ -471,7 +471,7 @@ typedef enum {
         * 128-bit Initialization Vector for AES,
         * 64-bit for other algorithms is mandatory.
         *
-        * By default the input data is padded using standard block padding (aka PKCS#5 padding).
+        * By default the input data is padded using standard block padding (#YACA_PADDING_PKCS7).
         * Padding can be disabled using yaca_context_set_property() and
         * #YACA_PROPERTY_PADDING, #YACA_PADDING_NONE,
         * then the total length of data passed until *_finalize() MUST be a multiple of block size.
@@ -642,6 +642,8 @@ typedef enum {
        YACA_PADDING_PKCS1,
        /** RSA signature/verify operations */
        YACA_PADDING_PKCS1_PSS,
+       /** PKCS #7 padding. Suitable for symmetric encrypt/decrypt operation. */
+       YACA_PADDING_PKCS7
 } yaca_padding_e;
 
 /**
index fb5edac..03c515a 100644 (file)
@@ -506,7 +506,7 @@ static int encrypt_ctx_backup(struct yaca_encrypt_context_s *c,
        bc->cipher = cipher;
        bc->sym_key = key_copy(sym_key);
        bc->iv = key_copy(iv);
-       bc->padding_none = false;
+       bc->padding = YACA_PADDING_PKCS7;
 
        c->backup_ctx = bc;
 
@@ -534,7 +534,8 @@ static int encrypt_ctx_restore(struct yaca_encrypt_context_s *c)
        ret = encrypt_ctx_init(c, c->backup_ctx->cipher, key->bit_len);
        assert(ret != YACA_ERROR_INVALID_PARAMETER);
 
-       if (c->backup_ctx->padding_none && EVP_CIPHER_CTX_set_padding(c->cipher_ctx, 0) != 1) {
+       if (c->backup_ctx->padding == YACA_PADDING_NONE &&
+           EVP_CIPHER_CTX_set_padding(c->cipher_ctx, 0) != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
                return ret;
@@ -739,16 +740,18 @@ int set_encrypt_property(yaca_context_h ctx,
        case YACA_PROPERTY_PADDING:
                if ((mode != EVP_CIPH_ECB_MODE && mode != EVP_CIPH_CBC_MODE) ||
                    value_len != sizeof(yaca_padding_e) ||
-                   *(yaca_padding_e*)value != YACA_PADDING_NONE ||
+                   (*(yaca_padding_e*)value != YACA_PADDING_NONE &&
+                   *(yaca_padding_e*)value != YACA_PADDING_PKCS7) ||
                    c->state == STATE_FINALIZED)
                        return YACA_ERROR_INVALID_PARAMETER;
 
-               if (EVP_CIPHER_CTX_set_padding(c->cipher_ctx, 0) != 1) {
+               yaca_padding_e padding = *(yaca_padding_e*)value;
+               if (EVP_CIPHER_CTX_set_padding(c->cipher_ctx, padding) != 1) {
                        ERROR_DUMP(YACA_ERROR_INTERNAL);
                        return YACA_ERROR_INTERNAL;
                }
                if (c->backup_ctx != NULL)
-                       c->backup_ctx->padding_none = true;
+                       c->backup_ctx->padding = padding;
                break;
        case YACA_PROPERTY_RC2_EFFECTIVE_KEY_BITS:
                if (value_len != sizeof(size_t) ||
index 107b3a8..553e8a1 100644 (file)
@@ -65,7 +65,7 @@ struct yaca_backup_context_s {
        const EVP_CIPHER *cipher;
        yaca_key_h sym_key;
        yaca_key_h iv;
-       bool padding_none;
+       yaca_padding_e padding;
 };
 
 enum encrypt_context_state_e {