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
  
#pike __REAL_VERSION__ 
#pragma strict_types 
 
//! Base class for cipher algorithms. 
//! 
//! Implements some common convenience functions, and prototypes. 
//! 
//! Note that no actual cipher algorithm is implemented 
//! in the base class. They are implemented in classes 
//! that inherit (usually via @[predef::Nettle.Cipher]) this class. 
//! 
//! @seealso 
//!   @[predef::Nettle.Cipher], @[Crypto.Cipher] 
 
//! Returns a human readable name for the algorithm. 
string(7bit) name(); 
 
//! @returns 
//!   The recommended key size for the cipher. 
int(0..) key_size(); 
 
//! @returns 
//!   The block size of the cipher (@expr{1@} for stream ciphers). 
int(0..) block_size(); 
 
//! This is the context for a single incrementally updated cipher. 
//! 
//! Most of the functions here are only prototypes, and need to 
//! be overrided via inherit. 
class State 
{ 
  //! Initializes the object for encryption. 
  //! 
  //! @seealso 
  //!   @[set_decrypt_key], @[crypt] 
  this_program set_encrypt_key(string(8bit) key, void|int force); 
 
  //! Initializes the object for decryption. 
  //! 
  //! @seealso 
  //!   @[set_encrypt_key], @[crypt] 
  this_program set_decrypt_key(string(8bit) key, void|int force); 
 
  //! Generate a key by calling @[random_string] and initialize the 
  //! object for encryption with that key. 
  //! 
  //! @returns 
  //!   The generated key. 
  //! 
  //! @seealso 
  //!   @[set_encrypt_key] 
  string(8bit) make_key() 
  { 
    string(8bit) key = random_string(global::key_size()); 
    set_encrypt_key(key); 
    return key; 
  } 
 
  //! Encrypts or decrypts data, using the current key. Neither the 
  //! input nor output data is automatically memory scrubbed, 
  //! unless @[String.secure] has been called on them. 
  //! 
  //! @param data 
  //!   For block ciphers, data must be an integral number of blocks. 
  //! 
  //! @returns 
  //!   The encrypted or decrypted data. 
  string(8bit) crypt(string(8bit) data); 
 
  //! @returns 
  //!   The actual key size for this cipher. 
  //! 
  //! Defaults to just returning @expr{global::key_size()@}. 
  int(0..) key_size() 
  { 
    return global::key_size(); 
  } 
 
  //! Returns a human readable name for the algorithm. 
  //! 
  //! Defaults to just returning @expr{global::name()@}. 
  string(7bit) name() 
  { 
    return global::name(); 
  } 
 
  //! @returns 
  //!   The block size of the cipher (@expr{1@} for stream ciphers). 
  //! 
  //! Defaults to just returning @expr{global::block_size()@}. 
  int(0..) block_size() 
  { 
    return global::block_size(); 
  } 
} 
 
//! Calling `() will return a @[State] object. 
State `()() { return State(); } 
 
//! Works as a shortcut for @expr{obj->set_encrypt_key(key)->crypt(data)@} 
string encrypt(string(8bit) key, string(8bit) data) { 
  return `()()->set_encrypt_key(key)->crypt(data); 
} 
 
//! Works as a shortcut for @expr{obj->set_decrypt_key(key)->crypt(data)@} 
string decrypt(string(8bit) key, string(8bit) data) { 
  return `()()->set_decrypt_key(key)->crypt(data); 
}