-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
385 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# This file is dual licensed under the terms of the Apache License, Version | ||
# 2.0, and the BSD License. See the LICENSE file in the root of this repository | ||
# for complete details. | ||
|
||
import typing | ||
|
||
from cryptography.hazmat.primitives import ciphers | ||
|
||
class CMAC: | ||
def __init__( | ||
self, | ||
algorithm: ciphers.BlockCipherAlgorithm, | ||
backend: typing.Any = None, | ||
) -> None: ... | ||
def update(self, data: bytes) -> None: ... | ||
def finalize(self) -> bytes: ... | ||
def verify(self, signature: bytes) -> None: ... | ||
def copy(self) -> CMAC: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// This file is dual licensed under the terms of the Apache License, Version | ||
// 2.0, and the BSD License. See the LICENSE file in the root of this repository | ||
// for complete details. | ||
|
||
use crate::hmac::DigestBytes; | ||
use crate::{cvt, cvt_p, OpenSSLResult}; | ||
use foreign_types_shared::{ForeignType, ForeignTypeRef}; | ||
use std::ptr; | ||
|
||
foreign_types::foreign_type! { | ||
type CType = ffi::CMAC_CTX; | ||
fn drop = ffi::CMAC_CTX_free; | ||
|
||
pub struct Cmac; | ||
pub struct CmacRef; | ||
} | ||
|
||
// SAFETY: It's safe to have `&` references from multiple threads. | ||
unsafe impl Sync for Cmac {} | ||
// SAFETY: It's safe to move the `Cmac` from one thread to another. | ||
unsafe impl Send for Cmac {} | ||
|
||
impl Cmac { | ||
pub fn new(key: &[u8], cipher: &openssl::symm::Cipher) -> OpenSSLResult<Cmac> { | ||
// SAFETY: All FFI conditions are handled. | ||
unsafe { | ||
let ctx = Cmac::from_ptr(cvt_p(ffi::CMAC_CTX_new())?); | ||
cvt(ffi::CMAC_Init( | ||
ctx.as_ptr(), | ||
key.as_ptr().cast(), | ||
key.len(), | ||
cipher.as_ptr(), | ||
ptr::null_mut(), | ||
))?; | ||
Ok(ctx) | ||
} | ||
} | ||
} | ||
|
||
impl CmacRef { | ||
pub fn update(&mut self, data: &[u8]) -> OpenSSLResult<()> { | ||
// SAFETY: All FFI conditions are handled. | ||
unsafe { | ||
cvt(ffi::CMAC_Update( | ||
self.as_ptr(), | ||
data.as_ptr().cast(), | ||
data.len(), | ||
))?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn finish(&mut self) -> OpenSSLResult<DigestBytes> { | ||
let mut buf = [0; ffi::EVP_MAX_MD_SIZE as usize]; | ||
let mut len = ffi::EVP_MAX_MD_SIZE as usize; | ||
// SAFETY: All FFI conditions are handled. | ||
unsafe { | ||
cvt(ffi::CMAC_Final(self.as_ptr(), buf.as_mut_ptr(), &mut len))?; | ||
} | ||
Ok(DigestBytes { buf, len }) | ||
} | ||
|
||
pub fn copy(&self) -> OpenSSLResult<Cmac> { | ||
// SAFETY: All FFI conditions are handled. | ||
unsafe { | ||
let h = Cmac::from_ptr(cvt_p(ffi::CMAC_CTX_new())?); | ||
cvt(ffi::CMAC_CTX_copy(h.as_ptr(), self.as_ptr()))?; | ||
Ok(h) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.