-
Notifications
You must be signed in to change notification settings - Fork 539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose HMAC-SHA-256 interface #8230
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1b3a66b
Expose HMAC-SHA-256 interface
vitoreiji 259e981
use HMAC-SHA256 implementation in AES
vaf-hub 63a06b7
add HMAC-SHA256 compatibility test
vaf-hub 8d0447d
Expose HMAC-SHA-256 interface on Android
vitoreiji 0445a32
add compatibility test for IOS
tutao-mac 9b7c6eb
expose hmac and compatibility test it in the tuta-sdk
vaf-hub e825894
Implement HMAC-SHA256 in IosNativeCryptoFacade
tutao-mac 6f2acb0
Use Foundation library directly for generating HMAC-SHA-256
juni2k 38a9320
Add test function: TUTAssertThrowsErrorAsync
juni2k 426ad12
Add round trip, bad key, bad data tests for HMAC-SHA-256 on iOS
juni2k 54884b2
Implement new NativeCryptoFacade methods on Android
vitoreiji 4143251
Fix compatibility test: `hmac_sha256()`
juni2k 0dbd4d2
Add round trip, bad key, bad data tests for HMAC-SHA-256 on Android
juni2k 3b75691
rename HMAC_SHA256_SIZE constant, pass struct rather than individual …
vaf-hub f542227
Throw when calling HMAC-SHA-256 functions in DesktopNativeCryptoFacade
juni2k 42328a7
make cargo-clippy happy
vaf-hub e7845b4
refactor rust hmac implementation to make the api more robust and the…
vaf-hub bfa42c0
Test SDK verify_hmac_sha256
vitoreiji File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
55 changes: 55 additions & 0 deletions
55
app-android/app/src/androidTest/java/de/tutao/tutanota/HmacTest.kt
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,55 @@ | ||
package de.tutao.tutanota | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import de.tutao.tutashared.CryptoError | ||
import de.tutao.tutashared.crypto.Crypto | ||
import org.junit.Assert.assertThrows | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import java.security.SecureRandom | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class HmacTest { | ||
lateinit var randomizer: SecureRandom | ||
lateinit var key: ByteArray | ||
lateinit var data: ByteArray | ||
lateinit var macTag: ByteArray | ||
|
||
@Before | ||
fun setup() { | ||
randomizer = SecureRandom() | ||
key = ByteArray(32) | ||
data = ByteArray(256) | ||
|
||
randomizer.nextBytes(key) | ||
randomizer.nextBytes(data) | ||
|
||
macTag = Crypto.hmacSha256(key, data) | ||
} | ||
|
||
@Test | ||
fun roundTrip() { | ||
Crypto.verifyHmacSha256(key, data, macTag) | ||
} | ||
|
||
@Test | ||
fun badKey() { | ||
val badKey = ByteArray(32) | ||
randomizer.nextBytes(badKey) | ||
|
||
assertThrows(CryptoError::class.java) { | ||
Crypto.verifyHmacSha256(badKey, data, macTag) | ||
} | ||
} | ||
|
||
@Test | ||
fun badData() { | ||
val badData = ByteArray(256) | ||
randomizer.nextBytes(badData) | ||
|
||
assertThrows(CryptoError::class.java) { | ||
Crypto.verifyHmacSha256(key, badData, macTag) | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
app-android/app/src/androidTest/java/de/tutao/tutanota/testdata/HmacSha256TestData.java
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,41 @@ | ||
package de.tutao.tutanota.testdata; | ||
|
||
public class HmacSha256TestData { | ||
|
||
String keyHex; | ||
String dataHex; | ||
String hmacSha256TagHex; | ||
|
||
public HmacSha256TestData() { | ||
} | ||
|
||
public HmacSha256TestData(String keyHex, String dataHex, String hmacSha256TagHex) { | ||
this.keyHex = keyHex; | ||
this.dataHex = dataHex; | ||
this.hmacSha256TagHex = hmacSha256TagHex; | ||
} | ||
|
||
public String getKeyHex() { | ||
return keyHex; | ||
} | ||
|
||
public void setKeyHex(String keyHex) { | ||
this.keyHex = keyHex; | ||
} | ||
|
||
public String getDataHex() { | ||
return dataHex; | ||
} | ||
|
||
public void setDataHex(String dataHex) { | ||
this.dataHex = dataHex; | ||
} | ||
|
||
public String getHmacSha256TagHex() { | ||
return hmacSha256TagHex; | ||
} | ||
|
||
public void setHmacSha256TagHex(String hmacSha256TagHex) { | ||
this.hmacSha256TagHex = hmacSha256TagHex; | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
app-android/tutashared/src/main/java/de/tutao/tutashared/crypto/Crypto.kt
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,24 @@ | ||
package de.tutao.tutashared.crypto | ||
|
||
import de.tutao.tutashared.AndroidNativeCryptoFacade.Companion.HMAC_SHA_256 | ||
import de.tutao.tutashared.CryptoError | ||
import javax.crypto.Mac | ||
import javax.crypto.spec.SecretKeySpec | ||
|
||
class Crypto { | ||
companion object { | ||
fun hmacSha256(key: ByteArray, data: ByteArray): ByteArray { | ||
val macKey = SecretKeySpec(key, HMAC_SHA_256) | ||
val hmac = Mac.getInstance(HMAC_SHA_256) | ||
hmac.init(macKey) | ||
return hmac.doFinal(data) | ||
} | ||
|
||
fun verifyHmacSha256(key: ByteArray, data: ByteArray, tag: ByteArray) { | ||
val computedTag = hmacSha256(key, data) | ||
if (!tag.contentEquals(computedTag)) { | ||
throw CryptoError("invalid mac") | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,41 +1,32 @@ | ||
import TutanotaSharedFramework | ||
import CryptoKit | ||
import tutasdk | ||
|
||
/// High-level cryptographic operations API | ||
/// Is an actor because we want to have serial execution for all the cryptogaphic operations, doing them in parallel is usually too | ||
/// much for the device. | ||
public actor IosNativeCryptoFacade: NativeCryptoFacade { | ||
public init() {} | ||
|
||
public func aesEncryptFile(_ key: DataWrapper, _ fileUri: String, _ iv: DataWrapper) async throws -> EncryptedFileInfo { | ||
|
||
if !FileUtils.fileExists(atPath: fileUri) { throw CryptoError(message: "File to encrypt does not exist \(fileUri)") } | ||
let encryptedFolder = try FileUtils.getEncryptedFolder() | ||
let fileName = (fileUri as NSString).lastPathComponent | ||
let encryptedFilePath = (encryptedFolder as NSString).appendingPathComponent(fileName) | ||
let plainTextData = try Data(contentsOf: URL(fileURLWithPath: fileUri)) | ||
let outputData = try aesEncryptData(plainTextData, withKey: key.data, withIV: iv.data) | ||
let result = EncryptedFileInfo(uri: encryptedFilePath, unencryptedSize: plainTextData.count) | ||
|
||
try outputData.write(to: URL(fileURLWithPath: encryptedFilePath)) | ||
|
||
return result | ||
} | ||
|
||
public func aesDecryptFile(_ key: DataWrapper, _ fileUri: String) async throws -> String { | ||
if !FileUtils.fileExists(atPath: fileUri) { throw CryptoError(message: "File to decrypt does not exist") } | ||
|
||
let encryptedData = try Data(contentsOf: URL(fileURLWithPath: fileUri)) | ||
let plaintextData = try aesDecryptData(encryptedData, withKey: key.data) | ||
|
||
let decryptedFolder = try FileUtils.getDecryptedFolder() | ||
let fileName = (fileUri as NSString).lastPathComponent | ||
let plaintextPath = (decryptedFolder as NSString).appendingPathComponent(fileName) | ||
try plaintextData.write(to: URL(fileURLWithPath: plaintextPath), options: .atomic) | ||
|
||
return plaintextPath | ||
} | ||
|
||
public func rsaEncrypt(_ publicKey: RsaPublicKey, _ data: DataWrapper, _ seed: DataWrapper) async throws -> DataWrapper { | ||
try tutasdk.rsaEncryptWithPublicKeyComponents( | ||
data: data.data, | ||
|
@@ -45,7 +36,6 @@ public actor IosNativeCryptoFacade: NativeCryptoFacade { | |
) | ||
.wrap() | ||
} | ||
|
||
public func rsaDecrypt(_ privateKey: RsaPrivateKey, _ data: DataWrapper) async throws -> DataWrapper { | ||
try tutasdk.rsaDecryptWithPrivateKeyComponents( | ||
ciphertext: data.data, | ||
|
@@ -56,28 +46,34 @@ public actor IosNativeCryptoFacade: NativeCryptoFacade { | |
) | ||
.wrap() | ||
} | ||
|
||
public func argon2idGeneratePassphraseKey(_ passphrase: String, _ salt: DataWrapper) async throws -> DataWrapper { | ||
try tutasdk.argon2idGenerateKeyFromPassphrase(passphrase: passphrase, salt: salt.data).wrap() | ||
} | ||
|
||
public func generateKyberKeypair(_ seed: DataWrapper) async throws -> TutanotaSharedFramework.KyberKeyPair { | ||
let keypair = tutasdk.generateKyberKeypair() | ||
return KyberKeyPair(publicKey: KyberPublicKey(raw: keypair.publicKey.wrap()), privateKey: KyberPrivateKey(raw: keypair.privateKey.wrap())) | ||
} | ||
|
||
public func kyberEncapsulate(_ publicKey: KyberPublicKey, _ seed: DataWrapper) async throws -> TutanotaSharedFramework.KyberEncapsulation { | ||
do { | ||
let sdkEncapsulation = try tutasdk.kyberEncapsulateWithPubKey(publicKeyBytes: publicKey.raw.data) | ||
return KyberEncapsulation(ciphertext: sdkEncapsulation.ciphertext.wrap(), sharedSecret: sdkEncapsulation.sharedSecret.wrap()) | ||
} catch { throw CryptoError(message: error.localizedDescription) } | ||
} | ||
|
||
public func kyberDecapsulate(_ privateKey: KyberPrivateKey, _ ciphertext: DataWrapper) async throws -> DataWrapper { | ||
do { return try tutasdk.kyberDecapsulateWithPrivKey(privateKeyBytes: privateKey.raw.data, ciphertext: ciphertext.data).wrap() } catch { | ||
throw CryptoError(message: error.localizedDescription) | ||
} | ||
|
||
} | ||
public func hmacSha256(_ key: DataWrapper, _ data: DataWrapper) -> DataWrapper { | ||
let symmetricKey = SymmetricKey(data: key.data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should probably be |
||
let macTag = HMAC<SHA256>.authenticationCode(for: data.data, using: symmetricKey) | ||
var bytes: [UInt8] = [] | ||
bytes.append(contentsOf: macTag) | ||
return DataWrapper(data: Data(bytes: bytes, count: bytes.count)) | ||
} | ||
public func verifyHmacSha256(_ key: DataWrapper, _ data: DataWrapper, _ tag: DataWrapper) async throws { | ||
let isValid = HMAC<SHA256>.isValidAuthenticationCode(tag.data, authenticating: data.data, using: SymmetricKey(data: key.data)) | ||
if !isValid { throw TUTErrorFactory.createError(withDomain: TUT_CRYPTO_ERROR, message: "invalid MAC: checksum and/or key is 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason we package it into a class? that's not very idiomatic for Kotlin to collect some functions into statics of a class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason is that we needed those functions with those signatures, which are different from what the
NativeCryptoFacade
wants, and it felt wrong to just stuff them intoAndroidNativeCryptoFacade
. Would that be better?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine to have them in this file, you can just leave them be as functions, without a wrapping class