test/crypto.c

Sun, 03 Jun 2018 14:40:07 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 03 Jun 2018 14:40:07 +0200
changeset 408
f71eab18ed38
parent 308
82275f589d8d
child 470
6bf798ad3aec
child 481
ff477f1f7765
permissions
-rw-r--r--

more dav documentation

documents -x option for set/get-property
adds documentation for new commands

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2017 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include "crypto.h"

#include <ucx/string.h>
#include <ucx/utils.h>
#include <ucx/buffer.h>
#include <libidav/utils.h>
#include <libidav/crypto.h>

static DavKey keys256[16];
static DavKey keys128[16];

static char *strings[] = {
    "a",
    "ab",
    "abc",
    "abcd",
    "a string",
    "hello world",
    "0               1",
    "0123 abcd XXXX <>",
    "libidav+dav+dav-sync",
    "R2xd1dI9o0HHd3aDuQrvuPyB",
    "7XxUqWvOvQOFo0SlKyEWzerR",
    "Ert2g8bjjyKpAebBNxdaJ5o7",
    "C0jReDRLHxF6MW1stR00OFiF",
    "eRD76vIQIUNRRFrnUrGtTI13",
    "VmfdNDkSzLKiEgCkWQ4Es4XaiY7TlW3LCm1gt3r66uJ0TeQm",
    "AdeoBAIo8Q54yO1Pwe1WH3rgDe4m5ZBUSgNUlrNqGjtN4UNN",
    "Ajkt3kmdG9uPRi41Q5299yuW9DJNKc0yGlF08K6PfodpYyOxzOiL7TbeDML0RbJAOaL2fpCC",
    "mmGmeUTxxAEyhx1Qw9CAodQ550rQpQhSR4ZuC4im9SDSl5ykGJOEJrdrTPciaIdyY9M6WSmg",
    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "--aabb2234000000001ffffffffffffffffas"
    "very long string ..............................................."
        "----------------------------------------------------------------"
        "0000000000000000000000000000000000000000000000000000000000000000"
        ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"
};

static char *cryptkey0aes256strings[] = {
    "UtIyzMm3e39YFEccglOxyJ4WPlqA01tohXCJSeaVKIA=",
    "rfHc8KMEp9HoGAHz08gyne+YH053EIoiheacHe0yiOY=",
    "C6oaFjGiPe0aN3SAC146ptKyyGbWO1X2hHDOHtV5PBU=",
    "y+bwmzxtjepi1Gpm1dwI7qJywu32hErGLCxFv4K5Zz4=",
    "I1uRSZ/fOvJ4Ng2VhBYMGAhfejhdJdu5Sjc67qdyHIY=",
    "J0s/JwsBebiBEhUJ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z9523cM6fYJDW4E/Q99cxy1ZpRO3hoFtV2iCQ/zWqvhfL96GjbV2yiqMY2iX2bJ+4iXKerrQMmxDUAYJYzQ==",
    "GrRF4S9W75f6Y82Bw1PS3ZfMs0sSY8yc5F7zwJ6YtAUCWvCpgLJ2fd1UGAdYrMoB9f8R2gXXM4ROGD1N/tWo5vDgQDFrzSttN6J+4n9yg18JuQ5Kna2P9sEOkPTSgZDmB9wjOAQ5U4LlmHI35ykcc0LjFkwy9qxXw1GbsATeD4WY+wq800ru6E158a9Fb6teRQUteNZCtgwTqPSr2sYJIQAaJO4Yv0vNp/7BgxI9CZM/Hvr+05GVVRA9YE3qwed3Hk1RMd15cfBo/+bsttTDg1sKtviPHtkBV88codPfoWYZjd8UPYE+q9P0zOM/wRvb5b2sUccRKbmRNmM+OKQySyG/lyCb+HedxjPQ+24i8NLjku3GWJG3egDnRQHY/nb1TBmOFjpNtARq4QhLf0HwDaGcZwchhGn0xpggcrqTuv4="
};

static char *cryptkey1aes256strings[] = {
    "zAkuGJUT4tkdbYg/oxLROlzHNWQPojgyFSGhEw3FpNE=",
    "RiDdDBabx2dxPWH/Hu+FRHN9e8dnoaX2sjNae81VoTU=",
    "ocdZlAi8uGD7JvmVNGA94BhZ7RoJrYWgjwENZ7+CmVQ=",
    "HMtoE2bd6VdJGtcRICoCI1o51jeoWoJyGVPhLLNzFc0=",
    "/lTX94wq8KMKK6IAO+/TxZbE9hCIXd+aribRzIzEcSs=",
    "GKZjuYqJXFyNd26SHrd+kgeHTq7SlDB11igXyyulJ9c=",
    "0il4W1FjrMzDeyaBkswYG+UYgKUBGAhNivzDAPngH3ENN8pSr8cE2m9Q1AKIyKcQ",
    "Can1I90vUnRynV74Y10EFEX7TGtaYKeUgkSLLG2UBOX1gspDeDK5Mea3ajPYT8GU",
    "6/jEE+HPx5CIr/NqK8mZ9jLeOJXqol6F8Dwzg+uGsDChoXwVwcuKdWBpCHwPDSpE",
    "BE8YyUqrqKnjIYP3NYvyPToaSj5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/FPSJsheEZu4ck2wv5qvlYaQx716xkTsXugnpecOSOq5uQkqmSGI0O+aVJ8sgel/JsiGEJew3JDh+jnuK/lKHnsKxhHcX/ZX+OluGK0KbCHYM5RWl5Fb2zJA1SDaDneFqgOqLdSryBOKJeLoNdiFsL7LQY+RK/ElUZn07QoQDLk+xJwFMbF/KTH9KjWA3vEVdh9C0Wc02Jf3AwpaRmrq9/q62dhm+bc+I4NRH1BmROll8PS5Y="
};

static char *cryptkey0aes128strings[] = {
    "yWwheGMLOFi7bUia+105EHpuS2Tv4shfRZh3T0CaXts=",
    "wdpntIG/wpTGUOzgt7Pi6GSnc8dVOtvHbjyF6h1p1LY=",
    "IJnMdm2WinhlDsQItx/DSW3CFygtEtxAw7cEacoP9j8=",
    "vd3IRIru28Xb35+3jKA7PfftbBMvF2z/Cb4e7/2e1vE=",
    "ub3TJHUtIsc2Sb1ZeNDfcDYEq9qDcAb6j+qm7Xyg1VA=",
    "tXgBCUyxvh7AXiw5ysbMM75v36aeoDniDLUjH723xsA=",
    "8g/Vj58LmuGz64biZiA+Oz8nG/e7s0EyF3kcWd5Lmgy86K+AG8c4FlQiAHk7Bscb",
    "BuEFNArQgT2uSrK6IsXeFiGlSz9Y3Sh1O7Xgqy6TzGt3XKx6Ixubv7sU8Xo0nKDz",
    "jjr0Qliga7PjqJ4rdTqCbd6p3jt3tIQ0xCftctoxaPFZQuiumWT47CxV+alclgV9",
    "G0rsiGmM4HZlpnkrPdjEI2+K1d/unmn2QAkl8I68j2+rswPY9VY1zPsRV3x0SfVZ",
    "WaoJI8oySoB3sgBSzdflISJRBz0zHarnkrV9wIn0wbKIeN2xxlzxQmUp/RtNnQZq",
    "YQNtCIRW0oeTbHPBRMCnfHlm6Xr8uvRgODhv3aVm5OPLmxtlEYAFPja1XvJEcsD6",
    "pAdDt3gDc+IsFkzya1cBEV+hi0kYGGDn8HYAq6gs8/sL1i6BjLX8EV1y0oxMIhZk",
    "RpOaTK9gAoboRy0oc2y/EY0ZinuG2Cnntc0KvWe7p7QpHqFdOgvhPy5PxF/OImIb",
    "bK71oogoMekXTWBycpiBZQ9tz13LDKePfYmsMTZWr1g4Z/9/baSBVHzJvZ2td253310QNVN7elQh5YBLauEaf6nqnRCfhFJjCbAaTMLNN8w=",
    "MyypcyOCy39DQJbUsy0fggduPkJVslSRizUqXQZUpuMQf6bDICuXjNBARV6HTBUqhWqatC+35wDBL771udPDPacy3UutZ/nJWQh1nEX0ffE=",
    "ps6CbvSuEhuQdzQCr77rgfpHKjWlyDLTPSlDN1ziLlfK4NOgzLRbhQNNfLzoSkGj4eMTqPDoOzeKrbpbpbisxdjUeylPYznuLcFyBVyZn9CRuOOJdkwkkhnooWDpMzSE",
    "oQ+IOj9HRGhv0FSpqsVfsHU50TYncFELLRFILA/JQUP9esKYNVjJG1c6uUaRtwCU9dtenBkKP/e/zh+3RSKY26GO/vxG3RWDnYUD1HvJDSR+W7xNUaakFMpGRPCBv6cK",
    "aiL1TCWid4j6F/UIPrpz4cq54SHfcp7KLTSaDrcGNIXnLHctbWU1aWHdBjmyZyACIAmQ5l8ul5KafKqaFqPbsw==",
    "cOLQc1zi9/l/02FVMBp9D98jnZhwrLLs/1Hn+XT1XxWO2lD56dYiGjMMwjJ0zuHvOGA4wmUwyL0z8kKK3XGhfQqu046i1GXCK/qdHB2b1hnBEEyv0PmKTr28RINP0xckxMk45SYq+HSfeiAyqdupiIHu3Wzx5aBbOjPd9aoGEx7mXRywgzJ56h6jwcNGv7UP3tKaTjkuUDvwhB2Lo875nd9cytC5kj4HLeSXBhtUcxaMxDEWTFK67pqMT6q51t2BV83eAp/BXkis8wpamhYqdv0yiSt4yamLm8IecGfPsHgfCcrnTtK220xbVBRvHMEbP3gui1nApB2vQiweE6Gk5IXIgwN+P/YrihjUzKtJgqCoQ0FigNTQemUGkNalg9+LxiXSg4AFundZQMaRNMNJA/MRKKvHcCps6POwENtYfBY="
};

static char *cryptkey1aes128strings[] = {
    "jJyZsBeggvHnPFCt4ABzJPPI88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zV56inCkHMWAt1prL6mzv9UadUYEkpPQK/w3Sq9N/3oeNCb21TqF1I9tAbWJOhRqTj098EnmnmJVI6p2mfRESEBEJULBYKsdQPE85CdL2b0Sh",
    "wscy5Q5mi1l8tdA/8v6MrdFQY1PuS10uQNFe/FXnnJS/MRcbcA/R9wlLuWiMt7cUg9Zs74lXc8w7tKkOrSX6zMOHu3hpDjCJA+ZE9gkbP70KIGRnddW5aFhXNy/Sz808",
    "92S3C7m5lQnj5Gpjz9zbPyYdPO5j03b/AZ5sSPHbuM/HIlIfcj289sNnFz08MLgDSxLqZsJnlVjLLahWWo1KSA==",
    "0WQozX4DdbVUATNnxn8kzkG30QGLB0kCUMmm1wdbPCqCWzOqo/id98xh++8PHfD/gmOHwIeZgcW6PTsIT7KI17LRq+ryHI8xdRYMW/oPzXQcaKCis0MbBYpaS9OZp6Wbrkbk9NUShqV340DdydBCtl3RXGhEJLyLWqF2XTyuXNL4srNPFP934rHLSSkU7eUD7FzNiE7mTgQ73E5o/h8porPRtNRnbH1hT5SZK71qC8pRlJjMSVBdMgNHfOJDvjFd0Xv+QwDqm3QqcrpirYhkP5M1ofiwBsuFNra1bnojMPiyXgduhUpxsJ0StvBlzEixrtA2NsxfEgDtrJik8ScOQ7GkB8alHPn3jEFa8KsucyQwxuoJ7W1pIzBYKQqZ3QU3I75aSZ5mAM8PxVVxLdC75iZuBe1PvUNle/bljmKJ3QA="
};

static char *d0 = "01234567890123456789012345678901";
static char *d1 = "gfXa3IJa;.d-afHS1237.:_A12=$%&%!";
static char *d2 = "%$dhXfdhgf(z@pWqoiv)Zal=di}weQ<c";
static char *d3 = "%$dhXfdhgf(z@pWqoiv)Zal=di}weQ<c";
static char *d4 = "<>/(!'§)._,;*++-<Qq@,.=)($%§1aX:";
static char *d5 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
static char *d6 = ".-.-.-.-.-.-.-.-.-$$$$$$$$$$$$$$";
static char *d7 = "uAh281._;(12<?2@@$2..;---assf34v";
static uint32_t d8[] = {17635, 0, 4274927276, 76546, 6098723, 516, 4193367221, 1224964255};
static uint32_t d9[] = {2234966275, 643123, 427492276, 3334964712, 1337123, 722464014, 4193367111, 3234966518};
static uint32_t d10[] = {3231664275, 1578414324, 427496129, 4034960712, 93371873, 1722499994, 4193300111, 8765678};
static uint32_t d11[] = {764564, 12348792, 999432723, 128943278, 664512343, 84327634, 709712334};
static uint8_t d12[] = {14, 94, 240, 253, 14, 0, 123, 94, 72, 197, 71, 56, 64, 156, 204, 231, 17, 18, 101, 24, 200, 15, 208, 150, 1, 85, 171, 18, 205, 30, 231, 7};
static uint8_t d13[] = {25, 43, 219, 253, 14, 0, 223, 94, 87, 127, 171, 56, 34, 26, 204, 31, 64, 18, 151, 124, 233, 12, 118, 154, 1, 185, 11, 18, 25, 30, 231, 207};
static uint8_t d14[] = {15, 0, 220, 253, 14, 5, 223, 234, 157, 157, 121, 56, 71, 216, 254, 31, 61, 192, 151, 255, 0, 12, 118, 14, 5, 185, 124, 18, 25, 54, 131, 227};
static uint8_t d15[] = {0, 43, 210, 25, 14, 0, 243, 94, 87, 0, 1, 156, 234, 6, 214, 31, 64, 18, 151, 124, 3, 12, 118, 154, 1, 18, 111, 18, 225, 31, 251, 207};

static void create_keys() { 
    void *data[16] = {d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13, d14, d15};  
    
    for(int i=0;i<16;i++) {
        keys256[i].length = 32;
        keys256[i].type = DAV_KEY_AES256;
        keys256[i].data = data[i];
        
        keys128[i].length = 16;
        keys128[i].type = DAV_KEY_AES128;
        keys128[i].data = data[i];
    }
}

UCX_TEST(test_util_decrypt_str_k) {
    create_keys();
    
    DavContext *ctx = dav_context_new();
    DavSession *sn = dav_session_new(ctx, "http://localhost/");
    
    UCX_TEST_BEGIN;
    
    for(int k=0;k<2;k++) {
        for(int i=0;i<20;i++) {
            char *c1;
            char *c2;
            if(k == 0) {
                c1 = cryptkey0aes128strings[i];
                c2 = cryptkey0aes256strings[i];
            } else {
                c1 = cryptkey1aes128strings[i];
                c2 = cryptkey1aes256strings[i];
            }
            
            char *s128 = util_decrypt_str_k(sn, c1, &keys128[k]);
            char *s256 = util_decrypt_str_k(sn, c2, &keys256[k]);
            
            UCX_TEST_ASSERT(!strcmp(s128, strings[i]), "s128 wrong");
            UCX_TEST_ASSERT(!strcmp(s256, strings[i]), "s256 wrong");
        }
    }
    
    UCX_TEST_END;
    
    dav_context_destroy(ctx);
}

UCX_TEST(test_util_encrypt_str_k) {
    DavContext *ctx = dav_context_new();
    DavSession *sn = dav_session_new(ctx, "http://localhost/");
    
    UCX_TEST_BEGIN;
    
    for(int k=0;k<16;k++) {
        for(int i=0;i<20;i++) {
            char *aes128 = util_encrypt_str_k(sn, strings[i], &keys128[k]);
            char *aes256 = util_encrypt_str_k(sn, strings[i], &keys256[k]);
            
            char *d1 = util_decrypt_str_k(sn, aes128, &keys128[k]);
            char *d2 = util_decrypt_str_k(sn, aes256, &keys256[k]);
            
            UCX_TEST_ASSERT(!strcmp(d1, strings[i]), "aes128 encrypt failed");
            UCX_TEST_ASSERT(!strcmp(d2, strings[i]), "aes256 encrypt failed");
        }
    }
    
    UCX_TEST_END;
}

UCX_TEST(test_crypto_stream) {
    UcxBuffer *data = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
    UcxBuffer *cbuf = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
    UcxBuffer *pbuf = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
    
    UCX_TEST_BEGIN;
    
    for(int i=0;i<32;i++) {
        DavKey *key = i < 16 ? &keys256[i] : &keys128[i%16];
        for(int j=0;j<20;j++) {
            data->pos = 0;
            data->size = 0;
            size_t slen = strlen(strings[j]);
            ucx_buffer_write(strings[j], 1, slen, data);
            ucx_buffer_seek(data, 0, SEEK_SET);

            cbuf->pos = 0;
            cbuf->size = 0;
            pbuf->pos = 0;
            pbuf->size = 0;

            AESEncrypter *enc = aes_encrypter_new(key, data, (dav_read_func)ucx_buffer_read);
            char buf[1024];
            size_t r = 0;
            while((r = aes_read(buf, 1, 1024, enc)) != 0) {
                ucx_buffer_write(buf, 1, r, cbuf);
            }
            aes_encrypter_close(enc);

            AESDecrypter *dec = aes_decrypter_new(key, pbuf, (dav_write_func)ucx_buffer_write);
            aes_write(cbuf->space, 1, cbuf->pos, dec);
            aes_decrypter_shutdown(dec);
            aes_decrypter_close(dec);

            UCX_TEST_ASSERT(slen == pbuf->pos, "wrong length after enc-dec");
            UCX_TEST_ASSERT(!memcmp(strings[j], pbuf->space, slen), "wrong content after enc-dec");
        }
    }
    
    UCX_TEST_END;
}

mercurial