-
Notifications
You must be signed in to change notification settings - Fork 0
/
ss.h
136 lines (125 loc) · 2.99 KB
/
ss.h
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
#pragma once
#include <stdio.h>
#include <gmp.h>
#include <stdbool.h>
#include <stdint.h>
//
// Generates the components for a new SS key.
//
// Provides:
// p: first prime
// q: second prime
// n: public modulus/exponent
//
// Requires:
// nbits: minimum # of bits in n
// iters: iterations of Miller-Rabin to use for primality check
// all mpz_t arguments to be initialized
//
void ss_make_pub(mpz_t p, mpz_t q, mpz_t n, uint64_t nbits, uint64_t iters);
//
// Generates components for a new SS private key.
//
// Provides:
// d: private exponent
// pq: private modulus
//
// Requires:
// p: first prime number
// q: second prime number
// all mpz_t arguments to be initialized
//
void ss_make_priv(mpz_t d, mpz_t pq, const mpz_t p, const mpz_t q);
//
// Export SS public key to output stream
//
// Requires:
// n: public modulus/exponent
// username: login name of keyholder ($USER)
// pbfile: open and writable file stream
//
void ss_write_pub(const mpz_t n, const char username[], FILE *pbfile);
//
// Export SS private key to output stream
//
// Requires:
// pq: private modulus
// d: private exponent
// pvfile: open and writable file stream
//
void ss_write_priv(const mpz_t pq, const mpz_t d, FILE *pvfile);
//
// Import SS public key from input stream
//
// Provides:
// n: public modulus
// username: $USER of the pubkey creator
//
// Requires:
// pbfile: open and readable file stream
// username: requires sufficient space
// all mpz_t arguments to be initialized
//
void ss_read_pub(mpz_t n, char username[], FILE *pbfile);
//
// Import SS private key from input stream
//
// Provides:
// pq: private modulus
// d: private exponent
//
// Requires:
// pvfile: open and readable file stream
// all mpz_t arguments to be initialized
//
void ss_read_priv(mpz_t pq, mpz_t d, FILE *pvfile);
//
// Encrypt number m into number c
//
// Provides:
// c: encrypted integer
//
// Requires:
// m: original integer
// n: public exponent/modulus
// all mpz_t arguments to be initialized
//
void ss_encrypt(mpz_t c, const mpz_t m, const mpz_t n);
//
// Encrypt an arbitrary file
//
// Provides:
// fills outfile with the encrypted contents of infile
//
// Requires:
// infile: open and readable file stream
// outfile: open and writable file stream
// n: public exponent and modulus
//
void ss_encrypt_file(FILE *infile, FILE *outfile, const mpz_t n);
//
// Decrypt number c into number m
//
// Provides:
// m: decrypted/original integer
//
// Requires:
// c: encrypted integer
// d: private exponent
// pq: private modulus
// all mpz_t arguments to be initialized
//
void ss_decrypt(mpz_t m, const mpz_t c, const mpz_t d, const mpz_t pq);
//
// Decrypt a file back into its original form.
//
// Provides:
// fills outfile with the unencrypted data from infile
//
// Requires:
// infile: open and readable file stream to encrypted data
// outfile: open and writable file stream
// d: private exponent
// pq: private modulus
//
void ss_decrypt_file(FILE *infile, FILE *outfile, const mpz_t d, const mpz_t pq);