Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
N
nettle
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Marcus Hoffmann
nettle
Commits
34cf8e58
Commit
34cf8e58
authored
23 years ago
by
Niels Möller
Browse files
Options
Downloads
Patches
Plain Diff
(Cipher Block Chaining): This section more or less complete now.
Rev: src/nettle/nettle.texinfo:1.5
parent
c6d08cf4
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
nettle.texinfo
+56
-17
56 additions, 17 deletions
nettle.texinfo
with
56 additions
and
17 deletions
nettle.texinfo
+
56
−
17
View file @
34cf8e58
...
...
@@ -296,6 +296,7 @@ This chapter describes all the Nettle functions, grouped by family.
@menu
* Hash functions::
* Cipher functions::
* Cipher Block Chaining::
* Miscellaneous functions::
@end menu
...
...
@@ -412,7 +413,7 @@ of the digest are written.
This functions doesn't change the state in any way.
@end deftypefun
@node Cipher functions,
Miscellaneous functions
, Hash functions, Reference
@node Cipher functions,
Cipher Block Chaining
, Hash functions, Reference
@comment node-name, next, previous, up
@section Cipher functions
...
...
@@ -441,7 +442,8 @@ However, using ECB is usually a bad idea. For a start, plaintext blocks
that are equal are transformed to ciphertext blocks that are equal; that
leaks information about the plaintext. Usually you should apply the
cipher is some feedback mode, @dfn
{
CBC
}
(Cipher Block Chaining) being one
of the most popular. XXX Add reference
of the most popular. @xref
{
Cipher Block Chaining
}
, for information on
how to apply CBC with Nettle.
A stream cipher can be used for messages of arbitrary length; a typical
stream cipher is a keyed pseudorandom generator. To encrypt a plaintext
...
...
@@ -798,10 +800,33 @@ in any other way.
Analogous to @code
{
twofish
_
encrypt
}
@end deftypefun
@node C
BC
@node C
ipher Block Chaining, Miscellaneous functions, Cipher functions, Reference
@comment node-name, next, previous, up
@section Cipher Block Chaining
When using CBC mode, cleartext blocks are not encrypted independently of
each other, like in Electronic Cookbook mode. Instead, when encrypting a
block in CBC mode, the previous ciphertext block is XOR:ed with the
cleartext before it is fed to the block cipher. When encrypting the
first block, a random block called an @dfn
{
IV
}
, or Initialization
Vector, is used as the ``previous ciphertext block''. The IV should be
chosen randomly, but it need not be kept secret, and can even be
transmitted in the clear together with the encrypted data.
In symbols, if @code
{
E
_
k
}
is the encryption function of a blockcipher,
and @code
{
IV
}
is the initialization vector, then @code
{
n
}
cleartext blocks
@code
{
M
_
1
}
,@dots
{}
@code
{
M
_
n
}
are transformed into @code
{
n
}
ciphertext blocks
@code
{
C
_
1
}
,@dots
{}
@code
{
C
_
n
}
as follows:
@example
C
_
1 = E
_
k(IV XOR M
_
1)
C
_
2 = E
_
k(C
_
1 XOR M
_
2)
@dots
{}
C
_
n = E
_
k(C
_
(n-1) XOR M
_
n)
@end example
Nettle includes a few utility functions for applying a block cipher in
Cipher Block Chaining (CBC) mode. The functions uses @code
{
void *
}
to
pass cipher contexts around.
...
...
@@ -810,15 +835,18 @@ pass cipher contexts around.
@deftypefunx
{
void
}
cbc
_
decrypt (void *@var
{
ctx
}
, void (*@var
{
f
}
)(), unsigned @var
{
block
_
size
}
, uint8
_
t *@var
{
iv
}
, unsigned @var
{
length
}
, uint8
_
t *@var
{
dst
}
, const uint8
_
t *@var
{
src
}
)
Applies the encryption or decryption function @var
{
f
}
in CBC mde. The
function f is really typed as @code
{
void f (void *@var
{
ctx
}
, unsigned
@var
{
length
}
, uint8
_
t @var
{
dst
}
, const uint8
_
t *@var
{
src
}
), and the
@code
{
cbc
_
encrypt
}
and @code
{
cbc
_
decrypt
}
functions pass their argument
@var
{
ctx
}
on to @code
{
f
}
.
function @var
{
f
}
is really typed as
@code
{
void f (void *@var
{
ctx
}
, unsigned @var
{
length
}
, uint8
_
t @var
{
dst
}
,
const uint8
_
t *@var
{
src
}
)
}
,
@noindent and the @code
{
cbc
_
encrypt
}
and @code
{
cbc
_
decrypt
}
functions pass their
argument @var
{
ctx
}
on to @var
{
f
}
.
@end deftypefun
There are also some macros to help use these functions correctly. The
are best explained by example.
There are also some macros to help use these functions correctly.
@deffn Macro CBC
_
CTX (@var
(
context
_
type
)
, @var
(
block
_
size
)
)
@deffn Macro CBC
_
CTX (@var
{
context
_
type
}
, @var
{
block
_
size
}
)
Expands into
@example
@
{
...
...
@@ -826,7 +854,10 @@ Expands into
uint8
_
t iv[block
_
size];
@
}
@end example
It can be used to define a CBC context stuct, either directly
@end deffn
It can be used to define a CBC context stuct, either directly,
@example
struct CBC
_
CTX(struct aes
_
ctx, AES
_
BLOCK
_
SIZE) ctx;
@end example
...
...
@@ -837,19 +868,27 @@ or to give it a struct tag,
struct aes
_
cbc
_
ctx CBC
_
CTX (struct aes
_
ctx, AES
_
BLOCK
_
SIZE);
@end example
@deffn Macro CBC
_
SET
_
KEY
(@var
{
ctx
}
, @var
{
iv
}
)
@deffn Macro CBC
_
SET
_
IV
(@var
{
ctx
}
, @var
{
iv
}
)
First argument is a pointer to a context struct as defined by @code
{
CBC
_
CTX
}
,
and the second is a pointer to an Initialization Vector (iv) that is
copied into the context.
and the second is a pointer to an Initialization Vector (IV) that is
copied into that context.
@end deffn
@deffn Macro CBC
_
ENCRYPT (@var
{
ctx
}
, @var
{
f
}
, @var
{
length
}
, @var
{
dst
}
, @var
{
src
}
)
@deffnx Macro CBC
_
DECRYPT (@var
{
ctx
}
, @var
{
f
}
, @var
{
length
}
, @var
{
dst
}
, @var
{
src
}
)
A simpler way to invoke @code
{
cbc
_
encrypt
}
and @code
{
cbc
_
decrypt
}
. First
argument is XXX Here
A simpler way to invoke @code
{
cbc
_
encrypt
}
and @code
{
cbc
_
decrypt
}
. The first
argument is a context struct as defined by @code
{
CBC
_
CTX
}
, the second
argument is an encryption or decryption function following Nettle's
conventions. The last three arguments define the source and destination
area for the operation.
@end deffn
These macros use some tricks to make the compiler display a warning if
the types of @var
{
f
}
and @var
{
ctx
}
don't match, e.g. if you try to use
an @code
{
struct aes
_
ctx
}
context with the @code
{
des
_
encrypt
}
function.
@node Miscellaneous functions, , Cipher
functions
, Reference
@node Miscellaneous functions, , Cipher
Block Chaining
, Reference
@comment node-name, next, previous, up
@section Miscellaneous functions
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment