check return values for EVP_Digest*() APIs
[openssl.git] / test / rmdtest.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13
14 #include "../e_os.h"
15
16 #ifdef OPENSSL_NO_RMD160
17 int main(int argc, char *argv[])
18 {
19     printf("No ripemd support\n");
20     return (0);
21 }
22 #else
23 # include <openssl/ripemd.h>
24 # include <openssl/evp.h>
25
26 # ifdef CHARSET_EBCDIC
27 #  include <openssl/ebcdic.h>
28 # endif
29
30 static char test[][100] = {
31     { "" },
32     { "a" },
33     { "abc" },
34     { "message digest" },
35     { "abcdefghijklmnopqrstuvwxyz" },
36     { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
37     { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
38     { "12345678901234567890123456789012345678901234567890123456789012345678901234567890" }
39 };
40
41 static char *ret[] = {
42     "9c1185a5c5e9fc54612808977ee8f548b2258d31",
43     "0bdc9d2d256b3ee9daae347be6f4dc835a467ffe",
44     "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc",
45     "5d0689ef49d2fae572b881b123a85ffa21595f36",
46     "f71c27109c692c1b56bbdceb5b9d2865b3708dbc",
47     "12a053384a9c0c88e405a06c27dcf49ada62eb2b",
48     "b0e20b6e3116640286ed3a87a5713079b21f5189",
49     "9b752e45573d4b39f4dbd3323cab82bf63326bfb",
50 };
51
52 static char *pt(unsigned char *md);
53 int main(int argc, char *argv[])
54 {
55     unsigned int i;
56     int err = 0;
57     char **R;
58     char *p;
59     unsigned char md[RIPEMD160_DIGEST_LENGTH];
60
61     R = ret;
62     for (i = 0; i < OSSL_NELEM(test); i++) {
63 # ifdef CHARSET_EBCDIC
64         ebcdic2ascii(test[i], test[i], strlen(test[i]));
65 # endif
66         if (!EVP_Digest(test[i], strlen(test[i]), md, NULL, EVP_ripemd160(),
67                         NULL)) {
68             printf("EVP Digest error.\n");
69             EXIT(1);
70         }
71         p = pt(md);
72         if (strcmp(p, (char *)*R) != 0) {
73             printf("error calculating RIPEMD160 on '%s'\n", test[i]);
74             printf("got %s instead of %s\n", p, *R);
75             err++;
76         } else
77             printf("test %d ok\n", i + 1);
78         R++;
79     }
80     EXIT(err);
81 }
82
83 static char *pt(unsigned char *md)
84 {
85     int i;
86     static char buf[80];
87
88     for (i = 0; i < RIPEMD160_DIGEST_LENGTH; i++)
89         sprintf(&(buf[i * 2]), "%02x", md[i]);
90     return (buf);
91 }
92 #endif