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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
/*
* Copyright (c) 1997 Stanford University
*
* The use of this software for revenue-generating purposes may require a
* license from the owners of the underlying intellectual property.
* Specifically, the SRP protocol may not be used for revenue-generating
* purposes without license.
*
* Within that constraint, permission to use, copy, modify, and distribute
* this software and its documentation for any purpose is hereby granted
* without fee, provided that the above copyright notices and this permission
* notice appear in all copies of the software and related documentation.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
* THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include "t_defines.h"
#include "t_pwd.h"
#include "t_client.h"
#include "t_sha.h"
struct t_client *
t_clientopen(u, n, g, s)
const char * u;
struct t_num * n;
struct t_num * g;
struct t_num * s;
{
struct t_client * tc;
unsigned char buf1[SHA_DIGESTSIZE], buf2[SHA_DIGESTSIZE];
SHA1_CTX ctxt;
int i;
if((tc = malloc(sizeof(struct t_client))) == 0)
return 0;
strncpy(tc->username, u, MAXUSERLEN);
SHA1Init(&tc->hash);
tc->n.len = n->len;
tc->n.data = tc->nbuf;
memcpy(tc->n.data, n->data, tc->n.len);
SHA1Init(&ctxt);
SHA1Update(&ctxt, tc->n.data, tc->n.len);
SHA1Final(buf1, &ctxt);
tc->g.len = g->len;
tc->g.data = tc->gbuf;
memcpy(tc->g.data, g->data, tc->g.len);
SHA1Init(&ctxt);
SHA1Update(&ctxt, tc->g.data, tc->g.len);
SHA1Final(buf2, &ctxt);
for(i = 0; i < sizeof(buf1); ++i)
buf1[i] ^= buf2[i];
SHA1Update(&tc->hash, buf1, sizeof(buf1));
SHA1Init(&ctxt);
SHA1Update(&ctxt, tc->username, strlen(tc->username));
SHA1Final(buf1, &ctxt);
SHA1Update(&tc->hash, buf1, sizeof(buf1));
tc->s.len = s->len;
tc->s.data = tc->sbuf;
memcpy(tc->s.data, s->data, tc->s.len);
SHA1Update(&tc->hash, tc->s.data, tc->s.len);
tc->a.data = tc->abuf;
tc->A.data = tc->Abuf;
tc->p.data = tc->pbuf;
tc->v.data = tc->vbuf;
SHA1Init(&tc->ckhash);
return tc;
}
struct t_num *
t_clientgenexp(tc)
struct t_client * tc;
{
BigInteger a, A, n, g;
char hexbuf[MAXHEXPARAMLEN];
if(tc->n.len < ALEN)
tc->a.len = tc->n.len;
else
tc->a.len = ALEN;
t_random(tc->a.data, tc->a.len);
a = BigIntegerFromBytes(tc->a.data, tc->a.len);
n = BigIntegerFromBytes(tc->n.data, tc->n.len);
g = BigIntegerFromBytes(tc->g.data, tc->g.len);
A = BigIntegerFromInt(0);
BigIntegerModExp(A, g, a, n);
tc->A.len = BigIntegerToBytes(A, tc->A.data);
BigIntegerFree(A);
BigIntegerFree(a);
BigIntegerFree(g);
BigIntegerFree(n);
SHA1Update(&tc->hash, tc->A.data, tc->A.len);
SHA1Update(&tc->ckhash, tc->A.data, tc->A.len);
return &tc->A;
}
void
t_clientpasswd(tc, password)
struct t_client * tc;
char * password;
{
BigInteger n, g, p, v;
SHA1_CTX ctxt;
unsigned char dig[SHA_DIGESTSIZE];
char hexbuf[MAXHEXPARAMLEN];
n = BigIntegerFromBytes(tc->n.data, tc->n.len);
g = BigIntegerFromBytes(tc->g.data, tc->g.len);
SHA1Init(&ctxt);
SHA1Update(&ctxt, tc->username, strlen(tc->username));
SHA1Update(&ctxt, ":", 1);
SHA1Update(&ctxt, password, strlen(password));
SHA1Final(dig, &ctxt);
SHA1Init(&ctxt);
SHA1Update(&ctxt, tc->s.data, tc->s.len);
SHA1Update(&ctxt, dig, sizeof(dig));
SHA1Final(dig, &ctxt);
p = BigIntegerFromBytes(dig, sizeof(dig));
v = BigIntegerFromInt(0);
BigIntegerModExp(v, g, p, n);
tc->p.len = BigIntegerToBytes(p, tc->p.data);
BigIntegerFree(p);
tc->v.len = BigIntegerToBytes(v, tc->v.data);
BigIntegerFree(v);
}
unsigned char *
t_clientgetkey(tc, serverval)
struct t_client * tc;
struct t_num * serverval;
{
BigInteger n, B, v, p, a, sum, S;
unsigned char sbuf[MAXPARAMLEN];
unsigned char dig[SHA_DIGESTSIZE];
unsigned slen;
unsigned int u;
char hexbuf[MAXHEXPARAMLEN];
SHA1_CTX ctxt;
SHA1Init(&ctxt);
SHA1Update(&ctxt, serverval->data, serverval->len);
SHA1Final(dig, &ctxt);
u = (dig[0] << 24) | (dig[1] << 16) | (dig[2] << 8) | dig[3];
if(u == 0)
return NULL;
SHA1Update(&tc->hash, serverval->data, serverval->len);
B = BigIntegerFromBytes(serverval->data, serverval->len);
n = BigIntegerFromBytes(tc->n.data, tc->n.len);
if(BigIntegerCmp(B, n) >= 0 || BigIntegerCmpInt(B, 0) == 0) {
BigIntegerFree(B);
BigIntegerFree(n);
return NULL;
}
v = BigIntegerFromBytes(tc->v.data, tc->v.len);
if(BigIntegerCmp(B, v) < 0)
BigIntegerAdd(B, B, n);
BigIntegerSub(B, B, v);
BigIntegerFree(v);
a = BigIntegerFromBytes(tc->a.data, tc->a.len);
p = BigIntegerFromBytes(tc->p.data, tc->p.len);
sum = BigIntegerFromInt(0);
BigIntegerMulInt(sum, p, u);
BigIntegerAdd(sum, sum, a);
BigIntegerFree(p);
BigIntegerFree(a);
S = BigIntegerFromInt(0);
BigIntegerModExp(S, B, sum, n);
slen = BigIntegerToBytes(S, sbuf);
BigIntegerFree(S);
BigIntegerFree(sum);
BigIntegerFree(B);
BigIntegerFree(n);
t_sessionkey(tc->session_key, sbuf, slen);
memset(sbuf, 0, slen);
SHA1Update(&tc->hash, tc->session_key, sizeof(tc->session_key));
SHA1Final(tc->session_response, &tc->hash);
SHA1Update(&tc->ckhash, tc->session_response, sizeof(tc->session_response));
SHA1Update(&tc->ckhash, tc->session_key, sizeof(tc->session_key));
return tc->session_key;
}
int
t_clientverify(tc, resp)
struct t_client * tc;
unsigned char * resp;
{
unsigned char expected[SHA_DIGESTSIZE];
SHA1Final(expected, &tc->ckhash);
return memcmp(expected, resp, sizeof(expected));
}
unsigned char *
t_clientresponse(tc)
struct t_client * tc;
{
return tc->session_response;
}
void
t_clientclose(tc)
struct t_client * tc;
{
memset(tc->abuf, 0, sizeof(tc->abuf));
memset(tc->pbuf, 0, sizeof(tc->pbuf));
memset(tc->vbuf, 0, sizeof(tc->vbuf));
memset(tc->session_key, 0, sizeof(tc->session_key));
free(tc);
}
|