-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic HMAC and HKDF functions (#38)
- Loading branch information
Showing
11 changed files
with
372 additions
and
64 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,25 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use thiserror::Error; | ||
|
||
// A function should validate its arguments and return an indicative errors where needed. | ||
// However, once the function is executing the cryptographic protocol/algorithm (directly/ | ||
// indirectly) then it should not return explicit errors as it might leak private information. | ||
// In those cases the function should return the opaque, general error FastCryptoError::GeneralError. | ||
// When in doubt, prefer FastCryptoError::GeneralError. | ||
|
||
#[derive(Error, Debug, PartialEq, Eq)] | ||
pub enum FastCryptoError { | ||
#[error("Invalid value was given to the function")] | ||
InvalidInput, | ||
|
||
#[error("Expected input of length at least {0}")] | ||
InputTooShort(usize), | ||
|
||
#[error("Expected input of length at most {0}")] | ||
InputTooLong(usize), | ||
|
||
#[error("General cryptographic error")] | ||
GeneralError, | ||
} |
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,37 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::traits::{FromUniformBytes, ToFromBytes}; | ||
use zeroize::{Zeroize, ZeroizeOnDrop}; | ||
|
||
/// Private key/seed of any/fixed size. | ||
/// | ||
#[derive(Debug, PartialEq, Eq, Zeroize, ZeroizeOnDrop)] | ||
pub struct PrivateSeed<const RECOMMENDED_LENGTH: usize, const FIXED_LENGTH_ONLY: bool> { | ||
bytes: Vec<u8>, | ||
} | ||
|
||
impl<const N: usize, const B: bool> FromUniformBytes<N> for PrivateSeed<N, B> {} | ||
|
||
impl<const N: usize, const B: bool> AsRef<[u8]> for PrivateSeed<N, B> { | ||
fn as_ref(&self) -> &[u8] { | ||
self.bytes.as_ref() | ||
} | ||
} | ||
|
||
impl<const RECOMMENDED_LENGTH: usize, const FIXED_LENGTH_ONLY: bool> ToFromBytes | ||
for PrivateSeed<RECOMMENDED_LENGTH, FIXED_LENGTH_ONLY> | ||
{ | ||
fn from_bytes(bytes: &[u8]) -> Result<Self, signature::Error> { | ||
if FIXED_LENGTH_ONLY && bytes.len() != RECOMMENDED_LENGTH { | ||
return Err(signature::Error::new()); | ||
} | ||
Ok(Self { | ||
bytes: bytes.to_vec(), | ||
}) | ||
} | ||
|
||
fn as_bytes(&self) -> &[u8] { | ||
self.as_ref() | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.