Skip to content

Commit

Permalink
aes
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT committed Jul 5, 2023
1 parent 44535c0 commit 8b8c71a
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 6 deletions.
123 changes: 123 additions & 0 deletions lib/aes.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
defmodule CA.AES do

def e(x,y), do: :erlang.element(x,y)
def privat(name), do: e(3,:public_key.pem_entry_decode(readPEM("priv/certs/",name)))
def public(name), do: e(3,e(8, e(2, :public_key.pem_entry_decode(readPEM("priv/certs/",name)))))
def readPEM(folder, name), do: hd(:public_key.pem_decode(e(2, :file.read_file(folder <> name))))
def shared(pub, key, scheme), do: :crypto.compute_key(:ecdh, pub, key, scheme)

def decryptAES256ECB(data, key, iv \\ :crypto.strong_rand_bytes(16)) do
:crypto.crypto_one_time(:aes_256_ecb,key,iv,data,[{:encrypt,false}])
end

def decryptAES256CBC(data, key, iv \\ :crypto.strong_rand_bytes(16)) do
:crypto.crypto_one_time(:aes_256_cbc,key,iv,data,[{:encrypt,false}])
end

def decryptAES256GCM(data, key, iv \\ :crypto.strong_rand_bytes(16)) do

Check warning on line 17 in lib/aes.ex

View workflow job for this annotation

GitHub Actions / build

variable "iv" is unused (if the variable is not meant to be used, prefix it with an underscore)
<<iv::binary-16, tag::binary-16, bin::binary>> = data

Check warning on line 18 in lib/aes.ex

View workflow job for this annotation

GitHub Actions / build

variable "bin" is unused (if the variable is not meant to be used, prefix it with an underscore)
:crypto.crypto_one_time_aead(:aes_256_gcm, key, iv, data, "AES256GCM", tag, false)
end

def decryptAES256CCM(data, key, iv \\ :crypto.strong_rand_bytes(16)) do

Check warning on line 22 in lib/aes.ex

View workflow job for this annotation

GitHub Actions / build

variable "iv" is unused (if the variable is not meant to be used, prefix it with an underscore)
<<iv::binary-16, tag::binary-16, bin::binary>> = data

Check warning on line 23 in lib/aes.ex

View workflow job for this annotation

GitHub Actions / build

variable "bin" is unused (if the variable is not meant to be used, prefix it with an underscore)
:crypto.crypto_one_time_aead(:aes_256_ccm, key, iv, data, "AES256CCM", tag, false)
end

def encryptAES256ECB(data, key, iv \\ :crypto.strong_rand_bytes(16)) do
:crypto.crypto_one_time(:aes_256_ecb,key,iv,data,[{:encrypt,true}])
end

def encryptAES256CBC(data, key, iv \\ :crypto.strong_rand_bytes(16)) do
:crypto.crypto_one_time(:aes_256_cbc,key,iv,data,[{:encrypt,true}])
end

def encryptAES256GCM(data, key, iv \\ :crypto.strong_rand_bytes(16)) do
{cipher, tag} = :crypto.crypto_one_time_aead(:aes_256_gcm, key, iv, data, "AES256GCM", true)
iv <> tag <> cipher
end

def encryptAES256CCM(data, key, iv \\ :crypto.strong_rand_bytes(16)) do
{cipher, tag} = :crypto.crypto_one_time_aead(:aes_256_ccm, key, iv, data, "AES256CCM", true)
iv <> tag <> cipher
end

def encrypt(crypto_codec, data, key, iv \\ :crypto.strong_rand_bytes(16))
def encrypt(:aes_256_ecb, data, key, iv), do: encryptAES256ECB(data, key, iv)
def encrypt(:aes_256_cbc, data, key, iv), do: encryptAES256CBC(data, key, iv)
def encrypt(:aes_256_gcm, data, key, iv), do: encryptAES256GCM(data, key, iv)
def encrypt(:aes_256_ccm, data, key, iv), do: encryptAES256CCM(data, key, iv)

def decrypt(crypto_codec, data, key, iv \\ :crypto.strong_rand_bytes(16))
def decrypt(:aes_256_ecb, data, key, iv), do: decryptAES256ECB(data, key, iv)
def decrypt(:aes_256_cbc, data, key, iv), do: decryptAES256CBC(data, key, iv)
def decrypt(:aes_256_gcm, data, key, iv), do: decryptAES256GCM(data, key, iv)
def decrypt(:aes_256_ccm, data, key, iv), do: decryptAES256CCM(data, key, iv)

def testSMIME() do
{:ok,base} = :file.read_file "priv/encrypted.txt" ; [_,s] = :string.split base, "\n\n"
:'CryptographicMessageSyntax-2010'.decode(:ContentInfo, :base64.decode(s))
end

def check_SECP384R1_GCM256() do # SECP384r1
scheme = :secp384r1
aliceP = public "client.pem"
aliceK = privat "client.key"
maximP = public "server.pem"
maximK = privat "server.key"
maximS = :binary.part(shared(aliceP,maximK,scheme),0,32)
aliceS = :binary.part(shared(maximP,aliceK,scheme),0,32)
iv = :crypto.strong_rand_bytes(16)
x = encrypt(:aes_256_gcm, "Success!", maximS, iv)
"Success!" == decrypt(:aes_256_gcm, x, aliceS, iv)

Check warning on line 72 in lib/aes.ex

View workflow job for this annotation

GitHub Actions / build

use of operator '==' has no effect
:ok
end

def check_X25519_GCM256() do # X25519
scheme = :x25519
{aliceP,aliceK} = :crypto.generate_key(:ecdh, scheme)
{maximP,maximK} = :crypto.generate_key(:ecdh, scheme)
maximS = shared(aliceP,maximK,scheme)
aliceS = shared(maximP,aliceK,scheme)
iv = :crypto.strong_rand_bytes(16)
x = encrypt(:aes_256_gcm, "Success!", maximS, iv)
"Success!" == decrypt(:aes_256_gcm, x, aliceS, iv)

Check warning on line 84 in lib/aes.ex

View workflow job for this annotation

GitHub Actions / build

use of operator '==' has no effect
:ok
end

def check_X448_GCM256() do # X488
scheme = :x448
{aliceP,aliceK} = :crypto.generate_key(:ecdh, scheme)
{maximP,maximK} = :crypto.generate_key(:ecdh, scheme)
maximS = :binary.part(shared(aliceP,maximK,scheme),0,32)
aliceS = :binary.part(shared(maximP,aliceK,scheme),0,32)
iv = :crypto.strong_rand_bytes(16)
x = encrypt(:aes_256_gcm, "Success!", maximS, iv)
"Success!" == decrypt(:aes_256_gcm, x, aliceS, iv)

Check warning on line 96 in lib/aes.ex

View workflow job for this annotation

GitHub Actions / build

use of operator '==' has no effect
:ok
end

def check_X448_CBC256() do # X488
scheme = :x448
{aliceP,aliceK} = :crypto.generate_key(:ecdh, scheme)
{maximP,maximK} = :crypto.generate_key(:ecdh, scheme)
maximS = :binary.part(shared(aliceP,maximK,scheme),0,32)
aliceS = :binary.part(shared(maximP,aliceK,scheme),0,32)
x = encrypt(:aes_256_cbc, "Success!", maximS)
"Success!" == decrypt(:aes_256_cbc, x, aliceS)

Check warning on line 107 in lib/aes.ex

View workflow job for this annotation

GitHub Actions / build

use of operator '==' has no effect
:ok
end

def check_X448_ECB256() do # X488
scheme = :x448
{aliceP,aliceK} = :crypto.generate_key(:ecdh, scheme)
{maximP,maximK} = :crypto.generate_key(:ecdh, scheme)
maximS = :binary.part(shared(aliceP,maximK,scheme),0,32)
aliceS = :binary.part(shared(maximP,aliceK,scheme),0,32)
x = encrypt(:aes_256_ecb, "Success!", maximS)
"Success!" == decrypt(:aes_256_ecb, x, aliceS)

Check warning on line 118 in lib/aes.ex

View workflow job for this annotation

GitHub Actions / build

use of operator '==' has no effect
:ok
end


end
9 changes: 4 additions & 5 deletions lib/sec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ defmodule CA.CRYPTO do
def privat(name), do: e(3,:public_key.pem_entry_decode(readPEM("priv/certs/",name)))
def public(name), do: e(3,e(8, e(2, :public_key.pem_entry_decode(readPEM("priv/certs/",name)))))
def readPEM(folder, name), do: hd(:public_key.pem_decode(e(2, :file.read_file(folder <> name))))
def decryptCBC(cipher, secret, iv), do: :crypto.crypto_one_time(:aes_256_cbc,secret,iv,cipher,[{:encrypt,false}])
def shared(pub, key, scheme), do: :crypto.compute_key(:ecdh, pub, key, scheme)
def eccCMS(ukm, len), do: {:'ECC-CMS-SharedInfo', {:'KeyWrapAlgorithm',{2,16,840,1,101,3,4,1,45},:asn1_NOVALUE}, ukm, <<len::32>>}

Expand All @@ -22,7 +21,7 @@ defmodule CA.CRYPTO do
{_,content} = :'CMSECCAlgs-2009-02'.encode(:'ECC-CMS-SharedInfo', eccCMS(ukm, 256))
kdf = KDF.derive(:sha512, sharedKey, 32, content)
unwrap = :aes_kw.unwrap(encryptedKey, kdf)
decryptCBC(data, unwrap, :binary.part(iv,2,16))
CA.AES.decrypt(:aes_256_cbc, data, unwrap, :binary.part(iv,2,16))
end

def testKDF() do
Expand All @@ -37,7 +36,7 @@ defmodule CA.CRYPTO do
unwrap = :aes_kw.unwrap(encryptedKey, kdf)
data = <<128, 196, 25, 250, 68, 103, 198, 72, 197, 203, 5, 173, 43, 24, 212, 147, 239, 124, 5, 57, 164, 158, 133, 227, 90, 54, 162, 115, 41, 2, 71, 129>>
iv = <<97, 97, 144, 119, 183, 207, 197, 200, 142, 1, 201, 219, 173, 207, 63, 20>>
decryptCBC(data, unwrap, iv)
CA.AES.decrypt(:aes_256_cbc, data, unwrap, iv)
end

def testUnwrap() do
Expand All @@ -46,14 +45,14 @@ defmodule CA.CRYPTO do
unwrap = :aes_kw.unwrap(encryptedKey, kdf)
data = <<188, 48, 46, 36, 148, 107, 169, 57, 176, 145, 47, 169, 237, 241, 244, 177, 79, 249, 130, 44, 179, 129, 108, 47, 159, 68, 126, 183, 213, 213, 205, 13>>
iv = <<187, 95, 134, 1, 63, 206, 38, 130, 149, 235, 230, 2, 143, 128, 235, 82>>
decryptCBC(data, unwrap, iv)
CA.AES.decrypt(:aes_256_cbc, data, unwrap, iv)
end

def testDecode() do
data = <<188, 48, 46, 36, 148, 107, 169, 57, 176, 145, 47, 169, 237, 241, 244, 177, 79, 249, 130, 44, 179, 129, 108, 47, 159, 68, 126, 183, 213, 213, 205, 13>>
iv = <<187, 95, 134, 1, 63, 206, 38, 130, 149, 235, 230, 2, 143, 128, 235, 82>>
unwrap = <<7, 54, 202, 106, 82, 159, 14, 38, 154, 188, 199, 36, 41, 123, 161, 56, 142, 171, 46, 246, 62, 18, 243, 1, 140, 31, 48, 224, 138, 166, 53, 36>>
decryptCBC(data, unwrap, iv)
CA.AES.decrypt(:aes_256_cbc, data, unwrap, iv)
end

end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule CA.Mixfile do
def project() do
[
app: :ca,
version: "4.7.1",
version: "4.7.2",
elixir: "~> 1.7",
description: "CA CXC 138 21 Certificate Authority",
package: package(),
Expand Down

0 comments on commit 8b8c71a

Please sign in to comment.