|
| 1 | +/* |
| 2 | + * Copyright (c) Edward Thomson. All rights reserved. |
| 3 | + * |
| 4 | + * This file is part of ntlmclient, distributed under the MIT license. |
| 5 | + * For full terms and copyright information, and for third-party |
| 6 | + * copyright information, see the included LICENSE.txt file. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <stdlib.h> |
| 10 | +#include <stdint.h> |
| 11 | +#include <string.h> |
| 12 | +#include <fcntl.h> |
| 13 | +#include <unistd.h> |
| 14 | +#include <errno.h> |
| 15 | + |
| 16 | +#include <CommonCrypto/CommonCrypto.h> |
| 17 | + |
| 18 | +#include "ntlm.h" |
| 19 | +#include "crypt.h" |
| 20 | + |
| 21 | +bool ntlm_random_bytes( |
| 22 | + ntlm_client *ntlm, |
| 23 | + unsigned char *out, |
| 24 | + size_t len) |
| 25 | +{ |
| 26 | + int fd, ret; |
| 27 | + size_t total = 0; |
| 28 | + |
| 29 | + if ((fd = open("/dev/urandom", O_RDONLY)) < 0) { |
| 30 | + ntlm_client_set_errmsg(ntlm, strerror(errno)); |
| 31 | + return false; |
| 32 | + } |
| 33 | + |
| 34 | + while (total < len) { |
| 35 | + if ((ret = read(fd, out, (len - total))) < 0) { |
| 36 | + ntlm_client_set_errmsg(ntlm, strerror(errno)); |
| 37 | + return false; |
| 38 | + } else if (ret == 0) { |
| 39 | + ntlm_client_set_errmsg(ntlm, "unexpected eof on random device"); |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + total += ret; |
| 44 | + } |
| 45 | + |
| 46 | + close(fd); |
| 47 | + return true; |
| 48 | +} |
| 49 | + |
| 50 | +bool ntlm_des_encrypt( |
| 51 | + ntlm_des_block *out, |
| 52 | + ntlm_des_block *plaintext, |
| 53 | + ntlm_des_block *key) |
| 54 | +{ |
| 55 | + size_t written; |
| 56 | + |
| 57 | + CCCryptorStatus result = CCCrypt(kCCEncrypt, |
| 58 | + kCCAlgorithmDES, kCCOptionECBMode, |
| 59 | + key, sizeof(ntlm_des_block), NULL, |
| 60 | + plaintext, sizeof(ntlm_des_block), |
| 61 | + out, sizeof(ntlm_des_block), &written); |
| 62 | + |
| 63 | + return (result == kCCSuccess) ? true : false; |
| 64 | +} |
| 65 | + |
| 66 | +bool ntlm_md4_digest( |
| 67 | + unsigned char out[CRYPT_MD4_DIGESTSIZE], |
| 68 | + const unsigned char *in, |
| 69 | + size_t in_len) |
| 70 | +{ |
| 71 | + return !!CC_MD4(in, in_len, out); |
| 72 | +} |
| 73 | + |
| 74 | +ntlm_hmac_ctx *ntlm_hmac_ctx_init(void) |
| 75 | +{ |
| 76 | + return calloc(1, sizeof(ntlm_hmac_ctx)); |
| 77 | +} |
| 78 | + |
| 79 | +bool ntlm_hmac_ctx_reset(ntlm_hmac_ctx *ctx) |
| 80 | +{ |
| 81 | + memset(ctx, 0, sizeof(ntlm_hmac_ctx)); |
| 82 | + return true; |
| 83 | +} |
| 84 | + |
| 85 | +bool ntlm_hmac_md5_init( |
| 86 | + ntlm_hmac_ctx *ctx, |
| 87 | + const unsigned char *key, |
| 88 | + size_t key_len) |
| 89 | +{ |
| 90 | + CCHmacInit(&ctx->native, kCCHmacAlgMD5, key, key_len); |
| 91 | + return true; |
| 92 | +} |
| 93 | + |
| 94 | +bool ntlm_hmac_md5_update( |
| 95 | + ntlm_hmac_ctx *ctx, |
| 96 | + const unsigned char *data, |
| 97 | + size_t data_len) |
| 98 | +{ |
| 99 | + CCHmacUpdate(&ctx->native, data, data_len); |
| 100 | + return true; |
| 101 | +} |
| 102 | + |
| 103 | +bool ntlm_hmac_md5_final( |
| 104 | + unsigned char *out, |
| 105 | + size_t *out_len, |
| 106 | + ntlm_hmac_ctx *ctx) |
| 107 | +{ |
| 108 | + if (*out_len < CRYPT_MD5_DIGESTSIZE) |
| 109 | + return false; |
| 110 | + |
| 111 | + CCHmacFinal(&ctx->native, out); |
| 112 | + |
| 113 | + *out_len = CRYPT_MD5_DIGESTSIZE; |
| 114 | + return true; |
| 115 | +} |
| 116 | + |
| 117 | +void ntlm_hmac_ctx_free(ntlm_hmac_ctx *ctx) |
| 118 | +{ |
| 119 | + free(ctx); |
| 120 | +} |
0 commit comments