test/crypto.c

Sun, 15 Sep 2019 16:11:58 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 15 Sep 2019 16:11:58 +0200
changeset 641
b138d1241e68
parent 515
2465dd550bb5
child 688
d405d2ac78e6
permissions
-rw-r--r--

finish hashing tests

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2018 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/JwsBebiBEhUJUjm9aliuX6NsMxfkHyD8t1yZqeA=",
    "riCs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CQ/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",
    "BE8YyUqrqKnjIYP3NYvyPToaSj5Sy8OW3DAFdrPpbCKG1wmyYo7E6pSO9/7DP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OSOq5uQkqmSGI0O+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[] = {
    "jJyZsBeggvHnPFCt4ABzJPPI88RHWXLREw760Pwh5VU=",
    "R+NDojkR6jmLQBK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q9N/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_buffer) {
    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++) {
            UcxBuffer *content = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
            ucx_buffer_puts(content, strings[j]);
            content->pos = 0;
            
            UcxBuffer *enc = aes_encrypt_buffer(content, key);
            UCX_TEST_ASSERT(enc->size >= content->size + 16, "aes_encrypt_buffer failed");
            
            UcxBuffer *dec = aes_decrypt_buffer(enc, key);
            UCX_TEST_ASSERT(dec->size == content->size, "aes_decrypt_buffer failed");
            
            UCX_TEST_ASSERT(!memcmp(content->space, dec->space, dec->size), "decrypted buffer has wrong content");
            
            ucx_buffer_free(content);
            ucx_buffer_free(enc);
            ucx_buffer_free(dec);
        }
    }
    
    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, NULL);
            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");
            
            data->pos = 0;
            UcxBuffer *enc2 = aes_encrypt_buffer(data, key);
            UcxBuffer *dec2 = aes_decrypt_buffer(enc2, key);
            
            UCX_TEST_ASSERT(dec2->size == data->size, "dec2 has wrong size");
            UCX_TEST_ASSERT(!memcmp(strings[j], dec2->space, dec2->size), "dec2 has wrong content");
        }
    }
    
    UCX_TEST_END;
}

static char *pws[] = {
    "1234",
    "abdefgh",
    "AAAAAAAAAAAAAAAAA",
    "5478FFJD:Loire923!$$123sd;fhgfdee432ASDAfd432"
};

static uint8_t salt[][16] = {
    { 1, 10, 100, 222},
    { 123, 12, 92, 14, 11, 0, 255, 190 },
    { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 },
    { 100, 240, 1, 190, 17, 0, 189, 50, 31, 13, 37, 25, 01, 0, 0, 7}
};
static int saltlen[] = { 4, 8, 12, 16 };

static uint8_t pwgenkeys[][32] = {
    
    { 94, 67, 10, 1, 46, 198, 81, 162, 236, 220, 211, 196, 141, 127, 122, 39  },
    { 94, 67, 10, 1, 46, 198, 81, 162, 236, 220, 211, 196, 141, 127, 122, 39, 96, 93, 105, 0, 131, 184, 21, 87, 165, 76, 247, 96, 74, 130, 90, 81  },
    { 23, 185, 87, 96, 109, 149, 18, 87, 24, 246, 70, 196, 142, 67, 187, 156  },
    { 23, 185, 87, 96, 109, 149, 18, 87, 24, 246, 70, 196, 142, 67, 187, 156, 131, 104, 108, 119, 175, 221, 108, 220, 75, 83, 91, 205, 157, 142, 132, 175  },
    { 224, 80, 236, 51, 156, 38, 77, 11, 198, 205, 190, 210, 193, 40, 188, 215  },
    { 224, 80, 236, 51, 156, 38, 77, 11, 198, 205, 190, 210, 193, 40, 188, 215, 247, 201, 231, 19, 243, 61, 20, 166, 155, 147, 234, 3, 148, 240, 255, 190  },
    { 29, 209, 169, 101, 7, 80, 162, 82, 196, 20, 110, 36, 66, 41, 189, 16  },
    { 29, 209, 169, 101, 7, 80, 162, 82, 196, 20, 110, 36, 66, 41, 189, 16, 65, 37, 90, 2, 226, 228, 201, 21, 87, 230, 115, 201, 87, 157, 53, 251  },
    { 107, 129, 5, 22, 175, 18, 160, 61, 29, 249, 96, 108, 136, 1, 238, 168  },
    { 107, 129, 5, 22, 175, 18, 160, 61, 29, 249, 96, 108, 136, 1, 238, 168, 32, 208, 199, 177, 44, 251, 145, 195, 189, 18, 119, 161, 167, 101, 248, 83  },
    { 14, 193, 58, 165, 156, 202, 237, 35, 113, 184, 24, 180, 221, 81, 213, 62  },
    { 14, 193, 58, 165, 156, 202, 237, 35, 113, 184, 24, 180, 221, 81, 213, 62, 211, 25, 211, 30, 47, 253, 168, 169, 84, 31, 162, 185, 75, 136, 22, 244  },
    { 83, 193, 218, 177, 173, 5, 134, 61, 45, 14, 141, 225, 194, 114, 91, 221  },
    { 83, 193, 218, 177, 173, 5, 134, 61, 45, 14, 141, 225, 194, 114, 91, 221, 61, 56, 69, 123, 1, 149, 247, 141, 71, 48, 55, 182, 237, 32, 20, 20  },
    { 223, 221, 209, 7, 249, 39, 34, 180, 121, 44, 144, 153, 47, 188, 104, 33  },
    { 223, 221, 209, 7, 249, 39, 34, 180, 121, 44, 144, 153, 47, 188, 104, 33, 29, 159, 51, 8, 200, 85, 137, 129, 97, 255, 26, 23, 147, 133, 11, 240  },
    { 142, 13, 212, 33, 112, 149, 245, 39, 20, 237, 141, 46, 35, 253, 228, 43  },
    { 142, 13, 212, 33, 112, 149, 245, 39, 20, 237, 141, 46, 35, 253, 228, 43, 219, 69, 181, 28, 235, 85, 202, 135, 6, 215, 130, 67, 235, 114, 91, 79  },
    { 53, 10, 18, 131, 28, 151, 84, 93, 2, 102, 69, 224, 174, 140, 2, 168  },
    { 53, 10, 18, 131, 28, 151, 84, 93, 2, 102, 69, 224, 174, 140, 2, 168, 213, 236, 43, 135, 148, 195, 134, 160, 143, 216, 109, 167, 150, 72, 162, 22  },
    { 26, 199, 244, 1, 219, 53, 16, 109, 121, 230, 129, 204, 69, 120, 246, 83  },
    { 26, 199, 244, 1, 219, 53, 16, 109, 121, 230, 129, 204, 69, 120, 246, 83, 93, 9, 117, 21, 49, 67, 56, 255, 147, 16, 18, 39, 245, 93, 20, 201  },
    { 195, 217, 248, 6, 86, 212, 115, 132, 32, 81, 238, 97, 116, 173, 176, 250  },
    { 195, 217, 248, 6, 86, 212, 115, 132, 32, 81, 238, 97, 116, 173, 176, 250, 207, 152, 10, 150, 113, 90, 246, 88, 41, 9, 195, 244, 70, 190, 165, 251  },
    { 60, 114, 156, 195, 119, 103, 167, 179, 70, 3, 11, 189, 25, 27, 175, 30  },
    { 60, 114, 156, 195, 119, 103, 167, 179, 70, 3, 11, 189, 25, 27, 175, 30, 115, 175, 250, 36, 181, 250, 22, 148, 210, 198, 36, 182, 207, 10, 150, 104  },
    { 204, 63, 177, 165, 41, 0, 40, 99, 117, 9, 245, 59, 176, 66, 3, 234  },
    { 204, 63, 177, 165, 41, 0, 40, 99, 117, 9, 245, 59, 176, 66, 3, 234, 106, 50, 241, 16, 254, 158, 60, 115, 221, 22, 141, 140, 220, 241, 69, 59  },
    { 253, 68, 7, 137, 52, 78, 146, 207, 4, 68, 42, 89, 166, 74, 108, 34  },
    { 253, 68, 7, 137, 52, 78, 146, 207, 4, 68, 42, 89, 166, 74, 108, 34, 183, 51, 183, 187, 192, 254, 20, 197, 159, 173, 10, 2, 137, 81, 148, 147  },
    { 242, 67, 91, 24, 61, 250, 13, 44, 131, 236, 224, 118, 37, 250, 67, 160  },
    { 242, 67, 91, 24, 61, 250, 13, 44, 131, 236, 224, 118, 37, 250, 67, 160, 57, 53, 134, 248, 210, 25, 218, 240, 124, 195, 151, 234, 220, 136, 193, 14  },
    { 226, 55, 98, 79, 192, 117, 212, 68, 109, 46, 58, 233, 95, 180, 2, 154  },
    { 226, 55, 98, 79, 192, 117, 212, 68, 109, 46, 58, 233, 95, 180, 2, 154, 135, 111, 74, 105, 94, 113, 77, 37, 133, 53, 72, 43, 15, 131, 69, 209  },
    { 36, 177, 96, 156, 175, 123, 208, 63, 203, 224, 204, 179, 152, 35, 5, 115  },
    { 36, 177, 96, 156, 175, 123, 208, 63, 203, 224, 204, 179, 152, 35, 5, 115, 142, 221, 213, 107, 223, 91, 99, 91, 126, 121, 22, 199, 195, 202, 123, 181  },
    { 223, 189, 161, 102, 183, 210, 183, 174, 247, 101, 81, 10, 73, 169, 246, 255  },
    { 223, 189, 161, 102, 183, 210, 183, 174, 247, 101, 81, 10, 73, 169, 246, 255, 248, 174, 81, 251, 70, 4, 221, 232, 147, 106, 138, 118, 156, 196, 58, 244  },
    { 175, 105, 140, 110, 225, 120, 1, 72, 76, 149, 8, 203, 116, 79, 187, 131  },
    { 175, 105, 140, 110, 225, 120, 1, 72, 76, 149, 8, 203, 116, 79, 187, 131, 218, 82, 5, 84, 22, 185, 236, 252, 165, 72, 154, 6, 50, 0, 89, 33  },
    { 90, 125, 87, 16, 162, 186, 98, 211, 44, 165, 26, 118, 138, 83, 6, 121  },
    { 90, 125, 87, 16, 162, 186, 98, 211, 44, 165, 26, 118, 138, 83, 6, 121, 66, 33, 63, 5, 235, 119, 12, 150, 13, 118, 198, 35, 176, 99, 252, 232  },
    { 97, 17, 86, 48, 46, 241, 84, 19, 149, 72, 120, 235, 239, 7, 165, 117  },
    { 97, 17, 86, 48, 46, 241, 84, 19, 149, 72, 120, 235, 239, 7, 165, 117, 226, 124, 56, 147, 68, 13, 49, 222, 211, 30, 121, 158, 74, 98, 244, 10  },
    { 20, 216, 59, 111, 87, 145, 3, 117, 255, 156, 25, 154, 190, 118, 78, 20  },
    { 20, 216, 59, 111, 87, 145, 3, 117, 255, 156, 25, 154, 190, 118, 78, 20, 248, 214, 78, 139, 61, 192, 140, 235, 59, 160, 40, 78, 54, 47, 25, 109  },
    { 32, 229, 86, 178, 39, 205, 7, 101, 40, 38, 134, 141, 19, 69, 80, 243  },
    { 32, 229, 86, 178, 39, 205, 7, 101, 40, 38, 134, 141, 19, 69, 80, 243, 28, 53, 160, 113, 203, 108, 57, 9, 231, 117, 86, 17, 206, 148, 110, 224  },
    { 210, 190, 211, 92, 237, 193, 7, 64, 87, 132, 86, 141, 30, 125, 82, 106  },
    { 210, 190, 211, 92, 237, 193, 7, 64, 87, 132, 86, 141, 30, 125, 82, 106, 25, 187, 172, 61, 235, 190, 17, 208, 203, 92, 238, 153, 58, 16, 49, 223  },
    { 244, 127, 138, 189, 35, 167, 166, 55, 4, 191, 212, 31, 233, 227, 153, 170  },
    { 244, 127, 138, 189, 35, 167, 166, 55, 4, 191, 212, 31, 233, 227, 153, 170, 40, 148, 206, 207, 252, 157, 92, 212, 241, 88, 224, 141, 179, 209, 252, 43  },
    { 144, 204, 246, 93, 75, 176, 77, 125, 131, 249, 85, 13, 133, 132, 179, 13  },
    { 144, 204, 246, 93, 75, 176, 77, 125, 131, 249, 85, 13, 133, 132, 179, 13, 49, 133, 237, 34, 95, 96, 223, 131, 7, 116, 177, 234, 58, 175, 154, 61  },
    { 168, 143, 20, 221, 217, 4, 182, 225, 236, 110, 92, 32, 110, 49, 20, 94  },
    { 168, 143, 20, 221, 217, 4, 182, 225, 236, 110, 92, 32, 110, 49, 20, 94, 184, 23, 40, 57, 120, 148, 146, 245, 154, 136, 43, 183, 113, 171, 180, 149  },
    { 187, 226, 167, 37, 42, 163, 119, 0, 115, 104, 56, 58, 207, 206, 35, 0  },
    { 187, 226, 167, 37, 42, 163, 119, 0, 115, 104, 56, 58, 207, 206, 35, 0, 93, 176, 200, 184, 33, 23, 15, 179, 21, 58, 210, 183, 59, 173, 205, 98  },
    { 46, 112, 16, 101, 211, 3, 163, 33, 2, 36, 37, 139, 46, 0, 29, 198  },
    { 46, 112, 16, 101, 211, 3, 163, 33, 2, 36, 37, 139, 46, 0, 29, 198, 128, 252, 22, 247, 105, 129, 84, 50, 252, 218, 215, 235, 200, 254, 34, 109  },
    { 115, 186, 123, 201, 63, 21, 207, 229, 86, 0, 7, 254, 45, 115, 84, 8  },
    { 115, 186, 123, 201, 63, 21, 207, 229, 86, 0, 7, 254, 45, 115, 84, 8, 39, 27, 113, 37, 108, 123, 230, 153, 209, 218, 180, 77, 66, 207, 239, 26  },
    { 252, 208, 199, 95, 179, 33, 229, 155, 159, 226, 234, 213, 21, 83, 30, 206  },
    { 252, 208, 199, 95, 179, 33, 229, 155, 159, 226, 234, 213, 21, 83, 30, 206, 174, 151, 174, 207, 58, 169, 148, 244, 37, 88, 44, 46, 72, 172, 15, 214  }
};


UCX_TEST(test_dav_pw2key) {
    
    
    UCX_TEST_BEGIN;
    
    for(int p=0;p<4;p++) {
        for(int s=0;s<4;s++) {
            DavKey *keys[4];
            keys[0] =  dav_pw2key(
                    pws[p],
                    salt[s],
                    saltlen[s],
                    DAV_PWFUNC_PBKDF2_SHA256,
                    DAV_KEY_AES128);
            keys[1] = dav_pw2key(
                    pws[p],
                    salt[s],
                    saltlen[s],
                    DAV_PWFUNC_PBKDF2_SHA256,
                    DAV_KEY_AES256);
            keys[2] = dav_pw2key(
                    pws[p],
                    salt[s],
                    saltlen[s],
                    DAV_PWFUNC_PBKDF2_SHA512,
                    DAV_KEY_AES128);
            keys[3] = dav_pw2key(
                    pws[p],
                    salt[s],
                    saltlen[s],
                    DAV_PWFUNC_PBKDF2_SHA512,
                    DAV_KEY_AES256);
            
            for(int i=0;i<4;i++) {
                DavKey *key = keys[i];
                
                int index = 16*p + 4*s + i;
                int keylen = index % 2 == 0 ? 16 : 32;
                
                UCX_TEST_ASSERT(keylen == key->length, "wrong key length");
                UCX_TEST_ASSERT(!memcmp(key->data, pwgenkeys[index], keylen), "wrong key data");
            }
        }
    }
    
    UCX_TEST_END;
}

mercurial