File: srp.h

package info (click to toggle)
sfs 1%3A0.8-0%2Bpre20060720.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 9,668 kB
  • ctags: 14,317
  • sloc: cpp: 78,358; ansic: 15,494; sh: 9,540; yacc: 786; makefile: 706; perl: 676; lex: 553; python: 146; sed: 70
file content (125 lines) | stat: -rw-r--r-- 3,342 bytes parent folder | download | duplicates (2)
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
// -*-c++-*-
/* $Id: srp.h,v 1.19 2004/05/12 08:42:38 dm Exp $ */

/*
 *
 * Copyright (C) 1999 David Mazieres (dm@uun.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 */

#ifndef _SRP_H_
#define _SRP_H_ 1

#include "bigint.h"
#include "sha1.h"
#include "blowfish.h"

typedef rpc_opaque<20> srp_hash;
typedef rpc_bytes<RPC_INFINITY> srpmsg;

enum srpres { SRP_FAIL, SRP_NEXT, SRP_SETPWD, SRP_LAST, SRP_DONE };

class srp_base {
protected:
  str salt;
  bigint A;
  bigint B;
  srp_hash M;
  srp_hash H;
  static bigint k1;
  static bigint k3;
  bigint *k;

  void setparam (const bigint &NN, const bigint &gg) { N = NN; g = gg; }
  bool checkparam (u_int iter = 32)
    { if (checkparam (N, g, iter)) return true; N = g = 0; return false; }
  bool setS (const bigint &S);

  struct paramcache {
    bigint N;
    u_int iter;
    paramcache () : iter (0) {}
  };
  enum { cachesize = 2 };
  static paramcache cache[cachesize];
  static int lastpos;

public:
  srp_hash sessid;
  str user;
  static u_int minprimsize;
  bigint S;
  bigint N;
  bigint g;

  static bool checkparam (const bigint &N, const bigint &g, u_int iter = 32);
  static bool seedparam (const bigint &N, const bigint &g, u_int iter = 1);
  static void genparam (size_t nbits, bigint *Np, bigint *gp);
};

class srp_client : public srp_base {
  int phase;
  bigint x;
  bigint a;

  srpres phase1a (srpmsg *msgout, const srpmsg *in);
  srpres phase1b (srpmsg *msgout, const srpmsg *in);
  srpres phase3 (srpmsg *msgout, const srpmsg *in);
  srpres phase5 (srpmsg *msgout, const srpmsg *in);

public:
  str pwd;
  str host;
  bool host_ok;
  u_int cost;
  eksblowfish eksb;

  srp_client () : phase (-1), host_ok (false) {}
  void setpwd (const str &p) { pwd = p; }
  str getname () const
    { return strbuf () << user << "@" << host << "/" << N.nbits (); }
  srpres init (srpmsg *msgout, const srp_hash &sessid,
	       str user, str pwd = NULL, int version = 6);
  srpres next (srpmsg *msgout, const srpmsg *in);

  str create (const bigint &N, const bigint &g,
	      str pwd, str host, u_int cost, u_int iter = 32);
};

class srp_server : public srp_base {
  int phase;
  bigint v;
  bigint b;
  bigint u;

  srpres phase2 (srpmsg *msgout, const srpmsg *msgin);
  srpres phase4 (srpmsg *msgout, const srpmsg *msgin);

public:
  srp_server () : phase (-1) {}
  srpres init (srpmsg *msgout, const srpmsg *msgin,
	       const srp_hash &sessid, str user, str info, int version = 6);
  srpres next (srpmsg *msgout, const srpmsg *msgin);

  static bool sane (str info);
};

bool import_srp_params (str raw, bigint *Np, bigint *gp);
str export_srp_params (const bigint &N, const bigint &g);

#endif /* !_SRP_H_  */