All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
otp_util.c
Go to the documentation of this file.
1 /*
2  * $Id: 72ac76a8c7bde70a22d475558c047ab9ff5d9640 $
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  *
18  * Copyright 2001,2002 Google, Inc.
19  * Copyright 2005,2006 TRI-D Systems, Inc.
20  */
21 
22 RCSID("$Id: 72ac76a8c7bde70a22d475558c047ab9ff5d9640 $")
23 
24 #include "extern.h"
25 
26 #include <inttypes.h>
27 #include <pthread.h>
28 #include <sys/types.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 /** Generate some random bytes
33  *
34  * @param rnd_data Buffer to write bytes to.
35  * @param len Number of bytes to write.
36  */
37 void otp_get_random(uint8_t *rnd_data, size_t len)
38 {
39  size_t bytes_read = 0;
40  size_t bytes_left;
41  int n;
42 
43  while (bytes_read < len) {
44  bytes_left = len - bytes_read;
45  uint32_t r = fr_rand();
46 
47  n = sizeof(r) < bytes_left ? sizeof(r) : bytes_left;
48 
49  memcpy(rnd_data + bytes_read, &r, n);
50 
51  bytes_read += n;
52  }
53 }
54 
55 /** Generate a random challenge (ascii chars 0-9)
56  *
57  * @note This is really cryptocard-specific (automatic ASCII conversion
58  * @note and null termination).
59  *
60  * @param[out] challenge Buffer to write random string to.
61  * @param[in] len Number of random bytes to write to buffer.
62  */
63 void otp_async_challenge(char challenge[OTP_MAX_CHALLENGE_LEN + 1],
64  size_t len)
65 {
66  uint8_t rawchallenge[OTP_MAX_CHALLENGE_LEN];
67  unsigned int i;
68 
69  otp_get_random(rawchallenge, len);
70 
71  /* Convert the raw bytes to ASCII decimal. */
72  for (i = 0; i < len; ++i) {
73  challenge[i] = '0' + rawchallenge[i] % 10;
74  }
75 
76  challenge[len] = '\0';
77 }
78 
79 /** Guaranteed initialization
80  *
81  */
82 void _otp_pthread_mutex_init(pthread_mutex_t *mutexp, pthread_mutexattr_t const *attr, char const *caller)
83 {
84  int rc;
85 
86  rc = pthread_mutex_init(mutexp, attr);
87  if (rc) {
88  ERROR("rlm_otp: %s: pthread_mutex_init: %s",
89  caller, fr_syserror(rc));
90 
91  fr_exit(1);
92  }
93 }
94 
95 /** Guaranteed lock
96  *
97  */
98 void _otp_pthread_mutex_lock(pthread_mutex_t *mutexp, char const *caller)
99 {
100  int rc;
101 
102  rc = pthread_mutex_lock(mutexp);
103  if (rc) {
104  ERROR("rlm_otp: %s: pthread_mutex_lock: %s",
105  caller, fr_syserror(rc));
106 
107  fr_exit(1);
108  }
109 }
110 
111 /** Guaranteed trylock
112  *
113  */
114 int _otp_pthread_mutex_trylock(pthread_mutex_t *mutexp, char const *caller)
115 {
116  int rc;
117 
118  rc = pthread_mutex_trylock(mutexp);
119  if (rc && rc != EBUSY) {
120  ERROR("rlm_otp: %s: pthread_mutex_trylock: %s",
121  caller, fr_syserror(rc));
122 
123  fr_exit(1);
124  }
125 
126  return rc;
127 }
128 
129 /** Guaranteed unlock
130  *
131  */
132 void _otp_pthread_mutex_unlock(pthread_mutex_t *mutexp, char const *caller)
133 {
134  int rc;
135 
136  rc = pthread_mutex_unlock(mutexp);
137  if (rc) {
138  ERROR("rlm_otp: %s: pthread_mutex_unlock: %s",
139  caller, fr_syserror(rc));
140 
141  fr_exit(1);
142  }
143 }
#define pthread_mutex_init(_x, _y)
Definition: rlm_eap.h:75
uint32_t fr_rand(void)
Return a 32-bit random number.
Definition: radius.c:1621
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition: log.c:238
void _otp_pthread_mutex_lock(pthread_mutex_t *mutexp, char const *caller)
Guaranteed lock.
Definition: otp_util.c:98
#define pthread_mutex_unlock(_x)
Definition: rlm_eap.h:78
void _otp_pthread_mutex_unlock(pthread_mutex_t *mutexp, char const *caller)
Guaranteed unlock.
Definition: otp_util.c:132
void otp_async_challenge(char challenge[OTP_MAX_CHALLENGE_LEN+1], size_t len)
Generate a random challenge (ascii chars 0-9)
Definition: otp_util.c:63
#define OTP_MAX_CHALLENGE_LEN
Definition: otp.h:35
void _otp_pthread_mutex_init(pthread_mutex_t *mutexp, pthread_mutexattr_t const *attr, char const *caller)
Guaranteed initialization.
Definition: otp_util.c:82
#define pthread_mutex_lock(_x)
Definition: rlm_eap.h:77
#define RCSID(id)
Definition: build.h:135
int _otp_pthread_mutex_trylock(pthread_mutex_t *mutexp, char const *caller)
Guaranteed trylock.
Definition: otp_util.c:114
static int r
Definition: rbmonkey.c:66
void otp_get_random(uint8_t *rnd_data, size_t len)
Generate some random bytes.
Definition: otp_util.c:37
#define fr_exit(_x)
Definition: libradius.h:508
#define ERROR(fmt,...)
Definition: log.h:145