.. hazmat:: Key Derivation Functions ======================== .. currentmodule:: cryptography.hazmat.primitives.kdf Key derivation functions derive bytes suitable for cryptographic operations from passwords or other data sources using a pseudo-random function (PRF). Different KDFs are suitable for different tasks such as: * Cryptographic key derivation Deriving a key suitable for use as input to an encryption algorithm. Typically this means taking a password and running it through an algorithm such as :class:`~cryptography.hazmat.primitives.kdf.pbkdf2.PBKDF2HMAC` or :class:`~cryptography.hazmat.primitives.kdf.hkdf.HKDF`. This process is typically known as `key stretching`_. * Password storage When storing passwords you want to use an algorithm that is computationally intensive. Legitimate users will only need to compute it once (for example, taking the user's password, running it through the KDF, then comparing it to the stored value), while attackers will need to do it billions of times. Ideal password storage KDFs will be demanding on both computational and memory resources. .. currentmodule:: cryptography.hazmat.primitives.kdf.pbkdf2 .. class:: PBKDF2HMAC(algorithm, length, salt, iterations, backend) .. versionadded:: 0.2 `PBKDF2`_ (Password Based Key Derivation Function 2) is typically used for deriving a cryptographic key from a password. It may also be used for key storage, but an alternate key storage KDF such as `scrypt`_ is generally considered a better solution. This class conforms to the :class:`~cryptography.hazmat.primitives.interfaces.KeyDerivationFunction` interface. .. doctest:: >>> import os >>> from cryptography.hazmat.primitives import hashes >>> from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC >>> from cryptography.hazmat.backends import default_backend >>> backend = default_backend() >>> salt = os.urandom(16) >>> # derive >>> kdf = PBKDF2HMAC( ... algorithm=hashes.SHA256(), ... length=32, ... salt=salt, ... iterations=100000, ... backend=backend ... ) >>> key = kdf.derive(b"my great password") >>> # verify >>> kdf = PBKDF2HMAC( ... algorithm=hashes.SHA256(), ... length=32, ... salt=salt, ... iterations=100000, ... backend=backend ... ) >>> kdf.verify(b"my great password", key) :param algorithm: An instance of a :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` provider. :param int length: The desired length of the derived key. Maximum is (2\ :sup:`32` - 1) * ``algorithm.digest_size``. :param bytes salt: A salt. `NIST SP 800-132`_ recommends 128-bits or longer. :param int iterations: The number of iterations to perform of the hash function. This can be used to control the length of time the operation takes. Higher numbers help mitigate brute force attacks against derived keys. See OWASP's `Password Storage Cheat Sheet`_ for more detailed recommendations if you intend to use this for password storage. :param backend: A :class:`~cryptography.hazmat.backends.interfaces.PBKDF2HMACBackend` provider. .. method:: derive(key_material) :param bytes key_material: The input key material. For PBKDF2 this should be a password. :return bytes: the derived key. :raises cryptography.exceptions.AlreadyFinalized: This is raised when :meth:`derive` or :meth:`verify` is called more than once. This generates and returns a new key from the supplied password. .. method:: verify(key_material, expected_key) :param bytes key_material: The input key material. This is the same as ``key_material`` in :meth:`derive`. :param bytes expected_key: The expected result of deriving a new key, this is the same as the return value of :meth:`derive`. :raises cryptography.exceptions.InvalidKey: This is raised when the derived key does not match the expected key. :raises cryptography.exceptions.AlreadyFinalized: This is raised when :meth:`derive` or :meth:`verify` is called more than once. This checks whether deriving a new key from the supplied ``key_material`` generates the same key as the ``expected_key``, and raises an exception if they do not match. This can be used for checking whether the password a user provides matches the stored derived key. .. currentmodule:: cryptography.hazmat.primitives.kdf.hkdf .. class:: HKDF(algorithm, length, salt, info, backend) .. versionadded:: 0.2 `HKDF`_ (HMAC-based Extract-and-Expand Key Derivation Function) is suitable for deriving keys of a fixed size used for other cryptographic operations. .. warning:: HKDF should not be used for password storage. .. doctest:: >>> import os >>> from cryptography.hazmat.primitives import hashes >>> from cryptography.hazmat.primitives.kdf.hkdf import HKDF >>> from cryptography.hazmat.backends import default_backend >>> backend = default_backend() >>> salt = os.urandom(16) >>> info = b"hkdf-example" >>> hkdf = HKDF( ... algorithm=hashes.SHA256(), ... length=32, ... salt=salt, ... info=info, ... backend=backend ... ) >>> key = hkdf.derive(b"input key") >>> hkdf = HKDF( ... algorithm=hashes.SHA256(), ... length=32, ... salt=salt, ... info=info, ... backend=backend ... ) >>> hkdf.verify(b"input key", key) :param algorithm: An instance of a :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` provider. :param int length: The desired length of the derived key. Maximum is ``255 * (algorithm.digest_size // 8)``. :param bytes salt: A salt. Randomizes the KDF's output. Optional, but highly recommended. Ideally as many bits of entropy as the security level of the hash: often that means cryptographically random and as long as the hash output. Worse (shorter, less entropy) salt values can still meaningfully contribute to security. May be reused. Does not have to be secret, but may cause stronger security guarantees if secret; see `RFC 5869`_ and the `HKDF paper`_ for more details. If ``None`` is explicitly passed a default salt of ``algorithm.digest_size // 8`` null bytes will be used. :param bytes info: Application specific context information. If ``None`` is explicitly passed an empty byte string will be used. :params backend: A :class:`~cryptography.hazmat.backends.interfaces.HMACBackend` provider. .. method:: derive(key_material) :param bytes key_material: The input key material. :retunr bytes: The derived key. Derives a new key from the input key material by performing both the extract and expand operations. .. method:: verify(key_material, expected_key) :param key_material bytes: The input key material. This is the same as ``key_material`` in :meth:`derive`. :param expected_key bytes: The expected result of deriving a new key, this is the same as the return value of :meth:`derive`. :raises cryptography.exceptions.InvalidKey: This is raised when the derived key does not match the expected key. :raises cryptography.exceptions.AlreadyFinalized: This is raised when :meth:`derive` or :meth:`verify` is called more than once. This checks whether deriving a new key from the supplied ``key_material`` generates the same key as the ``expected_key``, and raises an exception if they do not match. .. _`NIST SP 800-132`: http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf .. _`Password Storage Cheat Sheet`: https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet .. _`PBKDF2`: https://en.wikipedia.org/wiki/PBKDF2 .. _`scrypt`: https://en.wikipedia.org/wiki/Scrypt .. _`key stretching`: https://en.wikipedia.org/wiki/Key_stretching .. _`HKDF`: .. _`RFC 5869`: https://tools.ietf.org/html/rfc5869 .. _`HKDF paper`: https://eprint.iacr.org/2010/264 '>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 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
-- Well known name table entries.
-- Copyright (C) 2002, 2003, 2004, 2005 Tristan Gingold
--
-- GHDL is free software; you can redistribute it and/or modify it under
-- the terms of the GNU General Public License as published by the Free
-- Software Foundation; either version 2, or (at your option) any later
-- version.
--
-- GHDL is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or
-- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
-- for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with GHDL; see the file COPYING. If not, write to the Free
-- Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-- 02111-1307, USA.
with Name_Table;
with Ada.Exceptions;
package body Std_Names is
procedure Std_Names_Initialize is
procedure Def (S : String; Id : Name_Id) is
begin
if Name_Table.Get_Identifier (S) /= Id then
Ada.Exceptions.Raise_Exception
(Program_Error'Identity, "wrong name_id for " & S);
end if;
end Def;
begin
Def ("mod", Name_Mod);
Def ("rem", Name_Rem);
Def ("and", Name_And);
Def ("or", Name_Or);
Def ("xor", Name_Xor);
Def ("nand", Name_Nand);
Def ("nor", Name_Nor);
Def ("abs", Name_Abs);
Def ("not", Name_Not);
Def ("access", Name_Access);
Def ("after", Name_After);
Def ("alias", Name_Alias);
Def ("all", Name_All);
Def ("architecture", Name_Architecture);
Def ("array", Name_Array);
Def ("assert", Name_Assert);
Def ("attribute", Name_Attribute);
Def ("begin", Name_Begin);
Def ("block", Name_Block);
Def ("body", Name_Body);
Def ("buffer", Name_Buffer);
Def ("bus", Name_Bus);
Def ("case", Name_Case);
Def ("component", Name_Component);
Def ("configuration", Name_Configuration);
Def ("constant", Name_Constant);
Def ("disconnect", Name_Disconnect);
Def ("downto", Name_Downto);
Def ("else", Name_Else);
Def ("elsif", Name_Elsif);
Def ("end", Name_End);
Def ("entity", Name_Entity);
Def ("exit", Name_Exit);
Def ("file", Name_File);
Def ("for", Name_For);
Def ("function", Name_Function);
Def ("generate", Name_Generate);
Def ("generic", Name_Generic);
Def ("guarded", Name_Guarded);
Def ("if", Name_If);
Def ("in", Name_In);
Def ("inout", Name_Inout);
Def ("is", Name_Is);
Def ("label", Name_Label);
Def ("library", Name_Library);
Def ("linkage", Name_Linkage);
Def ("loop", Name_Loop);
Def ("map", Name_Map);
Def ("new", Name_New);
Def ("next", Name_Next);
Def ("null", Name_Null);
Def ("of", Name_Of);
Def ("on", Name_On);
Def ("open", Name_Open);
Def ("others", Name_Others);
Def ("out", Name_Out);
Def ("package", Name_Package);
Def ("port", Name_Port);
Def ("procedure", Name_Procedure);
Def ("process", Name_Process);
Def ("range", Name_Range);
Def ("record", Name_Record);
Def ("register", Name_Register);
Def ("report", Name_Report);
Def ("return", Name_Return);
Def ("select", Name_Select);
Def ("severity", Name_Severity);
Def ("signal", Name_Signal);
Def ("subtype", Name_Subtype);
Def ("then", Name_Then);
Def ("to", Name_To);
Def ("transport", Name_Transport);
Def ("type", Name_Type);
Def ("units", Name_Units);
Def ("until", Name_Until);
Def ("use", Name_Use);
Def ("variable", Name_Variable);
Def ("wait", Name_Wait);
Def ("when", Name_When);
Def ("while", Name_While);
Def ("with", Name_With);
-- VHDL93 reserved words.
Def ("xnor", Name_Xnor);
Def ("group", Name_Group);
Def ("impure", Name_Impure);
Def ("inertial", Name_Inertial);
Def ("literal", Name_Literal);
Def ("postponed", Name_Postponed);
Def ("pure", Name_Pure);
Def ("reject", Name_Reject);
Def ("shared", Name_Shared);
Def ("unaffected", Name_Unaffected);
Def ("sll", Name_Sll);
Def ("sla", Name_Sla);
Def ("sra", Name_Sra);
Def ("srl", Name_Srl);
Def ("rol", Name_Rol);
Def ("ror", Name_Ror);
Def ("protected", Name_Protected);
Def ("context", Name_Context);
Def ("parameter", Name_Parameter);
Def ("across", Name_Across);
Def ("break", Name_Break);
Def ("limit", Name_Limit);
Def ("nature", Name_Nature);
Def ("noise", Name_Noise);
Def ("procedural", Name_Procedural);
Def ("quantity", Name_Quantity);
Def ("reference", Name_Reference);
Def ("spectrum", Name_Spectrum);
Def ("subnature", Name_Subnature);
Def ("terminal", Name_Terminal);
Def ("through", Name_Through);
Def ("tolerance", Name_Tolerance);
-- Create operators.
Def ("=", Name_Op_Equality);
Def ("/=", Name_Op_Inequality);
Def ("<", Name_Op_Less);
Def ("<=", Name_Op_Less_Equal);
Def (">", Name_Op_Greater);
Def (">=", Name_Op_Greater_Equal);
Def ("+", Name_Op_Plus);
Def ("-", Name_Op_Minus);
Def ("*", Name_Op_Mul);
Def ("/", Name_Op_Div);
Def ("**", Name_Op_Exp);
Def ("&", Name_Op_Concatenation);
Def ("??", Name_Op_Condition);
Def ("?=", Name_Op_Match_Equality);
Def ("?/=", Name_Op_Match_Inequality);
Def ("?<", Name_Op_Match_Less);
Def ("?<=", Name_Op_Match_Less_Equal);
Def ("?>", Name_Op_Match_Greater);
Def ("?>=", Name_Op_Match_Greater_Equal);
-- Create Attributes.
Def ("base", Name_Base);
Def ("left", Name_Left);
Def ("right", Name_Right);
Def ("high", Name_High);
Def ("low", Name_Low);
Def ("pos", Name_Pos);
Def ("val", Name_Val);
Def ("succ", Name_Succ);
Def ("pred", Name_Pred);
Def ("leftof", Name_Leftof);
Def ("rightof", Name_Rightof);
Def ("reverse_range", Name_Reverse_Range);
Def ("length", Name_Length);
Def ("delayed", Name_Delayed);
Def ("stable", Name_Stable);
Def ("quiet", Name_Quiet);
Def ("transaction", Name_Transaction);
Def ("event", Name_Event);
Def ("active", Name_Active);
Def ("last_event", Name_Last_Event);
Def ("last_active", Name_Last_Active);
Def ("last_value", Name_Last_Value);
Def ("behavior", Name_Behavior);
Def ("structure", Name_Structure);
Def ("ascending", Name_Ascending);
Def ("image", Name_Image);
Def ("value", Name_Value);
Def ("driving", Name_Driving);
Def ("driving_value", Name_Driving_Value);
Def ("simple_name", Name_Simple_Name);
Def ("instance_name", Name_Instance_Name);
Def ("path_name", Name_Path_Name);
Def ("element", Name_Element);
Def ("contribution", Name_Contribution);
Def ("dot", Name_Dot);
Def ("integ", Name_Integ);
Def ("above", Name_Above);
Def ("zoh", Name_ZOH);
Def ("ltf", Name_LTF);
Def ("ztf", Name_ZTF);
Def ("ramp", Name_Ramp);
Def ("slew", Name_Slew);
-- Create standard.
Def ("std", Name_Std);
Def ("standard", Name_Standard);
Def ("boolean", Name_Boolean);
Def ("false", Name_False);
Def ("true", Name_True);
Def ("bit", Name_Bit);
Def ("character", Name_Character);
Def ("severity_level", Name_Severity_Level);
Def ("note", Name_Note);
Def ("warning", Name_Warning);
Def ("error", Name_Error);
Def ("failure", Name_Failure);
Def ("UNIVERSAL_INTEGER", Name_Universal_Integer);
Def ("UNIVERSAL_REAL", Name_Universal_Real);
Def ("CONVERTIBLE_INTEGER", Name_Convertible_Integer);
Def ("CONVERTIBLE_REAL", Name_Convertible_Real);
Def ("integer", Name_Integer);
Def ("real", Name_Real);
Def ("time", Name_Time);
Def ("fs", Name_Fs);
Def ("ps", Name_Ps);
Def ("ns", Name_Ns);
Def ("us", Name_Us);
Def ("ms", Name_Ms);
Def ("sec", Name_Sec);
Def ("min", Name_Min);
Def ("hr", Name_Hr);
Def ("delay_length", Name_Delay_Length);
Def ("now", Name_Now);
Def ("natural", Name_Natural);
Def ("positive", Name_Positive);
Def ("string", Name_String);
Def ("bit_vector", Name_Bit_Vector);
Def ("file_open_kind", Name_File_Open_Kind);
Def ("read_mode", Name_Read_Mode);
Def ("write_mode", Name_Write_Mode);
Def ("append_mode", Name_Append_Mode);
Def ("file_open_status", Name_File_Open_Status);
Def ("open_ok", Name_Open_Ok);
Def ("status_error", Name_Status_Error);
Def ("name_error", Name_Name_Error);
Def ("mode_error", Name_Mode_Error);
Def ("foreign", Name_Foreign);
Def ("boolean_vector", Name_Boolean_Vector);
Def ("to_bstring", Name_To_Bstring);
Def ("to_binary_string", Name_To_Binary_String);
Def ("to_ostring", Name_To_Ostring);
Def ("to_octal_string", Name_To_Octal_String);
Def ("to_hstring", Name_To_Hstring);
Def ("to_hex_string", Name_To_Hex_String);
Def ("integer_vector", Name_Integer_Vector);
Def ("real_vector", Name_Real_Vector);
Def ("time_vector", Name_Time_Vector);
Def ("digits", Name_Digits);
Def ("format", Name_Format);
Def ("unit", Name_Unit);
Def ("domain_type", Name_Domain_Type);
Def ("quiescent_domain", Name_Quiescent_Domain);
Def ("time_domain", Name_Time_Domain);
Def ("frequency_domain", Name_Frequency_Domain);
Def ("domain", Name_Domain);
Def ("frequency", Name_Frequency);
Def ("real_vector", Name_Real_Vector);
Def ("nul", Name_Nul);
Def ("soh", Name_Soh);
Def ("stx", Name_Stx);
Def ("etx", Name_Etx);
Def ("eot", Name_Eot);
Def ("enq", Name_Enq);
Def ("ack", Name_Ack);
Def ("bel", Name_Bel);
Def ("bs", Name_Bs);
Def ("ht", Name_Ht);
Def ("lf", Name_Lf);
Def ("vt", Name_Vt);
Def ("ff", Name_Ff);
Def ("cr", Name_Cr);
Def ("so", Name_So);
Def ("si", Name_Si);
Def ("dle", Name_Dle);
Def ("dc1", Name_Dc1);
Def ("dc2", Name_Dc2);
Def ("dc3", Name_Dc3);
Def ("dc4", Name_Dc4);
Def ("nak", Name_Nak);
Def ("syn", Name_Syn);
Def ("etb", Name_Etb);
Def ("can", Name_Can);
Def ("em", Name_Em);
Def ("sub", Name_Sub);
Def ("esc", Name_Esc);
Def ("fsp", Name_Fsp);
Def ("gsp", Name_Gsp);
Def ("rsp", Name_Rsp);
Def ("usp", Name_Usp);
Def ("del", Name_Del);
Def ("c128", Name_C128);
Def ("c129", Name_C129);
Def ("c130", Name_C130);
Def ("c131", Name_C131);
Def ("c132", Name_C132);
Def ("c133", Name_C133);
Def ("c134", Name_C134);
Def ("c135", Name_C135);
Def ("c136", Name_C136);
Def ("c137", Name_C137);
Def ("c138", Name_C138);
Def ("c139", Name_C139);
Def ("c140", Name_C140);
Def ("c141", Name_C141);
Def ("c142", Name_C142);
Def ("c143", Name_C143);
Def ("c144", Name_C144);
Def ("c145", Name_C145);
Def ("c146", Name_C146);
Def ("c147", Name_C147);
Def ("c148", Name_C148);
Def ("c149", Name_C149);
Def ("c150", Name_C150);
Def ("c151", Name_C151);
Def ("c152", Name_C152);
Def ("c153", Name_C153);
Def ("c154", Name_C154);
Def ("c155", Name_C155);
Def ("c156", Name_C156);
Def ("c157", Name_C157);
Def ("c158", Name_C158);
Def ("c159", Name_C159);
-- Create misc.
Def ("guard", Name_Guard);
Def ("deallocate", Name_Deallocate);
Def ("file_open", Name_File_Open);
Def ("file_close", Name_File_Close);
Def ("read", Name_Read);
Def ("write", Name_Write);
Def ("flush", Name_Flush);
Def ("endfile", Name_Endfile);
Def ("p", Name_P);