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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
|
.TH crypto 3 "crypto 1.5.2" "Ericsson AB" "ERLANG MODULE DEFINITION"
.SH MODULE
crypto \- Crypto Functions
.SH DESCRIPTION
.LP
This module provides a set of cryptographic functions\&.
.LP
References:
.RS 2
.TP 2
*
md5: The MD5 Message Digest Algorithm (RFC 1321)
.TP 2
*
sha: Secure Hash Standard (FIPS 180-2)
.TP 2
*
hmac: Keyed-Hashing for Message Authentication (RFC 2104)
.TP 2
*
des: Data Encryption Standard (FIPS 46-3)
.TP 2
*
aes: Advanced Encryption Standard (AES) (FIPS 197)
.TP 2
*
ecb, cbc, cfb, ofb: Recommendation for Block Cipher Modes of Operation (NIST SP 800-38A)\&.
.TP 2
*
rsa: Recommendation for Block Cipher Modes of Operation (NIST 800-38A)
.TP 2
*
dss: Digital Signature Standard (FIPS 186-2)
.RE
.LP
The above publications can be found at NIST publications <http://csrc\&.nist\&.gov/publications>, at IETF <http://www\&.ietf\&.org>\&.
.LP
\fITypes\fR
.nf
byte() = 0 \&.\&.\&. 255
ioelem() = byte() | binary() | iolist()
iolist() = [ioelem()]
.fi
.LP
.SH EXPORTS
.LP
.B
start() -> ok
.br
.RS
.LP
Starts the crypto server\&.
.RE
.LP
.B
stop() -> ok
.br
.RS
.LP
Stops the crypto server\&.
.RE
.LP
.B
info() -> [atom()]
.br
.RS
.LP
Provides the available crypto functions in terms of a list of atoms\&.
.RE
.LP
.B
info_lib() -> [{Name,VerNum,VerStr}]
.br
.RS
.TP
Types
Name = binary()
.br
VerNum = integer()
.br
VerStr = binary()
.br
.RE
.RS
.LP
Provides the name and version of the libraries used by crypto\&.
.LP
\fIName\fR is the name of the library\&. \fIVerNum\fR is the numeric version according to the library\&'s own versioning scheme\&. \fIVerStr\fR contains a text variant of the version\&.
.nf
> info_lib()\&.
[{<<"OpenSSL">>,9469983,<<"OpenSSL 0\&.9\&.8a 11 Oct 2005">>}]
.fi
.RE
.LP
.B
md5(Data) -> Digest
.br
.RS
.TP
Types
Data = iolist() | binary()
.br
Digest = binary()
.br
.RE
.RS
.LP
Computes an \fIMD5\fR message digest from \fIData\fR, where the length of the digest is 128 bits (16 bytes)\&.
.RE
.LP
.B
md5_init() -> Context
.br
.RS
.TP
Types
Context = binary()
.br
.RE
.RS
.LP
Creates an MD5 context, to be used in subsequent calls to \fImd5_update/2\fR\&.
.RE
.LP
.B
md5_update(Context, Data) -> NewContext
.br
.RS
.TP
Types
Data = iolist() | binary()
.br
Context = NewContext = binary()
.br
.RE
.RS
.LP
Updates an MD5 \fIContext\fR with \fIData\fR, and returns a \fINewContext\fR\&.
.RE
.LP
.B
md5_final(Context) -> Digest
.br
.RS
.TP
Types
Context = Digest = binary()
.br
.RE
.RS
.LP
Finishes the update of an MD5 \fIContext\fR and returns the computed \fIMD5\fR message digest\&.
.RE
.LP
.B
sha(Data) -> Digest
.br
.RS
.TP
Types
Data = iolist() | binary()
.br
Digest = binary()
.br
.RE
.RS
.LP
Computes an \fISHA\fR message digest from \fIData\fR, where the length of the digest is 160 bits (20 bytes)\&.
.RE
.LP
.B
sha_init() -> Context
.br
.RS
.TP
Types
Context = binary()
.br
.RE
.RS
.LP
Creates an SHA context, to be used in subsequent calls to \fIsha_update/2\fR\&.
.RE
.LP
.B
sha_update(Context, Data) -> NewContext
.br
.RS
.TP
Types
Data = iolist() | binary()
.br
Context = NewContext = binary()
.br
.RE
.RS
.LP
Updates an SHA \fIContext\fR with \fIData\fR, and returns a \fINewContext\fR\&.
.RE
.LP
.B
sha_final(Context) -> Digest
.br
.RS
.TP
Types
Context = Digest = binary()
.br
.RE
.RS
.LP
Finishes the update of an SHA \fIContext\fR and returns the computed \fISHA\fR message digest\&.
.RE
.LP
.B
md5_mac(Key, Data) -> Mac
.br
.RS
.TP
Types
Key = Data = iolist() | binary()
.br
Mac = binary()
.br
.RE
.RS
.LP
Computes an \fIMD5 MAC\fR message authentification code from \fIKey\fR and \fIData\fR, where the the length of the Mac is 128 bits (16 bytes)\&.
.RE
.LP
.B
md5_mac_96(Key, Data) -> Mac
.br
.RS
.TP
Types
Key = Data = iolist() | binary()
.br
Mac = binary()
.br
.RE
.RS
.LP
Computes an \fIMD5 MAC\fR message authentification code from \fIKey\fR and \fIData\fR, where the length of the Mac is 96 bits (12 bytes)\&.
.RE
.LP
.B
sha_mac(Key, Data) -> Mac
.br
.RS
.TP
Types
Key = Data = iolist() | binary()
.br
Mac = binary()
.br
.RE
.RS
.LP
Computes an \fISHA MAC\fR message authentification code from \fIKey\fR and \fIData\fR, where the length of the Mac is 160 bits (20 bytes)\&.
.RE
.LP
.B
sha_mac_96(Key, Data) -> Mac
.br
.RS
.TP
Types
Key = Data = iolist() | binary()
.br
Mac = binary()
.br
.RE
.RS
.LP
Computes an \fISHA MAC\fR message authentification code from \fIKey\fR and \fIData\fR, where the length of the Mac is 96 bits (12 bytes)\&.
.RE
.LP
.B
des_cbc_encrypt(Key, IVec, Text) -> Cipher
.br
.RS
.TP
Types
Key = Text = iolist() | binary()
.br
IVec = Cipher = binary()
.br
.RE
.RS
.LP
Encrypts \fIText\fR according to DES in CBC mode\&. \fIText\fR must be a multiple of 64 bits (8 bytes)\&. \fIKey\fR is the DES key, and \fIIVec\fR is an arbitrary initializing vector\&. The lengths of \fIKey\fR and \fIIVec\fR must be 64 bits (8 bytes)\&.
.RE
.LP
.B
des_cbc_decrypt(Key, IVec, Cipher) -> Text
.br
.RS
.TP
Types
Key = Cipher = iolist() | binary()
.br
IVec = Text = binary()
.br
.RE
.RS
.LP
Decrypts \fICipher\fR according to DES in CBC mode\&. \fIKey\fR is the DES key, and \fIIVec\fR is an arbitrary initializing vector\&. \fIKey\fR and \fIIVec\fR must have the same values as those used when encrypting\&. \fICipher\fR must be a multiple of 64 bits (8 bytes)\&. The lengths of \fIKey\fR and \fIIVec\fR must be 64 bits (8 bytes)\&.
.RE
.LP
.B
des3_cbc_encrypt(Key1, Key2, Key3, IVec, Text) -> Cipher
.br
.RS
.TP
Types
Key1 =Key2 = Key3 Text = iolist() | binary()
.br
IVec = Cipher = binary()
.br
.RE
.RS
.LP
Encrypts \fIText\fR according to DES3 in CBC mode\&. \fIText\fR must be a multiple of 64 bits (8 bytes)\&. \fIKey1\fR, \fIKey2\fR, \fIKey3\fR, are the DES keys, and \fIIVec\fR is an arbitrary initializing vector\&. The lengths of each of \fIKey1\fR, \fIKey2\fR, \fIKey3\fR and \fIIVec\fR must be 64 bits (8 bytes)\&.
.RE
.LP
.B
des3_cbc_decrypt(Key1, Key2, Key3, IVec, Cipher) -> Text
.br
.RS
.TP
Types
Key1 = Key2 = Key3 = Cipher = iolist() | binary()
.br
IVec = Text = binary()
.br
.RE
.RS
.LP
Decrypts \fICipher\fR according to DES3 in CBC mode\&. \fIKey1\fR, \fIKey2\fR, \fIKey3\fR are the DES key, and \fIIVec\fR is an arbitrary initializing vector\&. \fIKey1\fR, \fIKey2\fR, \fIKey3\fR and \fIIVec\fR must and \fIIVec\fR must have the same values as those used when encrypting\&. \fICipher\fR must be a multiple of 64 bits (8 bytes)\&. The lengths of \fIKey1\fR, \fIKey2\fR, \fIKey3\fR, and \fIIVec\fR must be 64 bits (8 bytes)\&.
.RE
.LP
.B
aes_cfb_128_encrypt(Key, IVec, Text) -> Cipher
.br
.B
aes_cbc_128_encrypt(Key, IVec, Text) -> Cipher
.br
.RS
.TP
Types
Key = Text = iolist() | binary()
.br
IVec = Cipher = binary()
.br
.RE
.RS
.LP
Encrypts \fIText\fR according to AES in Cipher Feedback mode (CFB) or Cipher Block Chaining mode (CBC)\&. \fIText\fR must be a multiple of 128 bits (16 bytes)\&. \fIKey\fR is the AES key, and \fIIVec\fR is an arbitrary initializing vector\&. The lengths of \fIKey\fR and \fIIVec\fR must be 128 bits (16 bytes)\&.
.RE
.LP
.B
aes_cfb_128_decrypt(Key, IVec, Cipher) -> Text
.br
.B
aes_cbc_128_decrypt(Key, IVec, Cipher) -> Text
.br
.RS
.TP
Types
Key = Cipher = iolist() | binary()
.br
IVec = Text = binary()
.br
.RE
.RS
.LP
Decrypts \fICipher\fR according to Cipher Feedback Mode (CFB) or Cipher Block Chaining mode (CBC)\&. \fIKey\fR is the AES key, and \fIIVec\fR is an arbitrary initializing vector\&. \fIKey\fR and \fIIVec\fR must have the same values as those used when encrypting\&. \fICipher\fR must be a multiple of 128 bits (16 bytes)\&. The lengths of \fIKey\fR and \fIIVec\fR must be 128 bits (16 bytes)\&.
.RE
.LP
.B
erlint(Mpint) ->
.br
.B
mpint(N) -> Mpint
.br
.RS
.TP
Types
Mpint = binary()
.br
N = integer()
.br
.RE
.RS
.LP
Convert a binary multi-precision integer \fIMpint\fR to and from an erlang big integer\&. A multi-precision integer is a binary with the following form: \fI<<ByteLen:32/integer, Bytes:ByteLen/binary>>\fR where both \fIByteLen\fR and \fIBytes\fR are big-endian\&. Mpints are used in some of the functions in \fIcrypto\fR and are not translated in the API for performance reasons\&.
.RE
.LP
.B
rand_bytes(N) -> binary()
.br
.RS
.TP
Types
N = integer()
.br
.RE
.RS
.LP
Generates N bytes randomly uniform 0\&.\&.255, and returns the result in a binary\&. Uses the \fIcrypto\fR library pseudo-random number generator\&.
.RE
.LP
.B
rand_uniform(Lo, Hi) -> N
.br
.RS
.TP
Types
Lo, Hi, N = Mpint | integer()
.br
Mpint = binary()
.br
.RE
.RS
.LP
Generate a random number \fIN, Lo =< N < Hi\&.\fR Uses the \fIcrypto\fR library pseudo-random number generator\&. The arguments (and result) can be either erlang integers or binary multi-precision integers\&.
.RE
.LP
.B
mod_exp(N, P, M) -> Result
.br
.RS
.TP
Types
N, P, M, Result = Mpint
.br
Mpint = binary()
.br
.RE
.RS
.LP
This function performs the exponentiation \fIN ^ P mod M\fR, using the \fIcrypto\fR library\&.
.RE
.LP
.B
rsa_verify(Digest, Signature, Key) -> Verified
.br
.RS
.TP
Types
Verified = boolean()
.br
Digest, Signature = MPint
.br
Key = [E, N]
.br
E, N = MPint
.br
MPint = binary()
.br
.RE
.RS
.LP
Verifies the digest and signature using the public key \fIKey\fR, using the \fIcrypto\fR library function for RSA signature verification\&.
.RE
.LP
.B
dss_verify(Digest, Signature, Key) -> Verified
.br
.RS
.TP
Types
Verified = boolean()
.br
Digest, Signature = MPint
.br
Key = [P, Q, G, Y]
.br
P, Q, G, Y = MPint
.br
MPint = binary()
.br
.RE
.RS
.LP
Verifies the digest and signature using the public key \fIKey\fR, using the \fIcrypto\fR library function for DSS signature verification\&.
.RE
.LP
.B
rc4_encrypt(Key, Data) -> Result
.br
.RS
.TP
Types
Key, Data = iolist() | binary()
.br
Result = binary()
.br
.RE
.RS
.LP
Encrypts the data with RC4 symmetric stream encryption\&. Since it is symmetric, the same function is used for decryption\&.
.RE
.LP
.B
exor(Data1, Data2) -> Result
.br
.RS
.TP
Types
Data1, Data2 = iolist() | binary()
.br
Result = binary()
.br
.RE
.RS
.LP
Performs bit-wise XOR (exclusive or) on the data supplied\&.
.RE
.SH DES IN CBC MODE
.LP
The Data Encryption Standard (DES) defines an algoritm for encrypting and decrypting an 8 byte quantity using an 8 byte key (actually only 56 bits of the key is used)\&.
.LP
When it comes to encrypting and decrypting blocks that are multiples of 8 bytes various modes are defined (NIST SP 800-38A)\&. One of those modes is the Cipher Block Chaining (CBC) mode, where the encryption of an 8 byte segment depend not only of the contents of the segment itself, but also on the result of encrypting the previous segment: the encryption of the previous segment becomes the initializing vector of the encryption of the current segment\&.
.LP
Thus the encryption of every segment depends on the encryption key (which is secret) and the encryption of the previous segment, except the first segment which has to be provided with a first initializing vector\&. That vector could be chosen at random, or be counter of some kind\&. It does not have to be secret\&.
.LP
The following example is drawn from the old FIPS 81 standard (replaced by NIST SP 800-38A), where both the plain text and the resulting cipher text is settled\&. We use the Erlang bitsyntax to define binary literals\&. The following Erlang code fragment returns `true\&'\&.
.nf
Key = <<16#01,16#23,16#45,16#67,16#89,16#ab,16#cd,16#ef>>,
IVec = <<16#12,16#34,16#56,16#78,16#90,16#ab,16#cd,16#ef>>,
P = "Now is the time for all ",
C = crypto:des_cbc_encrypt(K, I, P),
C == <<16#e5,16#c7,16#cd,16#de,16#87,16#2b,16#f2,16#7c,
16#43,16#e9,16#34,16#00,16#8c,16#38,16#9c,16#0f,
16#68,16#37,16#88,16#49,16#9a,16#7c,16#05,16#f6>>,
<<"Now is the time for all ">> ==
crypto:des_cbc_decrypt(Key,IVec,C)\&.
.fi
.LP
The following is true for the DES CBC mode\&. For all decompositions \fIP1 ++ P2 = P\fR of a plain text message \fIP\fR (where the length of all quantities are multiples of 8 bytes), the encryption \fIC\fR of \fIP\fR is equal to \fIC1 ++ C2\fR, where \fIC1\fR is obtained by encrypting \fIP1\fR with \fIKey\fR and the initializing vector \fIIVec\fR, and where \fIC2\fR is obtained by encrypting \fIP2\fR with \fIKey\fR and the initializing vector \fIl(C1)\fR, where \fIl(B)\fR denotes the last 8 bytes of the binary \fIB\fR\&.
.LP
Similarly, for all decompositions \fIC1 ++ C2 = C\fR of a cipher text message \fIC\fR (where the length of all quantities are multiples of 8 bytes), the decryption \fIP\fR of \fIC\fR is equal to \fIP1 ++ P2\fR, where \fIP1\fR is obtained by decrypting \fIC1\fR with \fIKey\fR and the initializing vector \fIIVec\fR, and where \fIP2\fR is obtained by decrypting \fIC2\fR with \fIKey\fR and the initializing vector \fIl(C1)\fR, where \fIl(\&.)\fR is as above\&.
.LP
For DES3 (which uses three 64 bit keys) the situation is the same\&.
|