File: homoenc.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 (102 lines) | stat: -rw-r--r-- 2,998 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// -*-c++-*-
/* $Id: homoenc.h,v 1.1 2006/02/25 02:02:39 mfreed Exp $ */

/*
 *
 * Copyright (C) 2005 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 _HOMOENC_H_
#define _HOMOENC_H_ 1

#include "crypt_prot.h"
#include "crypt.h"

class homoenc_pub {
public:
  homoenc_pub () {}
  virtual ~homoenc_pub () {}

  virtual const size_t &mod_size     () const = 0;
  virtual const bigint &ptext_modulus () const = 0;
  virtual const bigint &ctext_modulus () const = 0;

  virtual bool encrypt (crypt_ctext *c, const bigint &msg, 
			bool recover) const = 0;
  virtual bool encrypt (crypt_ctext *c, const str &msg, bool recover) const {
    bigint m = pre_encrypt (msg);
    if (!m) return false;
    return encrypt (c, m, recover);
  }

  virtual crypt_keytype ctext_type () const = 0;

  // 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 = 0;

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

  virtual bigint pre_encrypt  (const str &msg) const {
    size_t nbits = mod_size ();
    if (msg.len () > nbits) {
      warn << "pre_encrypt: message too large [len " 
	   << msg.len () << " bits " << nbits << "]\n";
      return 0;
    }
    
    bigint r;
    mpz_set_rawmag_le (&r, msg.cstr (), msg.len ());
    return r;
    
  }

  virtual str post_decrypt (const bigint &msg, size_t msglen) const {
    size_t nbits = mod_size ();
    if (msg.nbits () > nbits || msglen > nbits) {
      warn << "post_decrypt: message too large [len " << msg.nbits ()
	   << " buf " << msglen << " bits " << nbits << "]\n";
      return NULL;
    }
    
    zcbuf zm (nbits);
    mpz_get_rawmag_le (zm, zm.size, &msg);
  
    char *mp = zm;
    wmstr r (msglen);
    memcpy (r, mp, msglen);
    return r;
  }
};


class homoenc_priv : public virtual homoenc_pub {
public:
  homoenc_priv () {}
  virtual ~homoenc_priv () {}
  virtual str decrypt (const crypt_ctext &msg, size_t msglen, 
		       bool recover = true) const = 0;
};


#endif /* !_HOMOENC_H_  */