|
6 | 6 | use base64::engine::general_purpose::STANDARD_NO_PAD;
|
7 | 7 | #[cfg(CRYPTOGRAPHY_OPENSSL_320_OR_GREATER)]
|
8 | 8 | use base64::engine::Engine;
|
| 9 | +use pyo3::prelude::PyAnyMethods; |
9 | 10 | #[cfg(not(CRYPTOGRAPHY_IS_LIBRESSL))]
|
10 | 11 | use pyo3::types::PyBytesMethods;
|
11 | 12 |
|
12 | 13 | use crate::backend::hashes;
|
| 14 | +use crate::backend::hmac::Hmac; |
13 | 15 | use crate::buf::CffiBuf;
|
14 | 16 | use crate::error::{CryptographyError, CryptographyResult};
|
15 | 17 | use crate::exceptions;
|
@@ -447,12 +449,239 @@ impl Argon2id {
|
447 | 449 | }
|
448 | 450 | }
|
449 | 451 |
|
| 452 | +#[pyo3::pyclass(module = "cryptography.hazmat.primitives.kdf.hkdf", name = "HKDF")] |
| 453 | +struct Hkdf { |
| 454 | + algorithm: pyo3::Py<pyo3::PyAny>, |
| 455 | + salt: pyo3::Py<pyo3::types::PyBytes>, |
| 456 | + info: Option<pyo3::Py<pyo3::types::PyBytes>>, |
| 457 | + length: usize, |
| 458 | + used: bool, |
| 459 | +} |
| 460 | + |
| 461 | +#[pyo3::pymethods] |
| 462 | +impl Hkdf { |
| 463 | + #[new] |
| 464 | + #[pyo3(signature = (algorithm, length, salt=None, info=None, backend=None))] |
| 465 | + fn new( |
| 466 | + py: pyo3::Python<'_>, |
| 467 | + algorithm: pyo3::Py<pyo3::PyAny>, |
| 468 | + length: usize, |
| 469 | + salt: Option<pyo3::Py<pyo3::types::PyBytes>>, |
| 470 | + info: Option<pyo3::Py<pyo3::types::PyBytes>>, |
| 471 | + backend: Option<pyo3::Bound<'_, pyo3::PyAny>>, |
| 472 | + ) -> CryptographyResult<Self> { |
| 473 | + _ = backend; |
| 474 | + |
| 475 | + let algorithm_bound = algorithm.bind(py); |
| 476 | + let digest_size = algorithm_bound |
| 477 | + .getattr(pyo3::intern!(py, "digest_size"))? |
| 478 | + .extract::<usize>()?; |
| 479 | + |
| 480 | + let max_length = 255 * digest_size; |
| 481 | + if length > max_length { |
| 482 | + return Err(CryptographyError::from( |
| 483 | + pyo3::exceptions::PyValueError::new_err(format!( |
| 484 | + "Cannot derive keys larger than {} octets.", |
| 485 | + max_length |
| 486 | + )), |
| 487 | + )); |
| 488 | + } |
| 489 | + |
| 490 | + let salt = if let Some(salt) = salt { |
| 491 | + salt |
| 492 | + } else { |
| 493 | + let zero_salt = vec![0u8; digest_size]; |
| 494 | + pyo3::types::PyBytes::new(py, &zero_salt).into() |
| 495 | + }; |
| 496 | + |
| 497 | + Ok(Hkdf { |
| 498 | + algorithm, |
| 499 | + salt, |
| 500 | + info, |
| 501 | + length, |
| 502 | + used: false, |
| 503 | + }) |
| 504 | + } |
| 505 | + |
| 506 | + fn _extract<'p>( |
| 507 | + &self, |
| 508 | + py: pyo3::Python<'p>, |
| 509 | + key_material: &[u8], |
| 510 | + ) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> { |
| 511 | + let algorithm_bound = self.algorithm.bind(py); |
| 512 | + let mut hmac = Hmac::new_bytes(py, self.salt.as_bytes(py), algorithm_bound)?; |
| 513 | + hmac.update_bytes(key_material)?; |
| 514 | + hmac.finalize(py) |
| 515 | + } |
| 516 | + |
| 517 | + fn derive<'p>( |
| 518 | + &mut self, |
| 519 | + py: pyo3::Python<'p>, |
| 520 | + key_material: CffiBuf<'_>, |
| 521 | + ) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> { |
| 522 | + if self.used { |
| 523 | + return Err(exceptions::already_finalized_error()); |
| 524 | + } |
| 525 | + self.used = true; |
| 526 | + |
| 527 | + // HKDF Extract |
| 528 | + let prk = self._extract(py, key_material.as_bytes())?; |
| 529 | + |
| 530 | + // HKDF Expand |
| 531 | + let mut hkdf_expand = HkdfExpand::new( |
| 532 | + py, |
| 533 | + self.algorithm.clone_ref(py), |
| 534 | + self.length, |
| 535 | + self.info.as_ref().map(|i| i.clone_ref(py)), |
| 536 | + None, |
| 537 | + )?; |
| 538 | + let prk_bytes = prk.as_bytes(); |
| 539 | + let cffi_buf = CffiBuf::from_bytes(py, prk_bytes); |
| 540 | + hkdf_expand.derive(py, cffi_buf) |
| 541 | + } |
| 542 | + |
| 543 | + fn verify( |
| 544 | + &mut self, |
| 545 | + py: pyo3::Python<'_>, |
| 546 | + key_material: CffiBuf<'_>, |
| 547 | + expected_key: CffiBuf<'_>, |
| 548 | + ) -> CryptographyResult<()> { |
| 549 | + let actual = self.derive(py, key_material)?; |
| 550 | + let actual_bytes = actual.as_bytes(); |
| 551 | + let expected_bytes = expected_key.as_bytes(); |
| 552 | + |
| 553 | + if actual_bytes.len() != expected_bytes.len() |
| 554 | + || !openssl::memcmp::eq(actual_bytes, expected_bytes) |
| 555 | + { |
| 556 | + return Err(CryptographyError::from(exceptions::InvalidKey::new_err( |
| 557 | + "Keys do not match.", |
| 558 | + ))); |
| 559 | + } |
| 560 | + |
| 561 | + Ok(()) |
| 562 | + } |
| 563 | +} |
| 564 | + |
| 565 | +#[pyo3::pyclass( |
| 566 | + module = "cryptography.hazmat.primitives.kdf.hkdf", |
| 567 | + name = "HKDFExpand" |
| 568 | +)] |
| 569 | +struct HkdfExpand { |
| 570 | + algorithm: pyo3::Py<pyo3::PyAny>, |
| 571 | + info: pyo3::Py<pyo3::types::PyBytes>, |
| 572 | + length: usize, |
| 573 | + used: bool, |
| 574 | +} |
| 575 | + |
| 576 | +#[pyo3::pymethods] |
| 577 | +impl HkdfExpand { |
| 578 | + #[new] |
| 579 | + #[pyo3(signature = (algorithm, length, info, backend=None))] |
| 580 | + fn new( |
| 581 | + py: pyo3::Python<'_>, |
| 582 | + algorithm: pyo3::Py<pyo3::PyAny>, |
| 583 | + length: usize, |
| 584 | + info: Option<pyo3::Py<pyo3::types::PyBytes>>, |
| 585 | + backend: Option<pyo3::Bound<'_, pyo3::PyAny>>, |
| 586 | + ) -> CryptographyResult<Self> { |
| 587 | + _ = backend; |
| 588 | + |
| 589 | + let algorithm_bound = algorithm.bind(py); |
| 590 | + let digest_size = algorithm_bound |
| 591 | + .getattr(pyo3::intern!(py, "digest_size"))? |
| 592 | + .extract::<usize>()?; |
| 593 | + |
| 594 | + let max_length = 255 * digest_size; |
| 595 | + if length > max_length { |
| 596 | + return Err(CryptographyError::from( |
| 597 | + pyo3::exceptions::PyValueError::new_err(format!( |
| 598 | + "Cannot derive keys larger than {} octets.", |
| 599 | + max_length |
| 600 | + )), |
| 601 | + )); |
| 602 | + } |
| 603 | + |
| 604 | + let info = if let Some(info) = info { |
| 605 | + info |
| 606 | + } else { |
| 607 | + pyo3::types::PyBytes::new(py, b"").into() |
| 608 | + }; |
| 609 | + |
| 610 | + Ok(HkdfExpand { |
| 611 | + algorithm, |
| 612 | + info, |
| 613 | + length, |
| 614 | + used: false, |
| 615 | + }) |
| 616 | + } |
| 617 | + |
| 618 | + fn derive<'p>( |
| 619 | + &mut self, |
| 620 | + py: pyo3::Python<'p>, |
| 621 | + key_material: CffiBuf<'_>, |
| 622 | + ) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> { |
| 623 | + if self.used { |
| 624 | + return Err(exceptions::already_finalized_error()); |
| 625 | + } |
| 626 | + self.used = true; |
| 627 | + |
| 628 | + let algorithm_bound = self.algorithm.bind(py); |
| 629 | + |
| 630 | + let mut output = Vec::new(); |
| 631 | + let mut counter = 1u8; |
| 632 | + let mut previous_output = Vec::new(); |
| 633 | + |
| 634 | + let h_prime = Hmac::new_bytes(py, key_material.as_bytes(), algorithm_bound)?; |
| 635 | + while output.len() < self.length { |
| 636 | + let mut h = h_prime.copy(py)?; |
| 637 | + h.update_bytes(&previous_output)?; |
| 638 | + h.update_bytes(self.info.as_bytes(py))?; |
| 639 | + h.update_bytes(&[counter])?; |
| 640 | + |
| 641 | + let block = h.finalize(py)?; |
| 642 | + let block_bytes = block.as_bytes(); |
| 643 | + previous_output = block_bytes.to_vec(); |
| 644 | + output.extend_from_slice(block_bytes); |
| 645 | + |
| 646 | + counter += 1; |
| 647 | + } |
| 648 | + |
| 649 | + output.truncate(self.length); |
| 650 | + Ok(pyo3::types::PyBytes::new(py, &output)) |
| 651 | + } |
| 652 | + |
| 653 | + fn verify( |
| 654 | + &mut self, |
| 655 | + py: pyo3::Python<'_>, |
| 656 | + key_material: CffiBuf<'_>, |
| 657 | + expected_key: CffiBuf<'_>, |
| 658 | + ) -> CryptographyResult<()> { |
| 659 | + let actual = self.derive(py, key_material)?; |
| 660 | + let actual_bytes = actual.as_bytes(); |
| 661 | + let expected_bytes = expected_key.as_bytes(); |
| 662 | + |
| 663 | + if actual_bytes.len() != expected_bytes.len() |
| 664 | + || !openssl::memcmp::eq(actual_bytes, expected_bytes) |
| 665 | + { |
| 666 | + return Err(CryptographyError::from(exceptions::InvalidKey::new_err( |
| 667 | + "Keys do not match.", |
| 668 | + ))); |
| 669 | + } |
| 670 | + |
| 671 | + Ok(()) |
| 672 | + } |
| 673 | +} |
| 674 | + |
450 | 675 | #[pyo3::pymodule]
|
451 | 676 | pub(crate) mod kdf {
|
452 | 677 | #[pymodule_export]
|
453 | 678 | use super::derive_pbkdf2_hmac;
|
454 | 679 | #[pymodule_export]
|
455 | 680 | use super::Argon2id;
|
456 | 681 | #[pymodule_export]
|
| 682 | + use super::Hkdf; |
| 683 | + #[pymodule_export] |
| 684 | + use super::HkdfExpand; |
| 685 | + #[pymodule_export] |
457 | 686 | use super::Scrypt;
|
458 | 687 | }
|
0 commit comments