aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/lib/libotputil/lrfc1760-skey.c
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2023-01-12 02:59:18 -0900
committerDavid M. Syzdek <david@syzdek.net>2023-01-12 02:59:18 -0900
commitb997dc89a55142f42c8da77b3b35ae5a7f3a072c (patch)
treedae476d8abde417878d21dfe76380a6115c3445d /lib/libotputil/lrfc1760-skey.c
parent6ddf4274ca1c6faa9cd05c45de46426f38c03513 (diff)
downloadotputils-b997dc89a55142f42c8da77b3b35ae5a7f3a072c.tar.gz
otputils-b997dc89a55142f42c8da77b3b35ae5a7f3a072c.tar.bz2
otputils-b997dc89a55142f42c8da77b3b35ae5a7f3a072c.tar.xz
otputils-b997dc89a55142f42c8da77b3b35ae5a7f3a072c.zip
libotputil: adding otputil_skey_code() and otputil_skey_str()
Diffstat (limited to 'lib/libotputil/lrfc1760-skey.c')
-rw-r--r--lib/libotputil/lrfc1760-skey.c87
1 files changed, 87 insertions, 0 deletions
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
146int
147otputil_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
192char *
193otputil_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 */