LCOV - code coverage report
Current view: top level - home/mbr/git/openssl.git/test - p5_crpt2_test.c (source / functions) Hit Total Coverage
Test: lcov_coverage_final.info Lines: 41 57 71.9 %
Date: 2014-08-02 Functions: 3 4 75.0 %
Branches: 9 16 56.2 %

           Branch data     Line data    Source code
       1                 :            : /* Written by Christian Heimes, 2013 */
       2                 :            : /*
       3                 :            :  * Copyright (c) 2013 The OpenSSL Project.  All rights reserved.
       4                 :            :  *
       5                 :            :  * Redistribution and use in source and binary forms, with or without
       6                 :            :  * modification, are permitted provided that the following conditions
       7                 :            :  * are met:
       8                 :            :  *
       9                 :            :  * 1. Redistributions of source code must retain the above copyright
      10                 :            :  *    notice, this list of conditions and the following disclaimer. 
      11                 :            :  *
      12                 :            :  * 2. Redistributions in binary form must reproduce the above copyright
      13                 :            :  *    notice, this list of conditions and the following disclaimer in
      14                 :            :  *    the documentation and/or other materials provided with the
      15                 :            :  *    distribution.
      16                 :            :  *
      17                 :            :  * 3. All advertising materials mentioning features or use of this
      18                 :            :  *    software must display the following acknowledgment:
      19                 :            :  *    "This product includes software developed by the OpenSSL Project
      20                 :            :  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
      21                 :            :  *
      22                 :            :  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
      23                 :            :  *    endorse or promote products derived from this software without
      24                 :            :  *    prior written permission. For written permission, please contact
      25                 :            :  *    openssl-core@openssl.org.
      26                 :            :  *
      27                 :            :  * 5. Products derived from this software may not be called "OpenSSL"
      28                 :            :  *    nor may "OpenSSL" appear in their names without prior written
      29                 :            :  *    permission of the OpenSSL Project.
      30                 :            :  *
      31                 :            :  * 6. Redistributions of any form whatsoever must retain the following
      32                 :            :  *    acknowledgment:
      33                 :            :  *    "This product includes software developed by the OpenSSL Project
      34                 :            :  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
      35                 :            :  *
      36                 :            :  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
      37                 :            :  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      38                 :            :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
      39                 :            :  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
      40                 :            :  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      41                 :            :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
      42                 :            :  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
      43                 :            :  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      44                 :            :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
      45                 :            :  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
      46                 :            :  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
      47                 :            :  * OF THE POSSIBILITY OF SUCH DAMAGE.
      48                 :            :  */
      49                 :            : 
      50                 :            : 
      51                 :            : #include <stdio.h>
      52                 :            : #include <string.h>
      53                 :            : 
      54                 :            : #include "../e_os.h"
      55                 :            : 
      56                 :            : #include <openssl/opensslconf.h>
      57                 :            : #include <openssl/evp.h>
      58                 :            : #ifndef OPENSSL_NO_ENGINE
      59                 :            : #include <openssl/engine.h>
      60                 :            : #endif
      61                 :            : #include <openssl/err.h>
      62                 :            : #include <openssl/conf.h>
      63                 :            : 
      64                 :            : #ifdef OPENSSL_NO_SHA
      65                 :            : int main(int argc, char *argv[])
      66                 :            : {
      67                 :            :     printf("No SHA support\n");
      68                 :            :     return(0);
      69                 :            : }
      70                 :            : #else
      71                 :            : 
      72                 :            : typedef struct {
      73                 :            :         const char *pass;
      74                 :            :         int passlen;
      75                 :            :         const char *salt;
      76                 :            :         int saltlen;
      77                 :            :         int iter;
      78                 :            : } testdata;
      79                 :            : 
      80                 :            : static testdata test_cases[] = {
      81                 :            :         {"password", 8, "salt", 4, 1},
      82                 :            :         {"password", 8, "salt", 4, 2},
      83                 :            :         {"password", 8, "salt", 4, 4096},
      84                 :            :         {"passwordPASSWORDpassword", 24,
      85                 :            :          "saltSALTsaltSALTsaltSALTsaltSALTsalt", 36, 4096},
      86                 :            :         {"pass\0word", 9, "sa\0lt", 5, 4096},
      87                 :            :         {NULL},
      88                 :            : };
      89                 :            : 
      90                 :            : static const char *sha1_results[] = {
      91                 :            :         "0c60c80f961f0e71f3a9b524af6012062fe037a6",
      92                 :            :         "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957",
      93                 :            :         "4b007901b765489abead49d926f721d065a429c1",
      94                 :            :         "3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038",
      95                 :            :         "56fa6aa75548099dcc37d7f03425e0c3",
      96                 :            : };
      97                 :            : 
      98                 :            : static const char *sha256_results[] = {
      99                 :            :         "120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b",
     100                 :            :         "ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43",
     101                 :            :         "c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a",
     102                 :            :         "348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c63551"
     103                 :            :                 "8c7dac47e9",
     104                 :            :         "89b69d0516f829893c696226650a8687",
     105                 :            : };
     106                 :            : 
     107                 :            : static const char *sha512_results[] = {
     108                 :            :         "867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252c02d47"
     109                 :            :                 "0a285a0501bad999bfe943c08f050235d7d68b1da55e63f73b60a57fce",
     110                 :            :         "e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f0040713f18aefdb866d53cf76cab"
     111                 :            :                 "2868a39b9f7840edce4fef5a82be67335c77a6068e04112754f27ccf4e",
     112                 :            :         "d197b1b33db0143e018b12f3d1d1479e6cdebdcc97c5c0f87f6902e072f457b5143f30"
     113                 :            :                 "602641b3d55cd335988cb36b84376060ecd532e039b742a239434af2d5",
     114                 :            :         "8c0511f4c6e597c6ac6315d8f0362e225f3c501495ba23b868c005174dc4ee71115b59"
     115                 :            :                 "f9e60cd9532fa33e0f75aefe30225c583a186cd82bd4daea9724a3d3b8",
     116                 :            :         "9d9e9c4cd21fe4be24d5b8244c759665",
     117                 :            : };
     118                 :            : 
     119                 :            : static void
     120                 :          0 : hexdump(FILE *f, const char *title, const unsigned char *s, int l) {
     121                 :            :         int i;
     122                 :            :         fprintf(f, "%s", title);
     123         [ #  # ]:          0 :         for(i=0; i < l ; i++) {
     124                 :          0 :                 fprintf(f, "%02x", s[i]);
     125                 :            :         }
     126                 :            :         fprintf(f, "\n");
     127                 :          0 : }
     128                 :            : 
     129                 :            : static void
     130                 :         15 : convert(unsigned char *dst, const unsigned char *src, int len) {
     131                 :            :         int i;
     132         [ +  + ]:        540 :         for(i=0; i < len; i++, dst++, src+=2) {
     133                 :            :                 unsigned int n;
     134                 :        525 :                 sscanf((char *)src, "%2x", &n);
     135                 :        525 :                 *dst = (unsigned char)n;
     136                 :            :         }
     137                 :         15 :         *dst = 0;
     138                 :         15 : }
     139                 :            : 
     140                 :            : static void
     141                 :         15 : test_p5_pbkdf2(int i, char *digestname, testdata *test, const char *hex)
     142                 :            : {
     143                 :            :         const EVP_MD *digest;
     144                 :            :         unsigned char *out;
     145                 :            :         unsigned char *expected;
     146                 :            :         int keylen, r;
     147                 :            : 
     148                 :         15 :         digest = EVP_get_digestbyname(digestname);
     149         [ -  + ]:         15 :         if (digest == NULL) {
     150                 :          0 :                 fprintf(stderr, "unknown digest %s\n", digestname);
     151                 :          0 :                 EXIT(5);
     152                 :            :         }
     153                 :            : 
     154         [ -  + ]:         15 :         if ((strlen(hex) % 2) != 0) {
     155                 :          0 :                 fprintf(stderr, "odd hex digest %s %i\n", digestname, i);
     156                 :          0 :                 EXIT(5);
     157                 :            :         }
     158                 :         15 :         keylen = strlen(hex) / 2;
     159                 :         15 :         expected = OPENSSL_malloc(keylen + 1);
     160                 :         15 :         out = OPENSSL_malloc(keylen + 1);
     161         [ -  + ]:         15 :         if ((expected == NULL) || (out == NULL)) {
     162                 :          0 :                 fprintf(stderr, "malloc() failed\n");
     163                 :          0 :                 EXIT(5);
     164                 :            :         }
     165                 :         15 :         convert(expected, (const unsigned char *)hex, keylen);
     166                 :            : 
     167                 :         15 :         r = PKCS5_PBKDF2_HMAC(test->pass, test->passlen,
     168                 :         15 :                                                   (const unsigned char *)test->salt, test->saltlen,
     169                 :            :                                                   test->iter, digest, keylen, out);
     170                 :            : 
     171         [ -  + ]:         15 :         if (r == 0) {
     172                 :          0 :                 fprintf(stderr, "PKCS5_PBKDF2_HMAC(%s) failure test %i\n",
     173                 :            :                                 digestname, i);
     174                 :          0 :                 EXIT(3);
     175                 :            :         }
     176         [ -  + ]:         15 :         if (memcmp(expected, out, keylen) != 0) {
     177                 :          0 :                 fprintf(stderr, "Wrong result for PKCS5_PBKDF2_HMAC(%s) test %i\n",
     178                 :            :                                 digestname, i);
     179                 :          0 :                 hexdump(stderr, "expected: ", expected, keylen);
     180                 :          0 :                 hexdump(stderr, "result:   ", out, keylen);
     181                 :          0 :                 EXIT(2);
     182                 :            :         }
     183                 :         15 :         OPENSSL_free(expected);
     184                 :         15 :         OPENSSL_free(out);
     185                 :         15 : }
     186                 :            : 
     187                 :          1 : int main(int argc,char **argv) {
     188                 :            :         int i;
     189                 :          1 :         testdata *test = test_cases;
     190                 :            : 
     191                 :          1 :         CRYPTO_malloc_debug_init();
     192                 :          1 :         CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
     193                 :          1 :         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
     194                 :            : 
     195                 :          1 :         OpenSSL_add_all_digests();
     196                 :            : #ifndef OPENSSL_NO_ENGINE
     197                 :          1 :         ENGINE_load_builtin_engines();
     198                 :          1 :         ENGINE_register_all_digests();
     199                 :            : #endif
     200                 :            : 
     201                 :            :         printf("PKCS5_PBKDF2_HMAC() tests ");
     202         [ +  + ]:          6 :         for (i=0; test->pass != NULL; i++, test++) {
     203                 :            : #ifndef OPENSSL_NO_SHA0
     204                 :          5 :                 test_p5_pbkdf2(i, "sha1", test, sha1_results[i]);
     205                 :            : #endif
     206                 :            : #ifndef OPENSSL_NO_SHA256
     207                 :          5 :                 test_p5_pbkdf2(i, "sha256", test, sha256_results[i]);
     208                 :            : #endif
     209                 :            : #ifndef OPENSSL_NO_SHA512
     210                 :          5 :                 test_p5_pbkdf2(i, "sha512", test, sha512_results[i]);
     211                 :            : #endif
     212                 :            :                 printf(".");
     213                 :            :         }
     214                 :            :         printf(" done\n");
     215                 :            : 
     216                 :            : #ifndef OPENSSL_NO_ENGINE
     217                 :          1 :         ENGINE_cleanup();
     218                 :            : #endif
     219                 :          1 :         EVP_cleanup();
     220                 :          1 :         CRYPTO_cleanup_all_ex_data();
     221                 :          1 :         ERR_remove_thread_state(NULL);
     222                 :          1 :         ERR_free_strings();
     223                 :          1 :         CRYPTO_mem_leaks_fp(stderr);
     224                 :          1 :         return 0;
     225                 :            : }
     226                 :            : #endif /* OPENSSL_NO_SHA */

Generated by: LCOV version 1.9