Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / man / man3 / Crypt::CBC.3
CommitLineData
86530b38
AT
1.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "CBC 3"
132.TH CBC 3 "2002-09-11" "perl v5.8.0" "User Contributed Perl Documentation"
133.SH "NAME"
134Crypt::CBC \- Encrypt Data with Cipher Block Chaining Mode
135.SH "SYNOPSIS"
136.IX Header "SYNOPSIS"
137.Vb 8
138\& use Crypt::CBC;
139\& $cipher = Crypt::CBC->new( {'key' => 'my secret key',
140\& 'cipher' => 'Blowfish',
141\& 'iv' => '$KJh#(}q',
142\& 'regenerate_key' => 0, # default true
143\& 'padding' => 'space',
144\& 'prepend_iv' => 0
145\& });
146.Ve
147.PP
148.Vb 2
149\& $ciphertext = $cipher->encrypt("This data is hush hush");
150\& $plaintext = $cipher->decrypt($ciphertext);
151.Ve
152.PP
153.Vb 6
154\& $cipher->start('encrypting');
155\& open(F,"./BIG_FILE");
156\& while (read(F,$buffer,1024)) {
157\& print $cipher->crypt($buffer);
158\& }
159\& print $cipher->finish;
160.Ve
161.SH "DESCRIPTION"
162.IX Header "DESCRIPTION"
163This module is a Perl-only implementation of the cryptographic cipher
164block chaining mode (\s-1CBC\s0). In combination with a block cipher such as
165\&\s-1DES\s0 or \s-1IDEA\s0, you can encrypt and decrypt messages of arbitrarily long
166length. The encrypted messages are compatible with the encryption
167format used by \fBSSLeay\fR.
168.PP
169To use this module, you will first create a Crypt::CBC cipher object with
170\&\fInew()\fR. At the time of cipher creation, you specify an encryption key
171to use and, optionally, a block encryption algorithm. You will then
172call the \fIstart()\fR method to initialize the encryption or decryption
173process, \fIcrypt()\fR to encrypt or decrypt one or more blocks of data, and
174lastly \fIfinish()\fR, to pad and encrypt the final block. For your
175convenience, you can call the \fIencrypt()\fR and \fIdecrypt()\fR methods to
176operate on a whole data value at once.
177.Sh "\fInew()\fP"
178.IX Subsection "new()"
179.Vb 7
180\& $cipher = Crypt::CBC->new( {'key' => 'my secret key',
181\& 'cipher' => 'Blowfish',
182\& 'iv' => '$KJh#(}q',
183\& 'regenerate_key' => 0, # default true
184\& 'padding' => 'space',
185\& 'prepend_iv' => 0
186\& });
187.Ve
188.PP
189.Vb 2
190\& # or (for compatibility with earlier versions)
191\& $cipher = new Crypt::CBC($key,$algorithm);
192.Ve
193.PP
194The \fInew()\fR method creates a new Crypt::CBC object.
195.PP
196You must provide an encryption/decryption key, which can be any series
197of characters of any length. If regenerate_key is not specified as a
198false value, the actual key used is derived from the \s-1MD5\s0 hash of the
199key you provide. The cipher is optional and will default to \s-1DES\s0 unless
200specified otherwise. You may use any compatible block encryption
201algorithm that you have installed. Currently, this includes Crypt::DES,
202Crypt::DES_EDE3, Crypt::IDEA, Crypt::Blowfish, and Crypt::Rijndael. You
203may refer to them using their full names (\*(L"Crypt::IDEA\*(R") or in
204abbreviated form (\*(L"\s-1IDEA\s0\*(R").
205.PP
206An initialization vector may be specified, either by passing in a key of
207\&'iv' as an option to new, or by calling
208\&\f(CW$cipher\fR\->set_initialization_key($iv) before calling \f(CW$cipher\fR\->\fIstart()\fR.
209The \s-1IV\s0 will be ignored in decryption if the ciphertext is prepended by
210text which matches the regex /^RandomIV.{8}/, in which case the 8
211characters following \*(L"RandomIV\*(R" will be used as the \s-1IV\s0. When encrypting,
212by default the ciphertext will be prepended with "RandomIV<\s-1IV\s0>\*(L"
213(16 bytes). To disable this, set 'prepend_iv' to a false value. The
214padding method can be specified by the 'padding' option. If no padding
215method is specified, PKCS#5 (\*(R"standard") padding is assumed.
216.Sh "\fIstart()\fP"
217.IX Subsection "start()"
218.Vb 2
219\& $cipher->start('encrypting');
220\& $cipher->start('decrypting');
221.Ve
222.PP
223The \fIstart()\fR method prepares the cipher for a series of encryption or
224decryption steps, resetting the internal state of the cipher if
225necessary. You must provide a string indicating whether you wish to
226encrypt or decrypt. \*(L"E\*(R" or any word that begins with an \*(L"e\*(R" indicates
227encryption. \*(L"D\*(R" or any word that begins with a \*(L"d\*(R" indicates
228decryption.
229.Sh "\fIcrypt()\fP"
230.IX Subsection "crypt()"
231.Vb 1
232\& $ciphertext = $cipher->crypt($plaintext);
233.Ve
234.PP
235After calling \fIstart()\fR, you should call \fIcrypt()\fR as many times as
236necessary to encrypt the desired data.
237.Sh "\fIfinish()\fP"
238.IX Subsection "finish()"
239.Vb 1
240\& $ciphertext = $cipher->finish();
241.Ve
242.PP
243The \s-1CBC\s0 algorithm must buffer data blocks inernally until they are
244even multiples of the encryption algorithm's blocksize (typically 8
245bytes). After the last call to \fIcrypt()\fR you should call \fIfinish()\fR.
246This flushes the internal buffer and returns any leftover ciphertext.
247.PP
248In a typical application you will read the plaintext from a file or
249input stream and write the result to standard output in a loop that
250might look like this:
251.PP
252.Vb 4
253\& $cipher = new Crypt::CBC('hey jude!');
254\& $cipher->start('encrypting');
255\& print $cipher->crypt($_) while <>;
256\& print $cipher->finish();
257.Ve
258.Sh "\fIencrypt()\fP"
259.IX Subsection "encrypt()"
260.Vb 1
261\& $ciphertext = $cipher->encrypt($plaintext)
262.Ve
263.PP
264This convenience function runs the entire sequence of \fIstart()\fR, \fIcrypt()\fR
265and \fIfinish()\fR for you, processing the provided plaintext and returning
266the corresponding ciphertext.
267.Sh "\fIdecrypt()\fP"
268.IX Subsection "decrypt()"
269.Vb 1
270\& $plaintext = $cipher->decrypt($ciphertext)
271.Ve
272.PP
273This convenience function runs the entire sequence of \fIstart()\fR, \fIcrypt()\fR
274and \fIfinish()\fR for you, processing the provided ciphertext and returning
275the corresponding plaintext.
276.Sh "\fIencrypt_hex()\fP, \fIdecrypt_hex()\fP"
277.IX Subsection "encrypt_hex(), decrypt_hex()"
278.Vb 2
279\& $ciphertext = $cipher->encrypt_hex($plaintext)
280\& $plaintext = $cipher->decrypt_hex($ciphertext)
281.Ve
282.PP
283These are convenience functions that operate on ciphertext in a
284hexadecimal representation. \fBencrypt_hex($plaintext)\fR is exactly
285equivalent to \fBunpack('H*',encrypt($plaintext))\fR. These functions
286can be useful if, for example, you wish to place the encrypted
287.Sh "\fIget_initialization_vector()\fP"
288.IX Subsection "get_initialization_vector()"
289.Vb 1
290\& $iv = $cipher->get_initialization_vector()
291.Ve
292.PP
293This function will return the \s-1IV\s0 used in encryption and or decryption.
294This function may be useful to determine the random \s-1IV\s0 used when
295encrypting if none is specified in \fInew()\fR. The \s-1IV\s0 is not guaranteed to
296be set when encrypting until \fIstart()\fR is called, and when decrypting
297until \fIcrypt()\fR is called the first time.
298.Sh "\fIset_initialization_vector()\fP"
299.IX Subsection "set_initialization_vector()"
300.Vb 1
301\& $cipher->set_initialization_vector('76543210')
302.Ve
303.PP
304This function sets the \s-1IV\s0 used in encryption and/or decryption. This
305function may be useful if the \s-1IV\s0 is not contained within the ciphertext
306string being decrypted, or if a particular \s-1IV\s0 is desired for encryption.
307If not set, a random \s-1IV\s0 will be generated. The \s-1IV\s0 is not guaranteed to
308be set when encrypting until \fIstart()\fR is called, and when decrypting
309until \fIcrypt()\fR is called the first time.
310.Sh "Padding methods"
311.IX Subsection "Padding methods"
312Use the 'padding' option to change the padding method.
313.PP
314When the last block of plaintext is shorter than the block size,
315it must be padded. Padding methods include: \*(L"standard\*(R" (i.e., PKCS#5),
316\&\*(L"oneandzeroes\*(R", \*(L"space\*(R", and \*(L"null\*(R".
317.PP
318.Vb 5
319\& standard: (default) Binary safe
320\& pads with the number of bytes that should be truncated. So, if
321\& blocksize is 8, then "0A0B0C" will be padded with "05", resulting
322\& in "0A0B0C0505050505". If the final block is a full block of 8
323\& bytes, then a whole block of "0808080808080808" is appended.
324.Ve
325.PP
326.Vb 4
327\& oneandzeroes: Binary safe
328\& pads with "80" followed by as many "00" necessary to fill the
329\& block. If the last block is a full block and blocksize is 8, a
330\& block of "8000000000000000" will be appended.
331.Ve
332.PP
333.Vb 4
334\& null: text only
335\& pads with as many "00" necessary to fill the block. If the last
336\& block is a full block and blocksize is 8, a block of
337\& "0000000000000000" will be appended.
338.Ve
339.PP
340.Vb 2
341\& space: text only
342\& same as "null", but with "20".
343.Ve
344.PP
345Both the standard and oneandzeroes paddings are binary safe. The
346space and null paddings are recommended only for text data. Which
347type of padding you use depends on whether you wish to communicate
348with an external (non Crypt::CBC library). If this is the case, use
349whatever padding method is compatible.
350.PP
351You can also pass in a custom padding function. To do this, create a
352function that takes the arguments:
353.PP
354.Vb 1
355\& $padded_block = function($block,$blocksize,$direction);
356.Ve
357.PP
358where \f(CW$block\fR is the current block of data, \f(CW$blocksize\fR is the size to
359pad it to, \f(CW$direction\fR is \*(L"e\*(R" for encrypting and \*(L"d\*(R" for decrypting,
360and \f(CW$padded_block\fR is the result after padding or depadding.
361.PP
362When encrypting, the function should always return a string of
363<blocksize> length, and when decrypting, can expect the string coming
364in to always be that length. See \fI_standard_padding()\fR, \fI_space_padding()\fR,
365\&\fI_null_padding()\fR, or \fI_oneandzeroes_padding()\fR in the source for examples.
366.PP
367Standard and oneandzeroes padding are recommended, as both space and
368null padding can potentially truncate more characters than they should.
369.SH "EXAMPLES"
370.IX Header "EXAMPLES"
371Two examples, des.pl and idea.pl can be found in the eg/ subdirectory
372of the Crypt-CBC distribution. These implement command-line \s-1DES\s0 and
373\&\s-1IDEA\s0 encryption algorithms.
374.SH "LIMITATIONS"
375.IX Header "LIMITATIONS"
376The encryption and decryption process is about a tenth the speed of
377the equivalent SSLeay programs (compiled C). This could be improved
378by implementing this module in C. It may also be worthwhile to
379optimize the \s-1DES\s0 and \s-1IDEA\s0 block algorithms further.
380.SH "BUGS"
381.IX Header "BUGS"
382Please report them.
383.SH "AUTHOR"
384.IX Header "AUTHOR"
385Lincoln Stein, lstein@cshl.org
386.PP
387This module is distributed under the \s-1ARTISTIC\s0 \s-1LICENSE\s0 using the same
388terms as Perl itself.
389.SH "SEE ALSO"
390.IX Header "SEE ALSO"
391\&\fIperl\fR\|(1), \fICrypt::DES\fR\|(3), \fICrypt::IDEA\fR\|(3), rfc2898 (PKCS#5)