private.h: rename to contain dir
[platform/upstream/libwebsockets.git] / lib / tls / openssl / lws-gencrypto.c
1 /*
2  * libwebsockets - generic crypto api hiding the backend
3  *
4  * Copyright (C) 2017 - 2018 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  *
21  *  lws-gencrypto openssl-specific common code
22  */
23
24 #include "private-lib-core.h"
25 #include "private-lib-tls-openssl.h"
26
27 /*
28  * Care: many openssl apis return 1 for success.  These are translated to the
29  * lws convention of 0 for success.
30  */
31
32 int
33 lws_gencrypto_openssl_hash_to_NID(enum lws_genhash_types hash_type)
34 {
35         int h = -1;
36
37         switch (hash_type) {
38         case LWS_GENHASH_TYPE_UNKNOWN:
39                 break;
40         case LWS_GENHASH_TYPE_MD5:
41                 h = NID_md5;
42                 break;
43         case LWS_GENHASH_TYPE_SHA1:
44                 h = NID_sha1;
45                 break;
46         case LWS_GENHASH_TYPE_SHA256:
47                 h = NID_sha256;
48                 break;
49         case LWS_GENHASH_TYPE_SHA384:
50                 h = NID_sha384;
51                 break;
52         case LWS_GENHASH_TYPE_SHA512:
53                 h = NID_sha512;
54                 break;
55         }
56
57         return h;
58 }
59
60 const EVP_MD *
61 lws_gencrypto_openssl_hash_to_EVP_MD(enum lws_genhash_types hash_type)
62 {
63         const EVP_MD *h = NULL;
64
65         switch (hash_type) {
66         case LWS_GENHASH_TYPE_UNKNOWN:
67                 break;
68         case LWS_GENHASH_TYPE_MD5:
69                 h = EVP_md5();
70                 break;
71         case LWS_GENHASH_TYPE_SHA1:
72                 h = EVP_sha1();
73                 break;
74         case LWS_GENHASH_TYPE_SHA256:
75                 h = EVP_sha256();
76                 break;
77         case LWS_GENHASH_TYPE_SHA384:
78                 h = EVP_sha384();
79                 break;
80         case LWS_GENHASH_TYPE_SHA512:
81                 h = EVP_sha512();
82                 break;
83         }
84
85         return h;
86 }