diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/libotputil/libotputil.sym | 1 | ||||
| -rw-r--r-- | lib/libotputil/lrfc2289-otp.c | 127 | ||||
| -rw-r--r-- | lib/libotputil/lrfc2289-otp.h | 47 |
3 files changed, 175 insertions, 0 deletions
diff --git a/lib/libotputil/libotputil.sym b/lib/libotputil/libotputil.sym index 647def9..cd4b5a6 100644 --- a/lib/libotputil/libotputil.sym +++ b/lib/libotputil/libotputil.sym | |||
| @@ -85,6 +85,7 @@ otputil_hotp_code | |||
| 85 | otputil_hotp_str | 85 | otputil_hotp_str |
| 86 | # | 86 | # |
| 87 | # OTP functions (RFC 2289) | 87 | # OTP functions (RFC 2289) |
| 88 | otputil_otp_code | ||
| 88 | otputil_otp_decode | 89 | otputil_otp_decode |
| 89 | otputil_otp_decode_len | 90 | otputil_otp_decode_len |
| 90 | otputil_otp_encode | 91 | otputil_otp_encode |
diff --git a/lib/libotputil/lrfc2289-otp.c b/lib/libotputil/lrfc2289-otp.c new file mode 100644 index 0000000..e037f83 --- /dev/null +++ b/lib/libotputil/lrfc2289-otp.c | |||
| @@ -0,0 +1,127 @@ | |||
| 1 | /* | ||
| 2 | * OTP Utilities | ||
| 3 | * Copyright (C) 2022 David M. Syzdek <david@syzdek.net>. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are | ||
| 7 | * met: | ||
| 8 | * | ||
| 9 | * * Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * * Redistributions in binary form must reproduce the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer in the | ||
| 13 | * documentation and/or other materials provided with the distribution. | ||
| 14 | * * Neither the name of David M. Syzdek nor the | ||
| 15 | * names of its contributors may be used to endorse or promote products | ||
| 16 | * derived from this software without specific prior written permission. | ||
| 17 | * | ||
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 19 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| 20 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 21 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M SYZDEK BE LIABLE FOR | ||
| 22 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 28 | * SUCH DAMAGE. | ||
| 29 | */ | ||
| 30 | /* | ||
| 31 | * @file lib/libotputil/lrfc2289-otp.c | ||
| 32 | */ | ||
| 33 | #define _LIB_LIBOTPUTIL_LRFC2289_OTP_H 1 | ||
| 34 | #include "lrfc2289-otp.h" | ||
| 35 | |||
| 36 | /////////////// | ||
| 37 | // // | ||
| 38 | // Headers // | ||
| 39 | // // | ||
| 40 | /////////////// | ||
| 41 | #pragma mark - Headers | ||
| 42 | |||
| 43 | #include <errno.h> | ||
| 44 | #include <stdarg.h> | ||
| 45 | #include <string.h> | ||
| 46 | #include <strings.h> | ||
| 47 | #include <stdlib.h> | ||
| 48 | #include <ctype.h> | ||
| 49 | #include <openssl/evp.h> | ||
| 50 | #include <openssl/hmac.h> | ||
| 51 | |||
| 52 | #include "lmisc.h" | ||
| 53 | |||
| 54 | |||
| 55 | ////////////////// | ||
| 56 | // // | ||
| 57 | // Prototypes // | ||
| 58 | // // | ||
| 59 | ////////////////// | ||
| 60 | #pragma mark - Prototypes | ||
| 61 | |||
| 62 | |||
| 63 | ///////////////// | ||
| 64 | // // | ||
| 65 | // Functions // | ||
| 66 | // // | ||
| 67 | ///////////////// | ||
| 68 | #pragma mark - Functions | ||
| 69 | |||
| 70 | uint64_t | ||
| 71 | otputil_otp_code( | ||
| 72 | const char * otp_pass, | ||
| 73 | const char * otp_seed, | ||
| 74 | int otp_seq, | ||
| 75 | int otp_hash ) | ||
| 76 | { | ||
| 77 | char secret[OTPUTIL_OTP_PASS_MAX_LEN+OTPUTIL_OTP_SEED_MAX_LEN+1]; | ||
| 78 | size_t len; | ||
| 79 | const EVP_MD * evp_md; | ||
| 80 | unsigned char md[EVP_MAX_MD_SIZE]; | ||
| 81 | unsigned md_len; | ||
| 82 | unsigned secret_len; | ||
| 83 | unsigned u; | ||
| 84 | int pos; | ||
| 85 | |||
| 86 | assert(otp_pass != NULL); | ||
| 87 | assert(otp_seed != NULL); | ||
| 88 | |||
| 89 | // check input parameters | ||
| 90 | if (otp_seq == 0) | ||
| 91 | return(0); | ||
| 92 | if ((len = strlen(otp_pass)) > OTPUTIL_OTP_PASS_MAX_LEN) | ||
| 93 | return(0); | ||
| 94 | if (len < OTPUTIL_OTP_PASS_MIN_LEN) | ||
| 95 | return(0); | ||
| 96 | if ((len = strlen(otp_seed)) > OTPUTIL_OTP_SEED_MAX_LEN) | ||
| 97 | return(0); | ||
| 98 | if (len < OTPUTIL_OTP_SEED_MIN_LEN) | ||
| 99 | return(0); | ||
| 100 | if ((evp_md = otputil_evp_md(otp_hash)) == NULL) | ||
| 101 | return(0); | ||
| 102 | |||
| 103 | // generate secret | ||
| 104 | strncpy(secret, otp_pass, sizeof(secret)); | ||
| 105 | strncat(secret, otp_seed, sizeof(secret)-1); | ||
| 106 | secret_len = (unsigned)strlen(secret); | ||
| 107 | |||
| 108 | // generate hashes | ||
| 109 | for(pos = 0; (pos < otp_seq); pos++) | ||
| 110 | { | ||
| 111 | md_len = sizeof(md); | ||
| 112 | if (!(EVP_Digest(secret, secret_len, md, &md_len, evp_md, NULL))) | ||
| 113 | return(0); | ||
| 114 | for(u = 8; (u < md_len); u += 8) | ||
| 115 | md[u%8] ^= md[u]; | ||
| 116 | memcpy(secret, md, 8); | ||
| 117 | secret_len = 8; | ||
| 118 | }; | ||
| 119 | |||
| 120 | return( ((uint64_t)md[0] << 56) | ((uint64_t)md[1] << 48) | ||
| 121 | | ((uint64_t)md[2] << 40) | ((uint64_t)md[3] << 32) | ||
| 122 | | ((uint64_t)md[4] << 24) | ((uint64_t)md[5] << 16) | ||
| 123 | | ((uint64_t)md[6] << 8) | ((uint64_t)md[7] << 0) ); | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | /* end of source file */ | ||
diff --git a/lib/libotputil/lrfc2289-otp.h b/lib/libotputil/lrfc2289-otp.h new file mode 100644 index 0000000..7f8124a --- /dev/null +++ b/lib/libotputil/lrfc2289-otp.h | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | /* | ||
| 2 | * OTP Utilities | ||
| 3 | * Copyright (C) 2022 David M. Syzdek <david@syzdek.net>. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are | ||
| 7 | * met: | ||
| 8 | * | ||
| 9 | * * Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * * Redistributions in binary form must reproduce the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer in the | ||
| 13 | * documentation and/or other materials provided with the distribution. | ||
| 14 | * * Neither the name of David M. Syzdek nor the | ||
| 15 | * names of its contributors may be used to endorse or promote products | ||
| 16 | * derived from this software without specific prior written permission. | ||
| 17 | * | ||
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 19 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| 20 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 21 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M SYZDEK BE LIABLE FOR | ||
| 22 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 28 | * SUCH DAMAGE. | ||
| 29 | */ | ||
| 30 | /* | ||
| 31 | * @file lib/libotputil/lrfc2289-otp.h | ||
| 32 | */ | ||
| 33 | #ifndef _LIB_LIBOTPUTIL_LRFC2289_OTP_H | ||
| 34 | #define _LIB_LIBOTPUTIL_LRFC2289_OTP_H 1 | ||
| 35 | |||
| 36 | |||
| 37 | /////////////// | ||
| 38 | // // | ||
| 39 | // Headers // | ||
| 40 | // // | ||
| 41 | /////////////// | ||
| 42 | #pragma mark - Headers | ||
| 43 | |||
| 44 | #include "libotputil.h" | ||
| 45 | |||
| 46 | |||
| 47 | #endif /* end of header file */ | ||
