diff options
| author | David M. Syzdek <david@syzdek.net> | 2023-01-12 02:59:18 -0900 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2023-01-12 02:59:18 -0900 |
| commit | b997dc89a55142f42c8da77b3b35ae5a7f3a072c (patch) | |
| tree | dae476d8abde417878d21dfe76380a6115c3445d | |
| parent | 6ddf4274ca1c6faa9cd05c45de46426f38c03513 (diff) | |
| download | otputils-b997dc89a55142f42c8da77b3b35ae5a7f3a072c.tar.gz otputils-b997dc89a55142f42c8da77b3b35ae5a7f3a072c.tar.bz2 otputils-b997dc89a55142f42c8da77b3b35ae5a7f3a072c.tar.xz otputils-b997dc89a55142f42c8da77b3b35ae5a7f3a072c.zip | |
libotputil: adding otputil_skey_code() and otputil_skey_str()
| -rw-r--r-- | include/otputil.h | 17 | ||||
| -rw-r--r-- | lib/libotputil/libotputil.sym | 2 | ||||
| -rw-r--r-- | lib/libotputil/lrfc1760-skey.c | 87 |
3 files changed, 106 insertions, 0 deletions
diff --git a/include/otputil.h b/include/otputil.h index 13420fc..b6d6112 100644 --- a/include/otputil.h +++ b/include/otputil.h | |||
| @@ -173,6 +173,8 @@ | |||
| 173 | #define OTPUTIL_OTP_SEED_MIN_LEN 1 | 173 | #define OTPUTIL_OTP_SEED_MIN_LEN 1 |
| 174 | #define OTPUTIL_OTP_SEED_MAX_LEN 16 | 174 | #define OTPUTIL_OTP_SEED_MAX_LEN 16 |
| 175 | 175 | ||
| 176 | #define OTPUTIL_SKEY_PASS_MAX_LEN 127 | ||
| 177 | |||
| 176 | 178 | ||
| 177 | ///////////////// | 179 | ///////////////// |
| 178 | // // | 180 | // // |
| @@ -398,6 +400,13 @@ otputil_otp_str( | |||
| 398 | #pragma mark S/KEY functions (RFC 1760) | 400 | #pragma mark S/KEY functions (RFC 1760) |
| 399 | 401 | ||
| 400 | _OTPUTIL_F int | 402 | _OTPUTIL_F int |
| 403 | otputil_skey_code( | ||
| 404 | const char * skey_pass, | ||
| 405 | int skey_seq, | ||
| 406 | uint64_t * skey_resultp ); | ||
| 407 | |||
| 408 | |||
| 409 | _OTPUTIL_F int | ||
| 401 | otputil_skey_dict_value( | 410 | otputil_skey_dict_value( |
| 402 | const char * word ); | 411 | const char * word ); |
| 403 | 412 | ||
| @@ -407,6 +416,14 @@ otputil_skey_dict_word( | |||
| 407 | int value ); | 416 | int value ); |
| 408 | 417 | ||
| 409 | 418 | ||
| 419 | _OTPUTIL_F char * | ||
| 420 | otputil_skey_str( | ||
| 421 | const char * skey_pass, | ||
| 422 | int skey_seq, | ||
| 423 | char * dst, | ||
| 424 | size_t dstlen ); | ||
| 425 | |||
| 426 | |||
| 410 | //---------------// | 427 | //---------------// |
| 411 | // TOTP functions // | 428 | // TOTP functions // |
| 412 | //---------------// | 429 | //---------------// |
diff --git a/lib/libotputil/libotputil.sym b/lib/libotputil/libotputil.sym index c3faedb..cf898f6 100644 --- a/lib/libotputil/libotputil.sym +++ b/lib/libotputil/libotputil.sym | |||
| @@ -93,8 +93,10 @@ otputil_otp_encode_len | |||
| 93 | otputil_otp_str | 93 | otputil_otp_str |
| 94 | # | 94 | # |
| 95 | # S/KEY functions (RFC 1760) | 95 | # S/KEY functions (RFC 1760) |
| 96 | otputil_skey_code | ||
| 96 | otputil_skey_dict_value | 97 | otputil_skey_dict_value |
| 97 | otputil_skey_dict_word | 98 | otputil_skey_dict_word |
| 99 | otputil_skey_str | ||
| 98 | # | 100 | # |
| 99 | # TOTP functions (RFC 6238) | 101 | # TOTP functions (RFC 6238) |
| 100 | otputil_totp_code | 102 | otputil_totp_code |
diff --git a/lib/libotputil/lrfc1760-skey.c b/lib/libotputil/lrfc1760-skey.c index 62e5d5e..1853455 100644 --- a/lib/libotputil/lrfc1760-skey.c +++ b/lib/libotputil/lrfc1760-skey.c | |||
| @@ -43,6 +43,8 @@ | |||
| 43 | #include <errno.h> | 43 | #include <errno.h> |
| 44 | #include <string.h> | 44 | #include <string.h> |
| 45 | #include <strings.h> | 45 | #include <strings.h> |
| 46 | #include <openssl/evp.h> | ||
| 47 | #include <openssl/hmac.h> | ||
| 46 | 48 | ||
| 47 | #include "lrfc1760-skey-dict.h" | 49 | #include "lrfc1760-skey-dict.h" |
| 48 | 50 | ||
| @@ -136,4 +138,89 @@ otputil_skey_dict_word( | |||
| 136 | } | 138 | } |
| 137 | 139 | ||
| 138 | 140 | ||
| 141 | //---------------// | ||
| 142 | // OTP functions // | ||
| 143 | //---------------// | ||
| 144 | #pragma mark OTP functions | ||
| 145 | |||
| 146 | int | ||
| 147 | otputil_skey_code( | ||
| 148 | const char * skey_pass, | ||
| 149 | int skey_seq, | ||
| 150 | uint64_t * skey_resultp ) | ||
| 151 | { | ||
| 152 | unsigned char secret[OTPUTIL_SKEY_PASS_MAX_LEN+1]; | ||
| 153 | const EVP_MD * evp_md; | ||
| 154 | unsigned char md[EVP_MAX_MD_SIZE]; | ||
| 155 | unsigned md_len; | ||
| 156 | unsigned secret_len; | ||
| 157 | unsigned u; | ||
| 158 | int pos; | ||
| 159 | |||
| 160 | assert(skey_pass != NULL); | ||
| 161 | |||
| 162 | // generate secret | ||
| 163 | secret_len = (int)strlen(skey_pass); | ||
| 164 | if (secret_len > OTPUTIL_SKEY_PASS_MAX_LEN) | ||
| 165 | return(-1); | ||
| 166 | bindle_strlcpy((char *)secret, skey_pass, sizeof(secret)); | ||
| 167 | |||
| 168 | // check otp_hash | ||
| 169 | evp_md = EVP_md4(); | ||
| 170 | |||
| 171 | // generate hashes | ||
| 172 | for(pos = 0; (pos <= skey_seq); pos++) | ||
| 173 | { | ||
| 174 | md_len = sizeof(md); | ||
| 175 | if (!(EVP_Digest(secret, secret_len, md, &md_len, evp_md, NULL))) | ||
| 176 | return(-1); | ||
| 177 | for(u = 0; (u < 8); u++) | ||
| 178 | md[u] ^= md[u+8]; | ||
| 179 | memcpy(secret, md, 8); | ||
| 180 | secret_len = 8; | ||
| 181 | }; | ||
| 182 | |||
| 183 | *skey_resultp = ((uint64_t)secret[0] << 56) | ((uint64_t)secret[1] << 48) | ||
| 184 | | ((uint64_t)secret[2] << 40) | ((uint64_t)secret[3] << 32) | ||
| 185 | | ((uint64_t)secret[4] << 24) | ((uint64_t)secret[5] << 16) | ||
| 186 | | ((uint64_t)secret[6] << 8) | ((uint64_t)secret[7] << 0); | ||
| 187 | |||
| 188 | return(0); | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | char * | ||
| 193 | otputil_skey_str( | ||
| 194 | const char * skey_pass, | ||
| 195 | int skey_seq, | ||
| 196 | char * dst, | ||
| 197 | size_t dstlen ) | ||
| 198 | { | ||
| 199 | uint64_t val; | ||
| 200 | otputil_bv_t res; | ||
| 201 | uint8_t bv_val[8]; | ||
| 202 | static char buff[32]; | ||
| 203 | |||
| 204 | dstlen = ((dst)) ? dstlen : sizeof(buff); | ||
| 205 | dst = ((dst)) ? dst : buff; | ||
| 206 | res.bv_val = bv_val; | ||
| 207 | res.bv_len = sizeof(bv_val); | ||
| 208 | |||
| 209 | if (otputil_skey_code(skey_pass, skey_seq, &val) == -1) | ||
| 210 | return(NULL); | ||
| 211 | |||
| 212 | bv_val[0] = (val >> 56) & 0xff; | ||
| 213 | bv_val[1] = (val >> 48) & 0xff; | ||
| 214 | bv_val[2] = (val >> 40) & 0xff; | ||
| 215 | bv_val[3] = (val >> 32) & 0xff; | ||
| 216 | bv_val[4] = (val >> 24) & 0xff; | ||
| 217 | bv_val[5] = (val >> 16) & 0xff; | ||
| 218 | bv_val[6] = (val >> 8) & 0xff; | ||
| 219 | bv_val[7] = (val >> 0) & 0xff; | ||
| 220 | otputil_otp_encode(&res, dst, dstlen, OTPUTIL_ENC_SIXWORD); | ||
| 221 | |||
| 222 | return(dst); | ||
| 223 | } | ||
| 224 | |||
| 225 | |||
| 139 | /* end of source file */ | 226 | /* end of source file */ |
