| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include "otp.h" |
| 26 |
#include "crypto.h" |
| 27 |
|
| 28 |
static const char *trace_channel = "auth_otp"; |
| 29 |
|
| 30 |
static int otp(pool *p, const EVP_MD *md, |
| 31 |
const unsigned char *key, size_t key_len, |
| 32 |
unsigned long counter, unsigned int *code) { |
| 33 |
register unsigned int i; |
| 34 |
unsigned char hash[EVP_MAX_MD_SIZE], value[8]; |
| 35 |
size_t hash_len; |
| 36 |
int offset = 0; |
| 37 |
unsigned int truncated = 0; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
for (i = sizeof(value); i--; counter >>= 8) { |
| 44 |
value[i] = counter; |
| 45 |
} |
| 46 |
|
| 47 |
hash_len = EVP_MAX_MD_SIZE; |
| 48 |
if (auth_otp_hmac(md, key, key_len, value, sizeof(value), hash, |
| 49 |
&hash_len) < 0) { |
| 50 |
return -1; |
| 51 |
} |
| 52 |
|
| 53 |
pr_memscrub(value, sizeof(value)); |
| 54 |
|
| 55 |
offset = hash[hash_len-1] & 0x0f; |
| 56 |
|
| 57 |
truncated = ((hash[offset+0] & 0x7f) << 24) | |
| 58 |
((hash[offset+1] & 0xff) << 16) | |
| 59 |
((hash[offset+2] & 0xff) << 8) | |
| 60 |
(hash[offset+3] & 0xff); |
| 61 |
|
| 62 |
pr_memscrub(hash, sizeof(hash)); |
| 63 |
|
| 64 |
truncated &= 0x7fffffff; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
*code = truncated % 1000000; |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
int auth_otp_hotp(pool *p, const unsigned char *key, size_t key_len, |
| 74 |
unsigned long counter, unsigned int *code) { |
| 75 |
const EVP_MD *md; |
| 76 |
int res; |
| 77 |
|
| 78 |
if (p == NULL || |
| 79 |
key == NULL || |
| 80 |
key_len == 0 || |
| 81 |
code == NULL) { |
| 82 |
errno = EINVAL; |
| 83 |
return -1; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
md = EVP_sha1(); |
| 88 |
|
| 89 |
res = otp(p, md, key, key_len, counter, code); |
| 90 |
return res; |
| 91 |
} |
| 92 |
|
| 93 |
int auth_otp_totp(pool *p, const unsigned char *key, size_t key_len, |
| 94 |
unsigned long ts, unsigned int algo, unsigned int *code) { |
| 95 |
const EVP_MD *md; |
| 96 |
unsigned long counter; |
| 97 |
int res; |
| 98 |
|
| 99 |
if (p == NULL || |
| 100 |
key == NULL || |
| 101 |
key_len == 0 || |
| 102 |
code == NULL) { |
| 103 |
errno = EINVAL; |
| 104 |
return -1; |
| 105 |
} |
| 106 |
|
| 107 |
switch (algo) { |
| 108 |
case AUTH_OTP_ALGO_TOTP_SHA1: |
| 109 |
md = EVP_sha1(); |
| 110 |
break; |
| 111 |
|
| 112 |
#ifdef HAVE_SHA256_OPENSSL |
| 113 |
case AUTH_OTP_ALGO_TOTP_SHA256: |
| 114 |
md = EVP_sha256(); |
| 115 |
break; |
| 116 |
#endif |
| 117 |
|
| 118 |
#ifdef HAVE_SHA512_OPENSSL |
| 119 |
case AUTH_OTP_ALGO_TOTP_SHA512: |
| 120 |
md = EVP_sha512(); |
| 121 |
break; |
| 122 |
#endif |
| 123 |
|
| 124 |
default: |
| 125 |
pr_trace_msg(trace_channel, 4, |
| 126 |
"unsupported TOTP algorithm ID %u requested", algo); |
| 127 |
errno = EINVAL; |
| 128 |
return -1; |
| 129 |
} |
| 130 |
|
| 131 |
counter = ts / AUTH_OTP_TOTP_TIMESTEP_SECS; |
| 132 |
res = otp(p, md, key, key_len, counter, code); |
| 133 |
return res; |
| 134 |
} |