File: elgamal.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 (85 lines) | stat: -rw-r--r-- 3,001 bytes parent folder | download
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
// -*-c++-*-
/* $Id: elgamal.h,v 1.1 2006/02/25 02:02:40 mfreed Exp $ */

/*
 *
 * Copyright (C) 2006 Michael J. Freedman (mfreedman at alum.mit.edu)
 *
 * 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 _ELGAMAL_H_
#define _ELGAMAL_H_ 1

#include "homoenc.h"

class elgamal_pub : public virtual homoenc_pub {
public:
  const bigint p;		/* Prime modulus: 2q + 1     */
  const bigint q;		/* Prime modulus             */      
  const bigint g;		/* Random generator of Z^*_p */
  const bigint gr;		/* Generator^r               */
  const size_t nbits, abits;    /* Size of p, r              */
  const bigint p1;
  const bigint q1;

  elgamal_pub (const bigint &pp, const bigint &qq, 
	       const bigint &gg, const bigint &ggr, size_t aabits);

  virtual const size_t &mod_size      () const { return nbits; }
  virtual const bigint &ptext_modulus () const { return q;     }
  virtual const bigint &ctext_modulus () const { return p;     }

  virtual bool encrypt (crypt_ctext *c, const bigint &msg, bool recover) const;
  virtual bool encrypt (crypt_ctext *c, const str &msg,    bool recover) const
  { return homoenc_pub::encrypt (c, msg, recover); }

  virtual crypt_keytype ctext_type () const { return CRYPT_ELGAMAL; }

  // Resulting ciphertext is sum of msgs' corresponding plaintexts
  // ctext = ctext1*ctext2 ==> ptext = ptext1 + ptext2
  virtual void add (crypt_ctext *c, const crypt_ctext &msg1, 
		    const crypt_ctext &msg2) const;

  // Resulting ciphertext is msg's corresponding plaintext * constant
  // ctext = ctext1^const ==> ptext = const * ptext1 
  virtual void mult (crypt_ctext *c, const crypt_ctext &msg, 
		     const bigint &cons) const;

};


class elgamal_priv : public elgamal_pub, public virtual homoenc_priv {
public:
  const bigint r;		/* Random interger \in Z^*_p */
  const bigint i2;              /* 2^-1 mod q                */

  elgamal_priv (const bigint &pp, const bigint &qq, 
		const bigint &gg, const bigint &rr);

  static ptr<elgamal_priv> make (const bigint &p, 
				 const bigint &g, 
				 const bigint &r);
    
  str decrypt (const crypt_ctext &msg, size_t msglen, 
	       bool recover = true) const;
};

// abits gives length of random moduli [e.g., 160 bits for 1024-bit keys]
elgamal_priv elgamal_keygen (size_t nbits, size_t abits, u_int iter = 32);

#endif /* !_ELGAMAL_H_  */