hmacsha1.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* An implementation of HMAC using SHA-1.
  2. *
  3. * Copyright (c) 2003 Red Hat, Inc.
  4. * Written by Nalin Dahyabhai <nalin@redhat.com>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, and the entire permission notice in its entirety,
  11. * including the disclaimer of warranties.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote
  16. * products derived from this software without specific prior
  17. * written permission.
  18. *
  19. * ALTERNATIVELY, this product may be distributed under the terms of
  20. * the GNU Public License, in which case the provisions of the GPL are
  21. * required INSTEAD OF the above restrictions. (This clause is
  22. * necessary due to a potential bad interaction between the GPL and
  23. * the restrictions contained in a BSD-style copyright.)
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  27. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  29. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  33. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  35. * OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. /* See RFC 2104 for descriptions. */
  39. #include "config.h"
  40. #include <sys/types.h>
  41. #include <sys/stat.h>
  42. #include <errno.h>
  43. #include <fcntl.h>
  44. #include <grp.h>
  45. #include <pwd.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <unistd.h>
  49. #include <syslog.h>
  50. #include <security/pam_ext.h>
  51. #include "hmacsha1.h"
  52. #include "sha1.h"
  53. #define MINIMUM_KEY_SIZE SHA1_OUTPUT_SIZE
  54. #define MAXIMUM_KEY_SIZE SHA1_BLOCK_SIZE
  55. static void
  56. hmac_key_create(pam_handle_t *pamh, const char *filename, size_t key_size,
  57. uid_t owner, gid_t group)
  58. {
  59. int randfd, keyfd, i;
  60. size_t count;
  61. char *key;
  62. /* Open the destination file. */
  63. keyfd = open(filename,
  64. O_WRONLY | O_CREAT | O_EXCL | O_TRUNC,
  65. S_IRUSR | S_IWUSR);
  66. if (keyfd == -1) {
  67. pam_syslog(pamh, LOG_ERR, "Cannot create %s: %m", filename);
  68. return;
  69. }
  70. if (fchown(keyfd, owner, group) == -1) {
  71. pam_syslog(pamh, LOG_ERR, "Cannot chown %s: %m", filename);
  72. close(keyfd);
  73. return;
  74. }
  75. /* Open the random device to get key data. */
  76. randfd = open("/dev/urandom", O_RDONLY);
  77. if (randfd == -1) {
  78. pam_syslog(pamh, LOG_ERR, "Cannot open /dev/urandom: %m");
  79. close(keyfd);
  80. return;
  81. }
  82. /* Read random data for use as the key. */
  83. key = malloc(key_size);
  84. count = 0;
  85. if (!key) {
  86. close(keyfd);
  87. close(randfd);
  88. return;
  89. }
  90. while (count < key_size) {
  91. i = read(randfd, key + count, key_size - count);
  92. if ((i == 0) || (i == -1)) {
  93. break;
  94. }
  95. count += i;
  96. }
  97. close(randfd);
  98. /* If we didn't get enough, stop here. */
  99. if (count < key_size) {
  100. pam_syslog(pamh, LOG_ERR, "Short read on random device");
  101. memset(key, 0, key_size);
  102. free(key);
  103. close(keyfd);
  104. return;
  105. }
  106. /* Now write the key. */
  107. count = 0;
  108. while (count < key_size) {
  109. i = write(keyfd, key + count, key_size - count);
  110. if ((i == 0) || (i == -1)) {
  111. break;
  112. }
  113. count += i;
  114. }
  115. memset(key, 0, key_size);
  116. free(key);
  117. close(keyfd);
  118. }
  119. static void
  120. hmac_key_read(pam_handle_t *pamh, const char *filename, size_t default_key_size,
  121. uid_t owner, gid_t group,
  122. void **key, size_t *key_size)
  123. {
  124. char *tmp;
  125. int keyfd, i, count;
  126. struct stat st;
  127. tmp = NULL;
  128. *key = NULL;
  129. *key_size = 0;
  130. /* Try to open the key file. */
  131. keyfd = open(filename, O_RDONLY);
  132. if (keyfd == -1) {
  133. /* No such thing? Create it. */
  134. if (errno == ENOENT) {
  135. hmac_key_create(pamh, filename, default_key_size,
  136. owner, group);
  137. keyfd = open(filename, O_RDONLY);
  138. } else {
  139. pam_syslog(pamh, LOG_ERR, "Cannot open %s: %m", filename);
  140. }
  141. if (keyfd == -1)
  142. return;
  143. }
  144. /* If we failed to open the file, we're done. */
  145. if (fstat(keyfd, &st) == -1) {
  146. close(keyfd);
  147. return;
  148. }
  149. /* Read the contents of the file. */
  150. tmp = malloc(st.st_size);
  151. if (!tmp) {
  152. close(keyfd);
  153. return;
  154. }
  155. count = 0;
  156. while (count < st.st_size) {
  157. i = read(keyfd, tmp + count, st.st_size - count);
  158. if ((i == 0) || (i == -1)) {
  159. break;
  160. }
  161. count += i;
  162. }
  163. close(keyfd);
  164. /* Require that we got the expected amount of data. */
  165. if (count < st.st_size) {
  166. memset(tmp, 0, st.st_size);
  167. free(tmp);
  168. return;
  169. }
  170. /* Pass the key back. */
  171. *key = tmp;
  172. *key_size = st.st_size;
  173. }
  174. static void
  175. xor_block(unsigned char *p, unsigned char byte, size_t length)
  176. {
  177. size_t i;
  178. for (i = 0; i < length; i++) {
  179. p[i] = p[i] ^ byte;
  180. }
  181. }
  182. void
  183. hmac_sha1_generate(void **mac, size_t *mac_length,
  184. const void *raw_key, size_t raw_key_size,
  185. const void *text, size_t text_length)
  186. {
  187. unsigned char key[MAXIMUM_KEY_SIZE], tmp_key[MAXIMUM_KEY_SIZE];
  188. size_t maximum_key_size = SHA1_BLOCK_SIZE,
  189. minimum_key_size = SHA1_OUTPUT_SIZE;
  190. const unsigned char ipad = 0x36, opad = 0x5c;
  191. struct sha1_context sha1;
  192. unsigned char inner[SHA1_OUTPUT_SIZE], outer[SHA1_OUTPUT_SIZE];
  193. *mac = NULL;
  194. *mac_length = 0;
  195. #ifndef HMAC_ALLOW_SHORT_KEYS
  196. /* If the key is too short, don't bother. */
  197. if (raw_key_size < minimum_key_size) {
  198. return;
  199. }
  200. #endif
  201. /* If the key is too long, "compress" it, else copy it and pad it
  202. * out with zero bytes. */
  203. memset(key, 0, sizeof(key));
  204. if (raw_key_size > maximum_key_size) {
  205. sha1_init(&sha1);
  206. sha1_update(&sha1, raw_key, raw_key_size);
  207. sha1_output(&sha1, key);
  208. } else {
  209. memmove(key, raw_key, raw_key_size);
  210. }
  211. /* Generate the inner sum. */
  212. memcpy(tmp_key, key, sizeof(tmp_key));
  213. xor_block(tmp_key, ipad, sizeof(tmp_key));
  214. sha1_init(&sha1);
  215. sha1_update(&sha1, tmp_key, sizeof(tmp_key));
  216. sha1_update(&sha1, text, text_length);
  217. sha1_output(&sha1, inner);
  218. /* Generate the outer sum. */
  219. memcpy(tmp_key, key, sizeof(tmp_key));
  220. xor_block(tmp_key, opad, sizeof(tmp_key));
  221. sha1_init(&sha1);
  222. sha1_update(&sha1, tmp_key, sizeof(tmp_key));
  223. sha1_update(&sha1, inner, sizeof(inner));
  224. sha1_output(&sha1, outer);
  225. /* We don't need any of the keys any more. */
  226. memset(key, 0, sizeof(key));
  227. memset(tmp_key, 0, sizeof(tmp_key));
  228. /* Allocate space to store the output. */
  229. *mac_length = sizeof(outer);
  230. *mac = malloc(*mac_length);
  231. if (*mac == NULL) {
  232. *mac_length = 0;
  233. return;
  234. }
  235. memcpy(*mac, outer, *mac_length);
  236. }
  237. void
  238. hmac_sha1_generate_file(pam_handle_t *pamh, void **mac, size_t *mac_length,
  239. const char *keyfile, uid_t owner, gid_t group,
  240. const void *text, size_t text_length)
  241. {
  242. void *key;
  243. size_t key_length;
  244. hmac_key_read(pamh, keyfile,
  245. MAXIMUM_KEY_SIZE, owner, group,
  246. &key, &key_length);
  247. if (key == NULL) {
  248. *mac = NULL;
  249. *mac_length = 0;
  250. return;
  251. }
  252. hmac_sha1_generate(mac, mac_length,
  253. key, key_length,
  254. text, text_length);
  255. memset(key, 0, key_length);
  256. free(key);
  257. }
  258. size_t
  259. hmac_sha1_size(void)
  260. {
  261. return SHA1_OUTPUT_SIZE;
  262. }