Skip to content

Commit

Permalink
Merge pull request #10 from kanav99/rename
Browse files Browse the repository at this point in the history
Rename to AES
  • Loading branch information
kanav99 authored Apr 29, 2020
2 parents 7ca7afd + c8e9a70 commit 7dcef2c
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name = "Rijndael"
name = "AES"
uuid = "5a3fb44e-d90d-11e9-053b-99581e1bac63"
authors = ["Kanav Gupta <[email protected]>"]
version = "0.1.0"
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Rijndael.jl
# AES.jl

[![Build Status](https://travis-ci.org/kanav99/Rijndael.jl.svg?branch=master)](https://travis-ci.org/kanav99/Rijndael.jl)
[![Build Status](https://travis-ci.org/kanav99/AES.jl.svg?branch=master)](https://travis-ci.org/kanav99/AES.jl)

AES On-the-Fly mode in Julia

## API
```julia
using Rijndael
using AES

# make a new key
k = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
key = AES128Key(k)

# make a cipher object
cipher = AES(;key_length=128, mode=Rijndael.CBC, key=key)
cipher = AESCipher(;key_length=128, mode=AES.CBC, key=key)

# encrypt!
plaintext = "The quick brown fox jumps over the lazy dog."
Expand Down
4 changes: 2 additions & 2 deletions src/Rijndael.jl → src/AES.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Rijndael
module AES

using StaticArrays, Random

Expand All @@ -19,7 +19,7 @@ module Rijndael
include("encrypt.jl")
include("decrypt.jl")

export AES
export AESCipher
export AES128Key, AES192Key, AES256Key
export AESCache
export encrypt, decrypt
Expand Down
4 changes: 2 additions & 2 deletions src/decrypt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
Common interface for all modes and key lengths
"""
function decrypt(ciphertext::AESCipherText, cipher::AES; remove_pad=true)
function decrypt(ciphertext::AESCipherText, cipher::AESCipher; remove_pad=true)
if ciphertext.mode !== get_mode(cipher)
error("Mismatching mode and cipher")
end
Expand All @@ -19,7 +19,7 @@ function decrypt(ciphertext::AESCipherText, cipher::AES; remove_pad=true)
return (ciphertext.original_type)(raw)
end

function decrypt!(plaintext, ciphertext::AESCipherText, cipher::AES; remove_pad=true)
function decrypt!(plaintext, ciphertext::AESCipherText, cipher::AESCipher; remove_pad=true)
if ciphertext.mode !== get_mode(cipher)
error("Mismatching mode and cipher")
end
Expand Down
4 changes: 2 additions & 2 deletions src/encrypt.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Common interface for all modes and key lengths
"""
function encrypt(plaintext::Union{String,Array{UInt8}}, cipher::AES;
function encrypt(plaintext::Union{String,Array{UInt8}}, cipher::AESCipher;
iv=needs_iv(cipher) ? rand(UInt8, 16) : nothing)
if iscbc(cipher)
ciphertext = AESCBC(plaintext, iv, cipher.key, cipher.cache)
Expand All @@ -13,7 +13,7 @@ function encrypt(plaintext::Union{String,Array{UInt8}}, cipher::AES;
return AESCipherText(ciphertext, iv, get_key_length(cipher), get_mode(cipher), typeof(plaintext))
end

function encrypt!(ciphertext, plaintext::Union{String,Array{UInt8}}, cipher::AES;
function encrypt!(ciphertext, plaintext::Union{String,Array{UInt8}}, cipher::AESCipher;
iv=needs_iv(cipher) ? rand(UInt8, 16) : nothing)
if iscbc(cipher)
AESCBC!(ciphertext, plaintext, iv, cipher.key, cipher.cache)
Expand Down
20 changes: 10 additions & 10 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ end

"""
"""
mutable struct AES{mode,cacheType,keyType} <: AbstractCipher
mutable struct AESCipher{mode,cacheType,keyType} <: AbstractCipher
cache::cacheType
key::keyType
end

function AES(;key_length=128,mode=CBC,key=keygen(key_length))
function AESCipher(;key_length=128,mode=CBC,key=keygen(key_length))
if !is_valid_key_length(key_length)
error("$key_length is an invalid key length. Key length can be 128, 196 or 256.")
end
Expand All @@ -91,17 +91,17 @@ function AES(;key_length=128,mode=CBC,key=keygen(key_length))

cache = gen_cache(aes_key, mode)

AES{mode,typeof(cache),typeof(aes_key)}(cache,aes_key)
AESCipher{mode,typeof(cache),typeof(aes_key)}(cache,aes_key)
end

@inline isecb(cipher::AES{m,c,k}) where {m,c,k} = m == ECB
@inline iscbc(cipher::AES{m,c,k}) where {m,c,k} = m == CBC
@inline iscfb(cipher::AES{m,c,k}) where {m,c,k} = m == CFB
@inline isofb(cipher::AES{m,c,k}) where {m,c,k} = m == OFB
@inline isctr(cipher::AES{m,c,k}) where {m,c,k} = m == CTR
@inline isecb(cipher::AESCipher{m,c,k}) where {m,c,k} = m == ECB
@inline iscbc(cipher::AESCipher{m,c,k}) where {m,c,k} = m == CBC
@inline iscfb(cipher::AESCipher{m,c,k}) where {m,c,k} = m == CFB
@inline isofb(cipher::AESCipher{m,c,k}) where {m,c,k} = m == OFB
@inline isctr(cipher::AESCipher{m,c,k}) where {m,c,k} = m == CTR

@inline get_mode(cipher::AES{m,c,k}) where {m,c,k} = m
@inline get_key_length(cipher::AES) = get_key_length(cipher.key)
@inline get_mode(cipher::AESCipher{m,c,k}) where {m,c,k} = m
@inline get_key_length(cipher::AESCipher) = get_key_length(cipher.key)

@inline needs_iv(cipher) = iscbc(cipher) || isctr(cipher)

Expand Down
26 changes: 13 additions & 13 deletions test/blocktest.jl
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
using Rijndael, BenchmarkTools, Test
using AES, BenchmarkTools, Test

# AES128
k=[0x54,0x68,0x61,0x74,0x73,0x20,0x6D,0x79,0x20,0x4B,0x75,0x6E,0x67,0x20,0x46,0x75]
p=[0x54,0x77,0x6F,0x20,0x4F,0x6E,0x65,0x20,0x4E,0x69,0x6E,0x65,0x20,0x54,0x77,0x6F]
c=[0x29,0xC3,0x50,0x5F,0x57,0x14,0x20,0xF6,0x40,0x22,0x99,0xB3,0x1A,0x02,0xD7,0x3A]
key = AES128Key(k)

@test c == Rijndael.AESEncryptBlock(p, key)
@test p == Rijndael.AESDecryptBlock(c, key)
@test p == Rijndael.AESDecryptBlock(Rijndael.AESEncryptBlock(p,key),key)
@test c == AES.AESEncryptBlock(p, key)
@test p == AES.AESDecryptBlock(c, key)
@test p == AES.AESDecryptBlock(AES.AESEncryptBlock(p,key),key)

# Performance Checks
cache = Rijndael.gen_cache(key)
@btime Rijndael.AESEncryptBlock!(p, p, key, cache)
@btime Rijndael.AESDecryptBlock!(c, c, key, cache)
cache = AES.gen_cache(key)
@btime AES.AESEncryptBlock!(p, p, key, cache)
@btime AES.AESDecryptBlock!(c, c, key, cache)

# AES192
k = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17]
p = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF]
c = [0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0, 0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91]
key = AES192Key(k)

@test c == Rijndael.AESEncryptBlock(p, key)
@test p == Rijndael.AESDecryptBlock(c, key)
@test p == Rijndael.AESDecryptBlock(Rijndael.AESEncryptBlock(p,key),key)
@test c == AES.AESEncryptBlock(p, key)
@test p == AES.AESDecryptBlock(c, key)
@test p == AES.AESDecryptBlock(AES.AESEncryptBlock(p,key),key)

# Performance Checks
cache = Rijndael.gen_cache(key)
@btime Rijndael.AESEncryptBlock!(p, p, key, cache)
@btime Rijndael.AESDecryptBlock!(c, c, key, cache)
cache = AES.gen_cache(key)
@btime AES.AESEncryptBlock!(p, p, key, cache)
@btime AES.AESDecryptBlock!(c, c, key, cache)
4 changes: 2 additions & 2 deletions test/cbc.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Rijndael, Test
using AES, Test

k = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
key = AES128Key(k)

plaintext = "The quick brown fox jumps over the lazy dog."
iv = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]

cipher = AES(;key_length=128, mode=Rijndael.CBC, key=key)
cipher = AESCipher(;key_length=128, mode=AES.CBC, key=key)
ct = encrypt(plaintext, cipher; iv=iv)
pt = decrypt(ct, cipher)

Expand Down
4 changes: 2 additions & 2 deletions test/ctr.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Rijndael, Test
using AES, Test

k = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
key = AES128Key(k)

plaintext = "The quick brown fox jumps over the lazy dog."
iv = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]

cipher = AES(;key_length=128, mode=Rijndael.CTR, key=key)
cipher = AESCipher(;key_length=128, mode=AES.CTR, key=key)
ct = encrypt(plaintext, cipher; iv=iv)
pt = decrypt(ct, cipher)

Expand Down
4 changes: 2 additions & 2 deletions test/ecb.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Rijndael, Test
using AES, Test

k = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
key = AES128Key(k)

plaintext = "The quick brown fox jumps over the lazy dog."

cipher = AES(;key_length=128, mode=Rijndael.ECB, key=key)
cipher = AESCipher(;key_length=128, mode=AES.ECB, key=key)
ct = encrypt(plaintext, cipher)
pt = decrypt(ct, cipher)

Expand Down

2 comments on commit 7dcef2c

@kanav99
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/13857

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.0 -m "<description of version>" 7dcef2c35badb1a34a5a29c5c92179a23fd504d8
git push origin v0.1.0

Please sign in to comment.