-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from http4s/pr/native
Cross-build for Scala Native
- Loading branch information
Showing
15 changed files
with
390 additions
and
14 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 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
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions
28
crypto/native/src/main/scala/org/http4s/crypto/CryptoPlatform.scala
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,28 @@ | ||
/* | ||
* Copyright 2021 http4s.org | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.http4s.crypto | ||
|
||
import cats.effect.kernel.Sync | ||
|
||
private[crypto] trait CryptoCompanionPlatform { | ||
implicit def forSync[F[_]: Sync]: Crypto[F] = | ||
new UnsealedCrypto[F] { | ||
override def hash: Hash[F] = Hash[F] | ||
override def hmac: Hmac[F] = Hmac[F] | ||
override def hmacKeyGen: HmacKeyGen[F] = HmacKeyGen[F] | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
crypto/native/src/main/scala/org/http4s/crypto/HashPlatform.scala
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,56 @@ | ||
/* | ||
* Copyright 2021 http4s.org | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.http4s.crypto | ||
|
||
import cats.ApplicativeThrow | ||
import scodec.bits.ByteVector | ||
|
||
import scala.scalanative.unsafe._ | ||
import scala.scalanative.unsigned._ | ||
|
||
private[crypto] trait HashCompanionPlatform { | ||
implicit def forApplicativeThrow[F[_]](implicit F: ApplicativeThrow[F]): Hash[F] = | ||
new UnsealedHash[F] { | ||
def digest(algorithm: HashAlgorithm, data: ByteVector): F[ByteVector] = | ||
Zone { implicit z => | ||
import HashAlgorithm._ | ||
|
||
val name = algorithm match { | ||
case MD5 => c"MD5" | ||
case SHA1 => c"SHA1" | ||
case SHA256 => c"SHA256" | ||
case SHA512 => c"SHA512" | ||
} | ||
|
||
val `type` = openssl.evp.EVP_get_digestbyname(name) | ||
if (`type` == null) | ||
F.raiseError(new GeneralSecurityException("EVP_get_digestbyname")) | ||
else { | ||
val md = stackalloc[CUnsignedChar](openssl.evp.EVP_MAX_MD_SIZE) | ||
val size = stackalloc[CUnsignedInt]() | ||
|
||
if (openssl | ||
.evp | ||
.EVP_Digest(data.toPtr, data.size.toULong, md, size, `type`, null) == 1) | ||
F.pure(ByteVector.fromPtr(md.asInstanceOf[Ptr[Byte]], (!size).toLong)) | ||
else | ||
F.raiseError(new GeneralSecurityException("EVP_DIGEST")) | ||
} | ||
} | ||
|
||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
crypto/native/src/main/scala/org/http4s/crypto/HmacKeyGenPlatform.scala
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,36 @@ | ||
/* | ||
* Copyright 2021 http4s.org | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.http4s.crypto | ||
|
||
import cats.effect.kernel.Sync | ||
import scodec.bits.ByteVector | ||
|
||
import scala.scalanative.unsafe._ | ||
|
||
private[crypto] trait HmacKeyGenCompanionPlatform { | ||
implicit def forSync[F[_]](implicit F: Sync[F]): HmacKeyGen[F] = | ||
new UnsealedHmacKeyGen[F] { | ||
def generateKey[A <: HmacAlgorithm](algorithm: A): F[SecretKey[A]] = | ||
F.delay { | ||
val len = algorithm.minimumKeyLength | ||
val buf = stackalloc[Byte](len.toLong) | ||
if (openssl.rand.RAND_bytes(buf, len) != 1) | ||
throw new GeneralSecurityException("RAND_bytes") | ||
SecretKeySpec(ByteVector.fromPtr(buf, len.toLong), algorithm) | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
crypto/native/src/main/scala/org/http4s/crypto/HmacPlatform.scala
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,69 @@ | ||
/* | ||
* Copyright 2021 http4s.org | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.http4s.crypto | ||
|
||
import cats.ApplicativeThrow | ||
import scodec.bits.ByteVector | ||
|
||
import scala.scalanative.unsafe._ | ||
import scala.scalanative.unsigned._ | ||
|
||
private[crypto] trait HmacPlatform[F[_]] | ||
|
||
private[crypto] trait HmacCompanionPlatform { | ||
implicit def forApplicativeThrow[F[_]](implicit F: ApplicativeThrow[F]): Hmac[F] = | ||
new UnsealedHmac[F] { | ||
|
||
def digest(key: SecretKey[HmacAlgorithm], data: ByteVector): F[ByteVector] = | ||
Zone { implicit z => | ||
import HmacAlgorithm._ | ||
|
||
val SecretKeySpec(keyBytes, algorithm) = key | ||
|
||
val name = algorithm match { | ||
case SHA1 => c"SHA1" | ||
case SHA256 => c"SHA256" | ||
case SHA512 => c"SHA512" | ||
} | ||
|
||
val evpMd = openssl.evp.EVP_get_digestbyname(name) | ||
if (evpMd == null) | ||
F.raiseError(new GeneralSecurityException("EVP_get_digestbyname")) | ||
else { | ||
val md = stackalloc[CUnsignedChar](openssl.evp.EVP_MAX_MD_SIZE) | ||
val mdLen = stackalloc[CUnsignedInt]() | ||
|
||
if (openssl | ||
.hmac | ||
.HMAC( | ||
evpMd, | ||
keyBytes.toPtr, | ||
keyBytes.size.toInt, | ||
data.toPtr.asInstanceOf[Ptr[CUnsignedChar]], | ||
data.size.toULong, | ||
md, | ||
mdLen) != null) | ||
F.pure(ByteVector.fromPtr(md.asInstanceOf[Ptr[Byte]], (!mdLen).toLong)) | ||
else | ||
F.raiseError(new GeneralSecurityException("HMAC")) | ||
} | ||
} | ||
|
||
def importKey[A <: HmacAlgorithm](key: ByteVector, algorithm: A): F[SecretKey[A]] = | ||
F.pure(SecretKeySpec(key, algorithm)) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
crypto/native/src/main/scala/org/http4s/crypto/KeyPlatform.scala
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,23 @@ | ||
/* | ||
* Copyright 2021 http4s.org | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.http4s.crypto | ||
|
||
private[crypto] trait KeyPlatform | ||
private[crypto] trait PublicKeyPlatform | ||
private[crypto] trait PrivateKeyPlatform | ||
private[crypto] trait SecretKeyPlatform | ||
private[crypto] trait SecretKeySpecPlatform[+A <: Algorithm] |
Oops, something went wrong.