Fix sha1 digest length and type mismatch(size_t and uint) 96/84396/4
authorKyungwook Tak <k.tak@samsung.com>
Thu, 18 Aug 2016 10:07:24 +0000 (19:07 +0900)
committerKyungwook Tak <k.tak@samsung.com>
Thu, 18 Aug 2016 23:55:32 +0000 (08:55 +0900)
Change-Id: Ia53c49ea40d225971a3061241ee90c58534eaf65
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
src/manager/service/ss-crypto.cpp

index 1da144a..1e79b48 100644 (file)
 
 #include <dpl/log/log.h>
 
+// lengths defined as macro to be used independent to type (size_t, unsigned int, int)
+#define SALT_SIZE 32
+#define KEY_SIZE 16
+#define IV_SIZE KEY_SIZE
+
 namespace CKM {
 
 namespace {
 
-const int SALT_SIZE = 32;
-const int KEY_SIZE = 16;
-
 RawBuffer _get_key(const std::string &id)
 {
        unsigned char salt[SALT_SIZE];
@@ -56,8 +58,9 @@ RawBuffer _get_key(const std::string &id)
 
 RawBuffer _get_iv(const RawBuffer &src)
 {
-       RawBuffer iv(KEY_SIZE);
-       size_t ivlen = iv.size();
+       RawBuffer iv(SHA_DIGEST_LENGTH);
+
+       unsigned int ivlen = 0;
 
        if (::EVP_Digest(src.data(), src.size(), iv.data(), &ivlen, ::EVP_sha1(), nullptr)
                        != 1) {
@@ -65,6 +68,13 @@ RawBuffer _get_iv(const RawBuffer &src)
                return RawBuffer();
        }
 
+       if (ivlen < IV_SIZE) {
+               LogError("Invalid iv size: " << ivlen);
+               return RawBuffer();
+       }
+
+       iv.resize(IV_SIZE);
+
        return iv;
 }
 
@@ -73,6 +83,16 @@ RawBuffer _decrypt(const RawBuffer &key, const RawBuffer &iv, const RawBuffer &c
        auto algo = ::EVP_aes_128_cbc();
        int tmp_len = (ciphertext.size() / algo->block_size + 1) * algo->block_size;
 
+       if (key.size() != KEY_SIZE) {
+               LogError("Invalid key size: " << key.size() << ", expected: " << KEY_SIZE);
+               return RawBuffer();
+       }
+
+       if (iv.size() != IV_SIZE) {
+               LogError("Invalid iv size: " << iv.size() << ", expected: " << IV_SIZE);
+               return RawBuffer();
+       }
+
        RawBuffer plaintext(tmp_len, 0);
 
        std::unique_ptr<EVP_CIPHER_CTX, void(*)(EVP_CIPHER_CTX *)> ctxptr(