Add possibility to disabling encrypt/decrypt padding. 01/82201/5
authorDariusz Michaluk <d.michaluk@samsung.com>
Mon, 1 Aug 2016 12:35:06 +0000 (14:35 +0200)
committerDariusz Michaluk <d.michaluk@samsung.com>
Thu, 4 Aug 2016 10:16:10 +0000 (12:16 +0200)
Change-Id: I42bbd36013b6d39917f6946f35d533dc4f0dbd8e

api/yaca/yaca_simple.h
api/yaca/yaca_types.h
src/debug.c
src/encrypt.c

index 34a238c..e151397 100755 (executable)
@@ -25,6 +25,7 @@
  *           - All operations are single-shot (no streaming possible)
  *           - Context is not used
  *           - For now only digest and symmetric ciphers are supported
+ *           - Disabling PKCS#5 padding for ECB and CBC chaining is not supported.
  *           - GCM and CCM chaining is not supported
  *           - All outputs are allocated by the library
  */
@@ -82,7 +83,7 @@ int yaca_simple_calculate_digest(yaca_digest_algorithm_e algo,
  *
  * @param[in]  algo            Encryption algorithm (select #YACA_ENCRYPT_AES if unsure)
  * @param[in]  bcm             Chaining mode (select #YACA_BCM_CBC if unsure)
- * @param[in]  sym_key         Symmetric encryption key (see key.h for key generation functions)
+ * @param[in]  sym_key         Symmetric encryption key (see yaca_key.h for key generation functions)
  * @param[in]  iv              Initialization vector
  * @param[in]  plaintext       Plaintext to be encrypted
  * @param[in]  plaintext_len   Length of the plaintext
index 81fcf92..8a3b1b9 100755 (executable)
@@ -441,7 +441,12 @@ typedef enum {
 
        /**
         * ECB block cipher mode.
-        * Encrypts 64 bit at a time. No IV is used.
+        * No IV is used.
+        *
+        * By default the input data is padded using standard block padding (aka PKCS#5 padding).
+        * 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.
+        * #YACA_PROPERTY_PADDING can be set at the latest before the *_finalize() call.
         */
        YACA_BCM_ECB,
 
@@ -454,6 +459,11 @@ typedef enum {
        /**
         * CBC block cipher mode.
         * 16-byte initialization vector is mandatory.
+        *
+        * By default the input data is padded using standard block padding (aka PKCS#5 padding).
+        * 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.
+        * #YACA_PROPERTY_PADDING can be set at the latest before the *_finalize() call.
         */
        YACA_BCM_CBC,
 
@@ -575,7 +585,7 @@ typedef enum {
  */
 typedef enum {
        /**
-        * Padding for the sign/verify operation. Property type is #yaca_padding_e.
+        * Padding for the encrypt/decrypt or sign/verify operation. Property type is #yaca_padding_e.
         *
         * This property can be set at the latest before the *_finalize() call.
         */
index 9e5164b..7f9d470 100644 (file)
@@ -127,6 +127,8 @@ int error_handle(const char *file, int line, const char *function)
        case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED):
        case ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_READ_BIO, PEM_R_NO_START_LINE):
        case ERR_PACK(ERR_LIB_ASN1, ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_NOT_ENOUGH_DATA):
+       case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH):
+       case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH):
                ret = YACA_ERROR_INVALID_PARAMETER;
                break;
        case ERR_PACK(ERR_LIB_ASN1, ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG):
index 4fa6965..d1c756c 100644 (file)
@@ -494,6 +494,17 @@ int set_encrypt_property(yaca_context_h ctx,
 
                ret = encrypt_ctx_set_ccm_tag_len(c, *(size_t*)value);
                break;
+       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)
+                       return YACA_ERROR_INVALID_PARAMETER;
+
+               if (EVP_CIPHER_CTX_set_padding(c->cipher_ctx, 0) != 1) {
+                       ERROR_DUMP(YACA_ERROR_INTERNAL);
+                       return YACA_ERROR_INTERNAL;
+               }
+               break;
        default:
                return YACA_ERROR_INVALID_PARAMETER;
        }
@@ -828,11 +839,8 @@ int encrypt_finalize(yaca_context_h ctx,
 
        if (EVP_CIPHER_CTX_mode(c->cipher_ctx) != EVP_CIPH_WRAP_MODE) {
                ret = EVP_CipherFinal(c->cipher_ctx, output, &loutput_len);
-               if (ret != 1 || loutput_len < 0) {
-                       ret = YACA_ERROR_INTERNAL;
-                       ERROR_DUMP(ret);
-                       return ret;
-               }
+               if (ret != 1 || loutput_len < 0)
+                       return ERROR_HANDLE();
        }
 
        *output_len = loutput_len;