Debian Codegraph @

src/libs/alibxx/crypto/hmacsha.cpp

raw
1 /*
2 * This File is part of Davix, The IO library for HTTP based protocols
3 * Copyright (C) 2013 Adrien Devresse <[email protected]>, CERN
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21
22 #include <iostream>
23 #include <davix_internal_config.hpp>
24 #include "hmacsha.hpp"
25
26
27 #ifdef HAVE_OPENSSL
28 #include <openssl/hmac.h>
29 #include <openssl/sha.h>
30 #include <openssl/rsa.h>
31 #include <openssl/pem.h>
32 #include <openssl/evp.h>
33
34 // 0 for sha1, 1 for sha256
35 #define HMAC_SHA1 0
36 #define HMAC_SHA256 1
37
38 static std::string hmac_openssl(int type, const std::string & key, const std::string & data) {
39 unsigned char buffer_res[65];
40 unsigned int buffer_len = (type == HMAC_SHA256) ? 64 : 40;
41 const EVP_MD *hashtype = (type == HMAC_SHA256) ? EVP_sha256() : EVP_sha1();
42 HMAC(hashtype, (const unsigned char*)key.c_str(), key.size(),
43 (const unsigned char*) data.c_str(), data.size(), buffer_res, &buffer_len);
44 return std::string((char*) buffer_res, buffer_len);
45 }
46
47 #endif
48
49 std::string hmac_sha1(const std::string & key, const std::string & data){
50 #ifdef HAVE_OPENSSL
51 return hmac_openssl(HMAC_SHA1, key, data);
52 #else
53 #error "No support for hmac calculation"
54 #endif
55 }
56
57 std::string hmac_sha256(const std::string & key, const std::string & data){
58 #ifdef HAVE_OPENSSL
59 return hmac_openssl(HMAC_SHA256, key, data);
60 #else
61 #error "No support for hmac calculation"
62 #endif
63 }
64
65 std::string sha256(const std::string input) {
66 #ifdef HAVE_OPENSSL
67 unsigned char hash[SHA256_DIGEST_LENGTH];
68 SHA256_CTX sha256;
69 SHA256_Init(&sha256);
70 SHA256_Update(&sha256, input.c_str(), input.size());
71 SHA256_Final(hash, &sha256);
72 return std::string((char*) hash, SHA256_DIGEST_LENGTH);
73 #else
74 #error "No support for sha256 calculation"
75 #endif
76 }
77
78 std::string rsasha256(const std::string &key, const std::string &data) {
79 #ifdef HAVE_OPENSSL
80
81 BIO* bio = BIO_new_mem_buf( (void*) key.data(), key.size());
82 if(!bio) return "";
83
84 EVP_PKEY* private_key = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
85 if(!private_key) {
86 BIO_free(bio);
87 return "";
88 }
89
90 RSA *rsa = EVP_PKEY_get1_RSA(private_key);
91 if(!rsa) {
92 EVP_PKEY_free(private_key);
93 BIO_free(bio);
94 return "";
95 }
96
97 std::string digest = sha256(data);
98
99 unsigned int siglen;
100 char retbuff[2048];
101 if(RSA_sign(NID_sha256, (const unsigned char*) digest.c_str(), digest.size(), (unsigned char*) retbuff, &siglen, rsa) != 1) {
102 siglen = 0;
103 }
104
105 RSA_free(rsa);
106 EVP_PKEY_free(private_key);
107 BIO_free(bio);
108
109 return std::string(retbuff, siglen);
110 #else
111 #error "No support for sha256 calculation"
112 #endif
113 }