|
23 | 23 | #include <stdlib.h> |
24 | 24 | #include <string.h> |
25 | 25 | #include <unistd.h> |
26 | | - |
27 | | -#include <linux/if_alg.h> |
28 | | -#include <linux/socket.h> |
29 | | - |
30 | | -#include <sys/socket.h> |
31 | 26 | #include <sys/stat.h> |
32 | 27 | #include <sys/types.h> |
33 | 28 |
|
|
37 | 32 | #include "nvme-cmds.h" |
38 | 33 | #include "nvme-print.h" |
39 | 34 | #include "nvme.h" |
| 35 | +#include "util/hash.h" |
40 | 36 |
|
41 | 37 | #define CREATE_CMD |
42 | 38 |
|
43 | | - |
44 | | -#ifndef AF_ALG |
45 | | -#define AF_ALG 38 |
46 | | -#endif |
47 | | -#ifndef SOL_ALG |
48 | | -#define SOL_ALG 279 |
49 | | -#endif |
50 | | - |
51 | | -#define HMAC_SHA256_ALGO_NAME "hmac(sha256)" |
52 | | -#define MD5_HASH_ALGO_NAME "md5" |
53 | | -#define HMAC_SHA256_HASH_SIZE 32 |
54 | | -#define MD5_HASH_HASH_SIZE 16 |
55 | | - |
56 | | -/* |
57 | | - * Utility function to create hash value of given data (with given key) using |
58 | | - * given hash algorithm; this function uses kernel crypto services |
59 | | - */ |
60 | | -unsigned char *create_hash(const char *algo, |
61 | | - int hash_size, |
62 | | - unsigned char *data, |
63 | | - int datalen, |
64 | | - unsigned char *key, |
65 | | - int keylen) |
66 | | -{ |
67 | | - int error, infd, outfd = -1; |
68 | | - unsigned char *hash = NULL; |
69 | | - struct sockaddr_alg provider_sa = { |
70 | | - .salg_family = AF_ALG, |
71 | | - .salg_type = "hash", |
72 | | - .salg_name = { 0 } |
73 | | - }; |
74 | | - |
75 | | - /* copy algorithm name */ |
76 | | - if (strlen(algo) > sizeof(provider_sa.salg_name)) { |
77 | | - fprintf(stderr, "%s: algorithm name overflow", __func__); |
78 | | - return hash; |
79 | | - } |
80 | | - memcpy(provider_sa.salg_name, algo, strlen(algo)); |
81 | | - |
82 | | - /* open netlink socket connection to algorigm provider and bind */ |
83 | | - infd = socket(AF_ALG, SOCK_SEQPACKET, 0); |
84 | | - if (infd < 0) { |
85 | | - perror("socket"); |
86 | | - return hash; |
87 | | - } |
88 | | - error = bind(infd, (struct sockaddr *)&provider_sa, sizeof(provider_sa)); |
89 | | - if (error < 0) { |
90 | | - perror("bind"); |
91 | | - goto out_close_infd; |
92 | | - } |
93 | | - |
94 | | - /* if algorithm requires key, set it first - empty keys not accepted !*/ |
95 | | - if (key != NULL && keylen > 0) { |
96 | | - error = setsockopt(infd, SOL_ALG, ALG_SET_KEY, key, keylen); |
97 | | - if (error < 0) { |
98 | | - perror("setsockopt"); |
99 | | - goto out_close_infd; |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - /* now send data to hash */ |
104 | | - outfd = accept(infd, NULL, 0); |
105 | | - if (outfd < 0) { |
106 | | - perror("accept"); |
107 | | - goto out_close_infd; |
108 | | - } |
109 | | - error = send(outfd, data, datalen, 0); |
110 | | - if (error < 0) { |
111 | | - perror("send"); |
112 | | - goto out_close_outfd; |
113 | | - } |
114 | | - |
115 | | - /* read computed hash */ |
116 | | - hash = (unsigned char *)calloc(hash_size, 1); |
117 | | - if (hash == NULL) { |
118 | | - perror("calloc"); |
119 | | - goto out_close_outfd; |
120 | | - } |
121 | | - |
122 | | - error = read(outfd, hash, hash_size); |
123 | | - if (error != hash_size) { |
124 | | - perror("read"); |
125 | | - free(hash); |
126 | | - hash = NULL; |
127 | | - } |
128 | | -out_close_outfd: |
129 | | - close(outfd); |
130 | | -out_close_infd: |
131 | | - close(infd); |
132 | | - |
133 | | - return hash; |
134 | | -} |
135 | | - |
136 | | -/* Function that computes hmac-sha256 hash of given data and key pair. Returns |
137 | | - * byte stream (non-null terminated) upon success, NULL otherwise. |
138 | | - */ |
139 | | -unsigned char *hmac_sha256(unsigned char *data, int datalen, unsigned char *key, |
140 | | - int keylen) |
141 | | -{ |
142 | | - return create_hash(HMAC_SHA256_ALGO_NAME, |
143 | | - HMAC_SHA256_HASH_SIZE, |
144 | | - data, |
145 | | - datalen, |
146 | | - key, |
147 | | - keylen); |
148 | | -} |
149 | | - |
150 | | -/* Function that computes md5 of given buffer - md5 hash is used as nonce |
151 | | - * Returns byte stream (non-null terminated) upon success, NULL otherwise. |
152 | | - */ |
153 | | -unsigned char *rpmb_md5(unsigned char *data, int datalen) |
154 | | -{ |
155 | | - return create_hash(MD5_HASH_ALGO_NAME, |
156 | | - MD5_HASH_HASH_SIZE, |
157 | | - data, |
158 | | - datalen, |
159 | | - NULL, |
160 | | - 0); |
161 | | -} |
162 | | - |
163 | 39 | /* Read data from given file into buffer and return its length */ |
164 | 40 | static int read_file(const char *file, unsigned char **data, unsigned int *len) |
165 | 41 | { |
@@ -300,7 +176,7 @@ static int recv_rpmb_rsp(struct libnvme_transport_handle *hdl, int tgt, int size |
300 | 176 | static void rpmb_nonce_init(struct rpmb_data_frame_t *req) |
301 | 177 | { |
302 | 178 | int num = rand(); |
303 | | - unsigned char *hash = rpmb_md5((unsigned char *)&num, sizeof(num)); |
| 179 | + unsigned char *hash = create_md5((unsigned char *)&num, sizeof(num)); |
304 | 180 | if (hash) memcpy(req->nonce, hash, sizeof(req->nonce)); |
305 | 181 | } |
306 | 182 |
|
@@ -662,8 +538,8 @@ static int auth_data_write_chunk(struct libnvme_transport_handle *hdl, |
662 | 538 | req->write_counter = write_cntr; |
663 | 539 |
|
664 | 540 | /* compute HMAC hash */ |
665 | | - mac = hmac_sha256(((unsigned char *)req + 223), req_size - 223, |
666 | | - keybuf, keysize); |
| 541 | + mac = create_hmac_sha256(((unsigned char *)req + 223), req_size - 223, |
| 542 | + keybuf, keysize); |
667 | 543 | if (mac == NULL) { |
668 | 544 | fprintf(stderr, "failed to compute HMAC-SHA256\n"); |
669 | 545 | error = -1; |
@@ -770,8 +646,8 @@ static int rpmb_write_config_block(struct libnvme_transport_handle *hdl, |
770 | 646 |
|
771 | 647 | free(cfg_buf_read); |
772 | 648 | req->write_counter = write_cntr; |
773 | | - mac = hmac_sha256(((unsigned char *)req + 223), req_size - 223, |
774 | | - keybuf, keysize); |
| 649 | + mac = create_hmac_sha256(((unsigned char *)req + 223), req_size - 223, |
| 650 | + keybuf, keysize); |
775 | 651 | if (mac == NULL) { |
776 | 652 | fprintf(stderr, "failed to compute hmac-sha256 hash\n"); |
777 | 653 | error = -EINVAL; |
|
0 commit comments