Skip to content
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

The way to use AES. #10

Closed
sunoru opened this issue Jul 6, 2016 · 3 comments
Closed

The way to use AES. #10

sunoru opened this issue Jul 6, 2016 · 3 comments

Comments

@sunoru
Copy link
Member

sunoru commented Jul 6, 2016

AES encryption is an important part of the RNGs for secure use. There are some methods to use AES:

  1. Implement the encryption algorithm in pure Julia. There are also existing packages such as AES.jl (but this is not registered), and Nettle.jl (though this also provides many other functions).
  2. Use the mature OpenSSL. This may also have two different ways: to wrap it by ourselves or use the existing ones (for example, Crypto.jl, but it is not completed so doesn't guarantee the security)...

It would be better if we can directly use AES-NI on the most kinds of CPU, so I'm also wondering how will llvm do with this? Anyway, OpenSSL should have been already optimized about such things.

@sunoru
Copy link
Member Author

sunoru commented Jul 22, 2016

When trying to use the AES intrinsics (as https://github.com/frasercrmck/llvm-leg/blob/master/test/CodeGen/X86/aes_intrinsics.ll), I found for now the llvmcall only accepts array ABI such as [2 x i64] (JuliaLang/julia@a93e9a1), but to use the intrinsics it's necessary to pass <2 x i64>

julia> function aesenc(x::Tuple{UInt64, UInt64}, y::Tuple{UInt64, UInt64})
           Base.llvmcall(("""declare <2 x i64> @llvm.x86.aesni.aesenc(<2 x i64>, <2 x i64>)""","""
               %3 = call <2 x i64> @llvm.x86.aesni.aesenc(<2 x i64> %0, <2 x i64> %1)
           ret <2 x i64> %3"""), Tuple{UInt64, UInt64}, Tuple{Tuple{UInt64, UInt64}, Tuple{UInt64, UInt64}}, x, y)
       end
aesenc (generic function with 1 method)

julia> aesenc((1 % UInt64, 1 % UInt64), (1 % UInt64, 1 % UInt64))
ERROR: error compiling aesenc: Failed to parse LLVM Assembly: 
julia: llvmcall:5:58: error: '%0' defined with type '[2 x i64]'
    %3 = call <2 x i64> @llvm.x86.aesni.aesenc(<2 x i64> %0, <2 x i64> %1)
                                                         ^

 in eval(::Module, ::Any) at ./boot.jl:231
 in macro expansion at ./REPL.jl:92 [inlined]
 in (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:46

@simonbyrne
Copy link

simonbyrne commented Jul 22, 2016

Sorry we haven't had a chance to chat recently, I've been travelling a lot. The distinction here is between what LLVM terms vectors and arrays. From what I understand (which may well be wrong), vectors are what can be used with SIMD instructions, whereas arrays are more general structs.

In 0.5 you can use VecElement introduced by JuliaLang/julia#15244 inside Tuples:

julia> function aesenc(x, y)
                  Base.llvmcall(("""declare <2 x i64> @llvm.x86.aesni.aesenc(<2 x i64>, <2 x i64>)""","""
                      %3 = call <2 x i64> @llvm.x86.aesni.aesenc(<2 x i64> %0, <2 x i64> %1)
                  ret <2 x i64> %3"""), NTuple{2,VecElement{UInt64}}, Tuple{NTuple{2,VecElement{UInt64}},NTuple{2,VecElement{UInt64}}}, x, y)
              end
aesenc (generic function with 1 method)

julia> aesenc((VecElement(1 % UInt64), VecElement(1 % UInt64)), (VecElement(1 % UInt64), VecElement(1 % UInt64)))
(VecElement{UInt64}(0x63636363427c7c5c),VecElement{UInt64}(0x63636363427c7c5c))

julia> @code_native aesenc((VecElement(1 % UInt64), VecElement(1 % UInt64)), (VecElement(1 % UInt64), VecElement(1 % UInt64)))
    .section    __TEXT,__text,regular,pure_instructions
Filename: REPL[1]
    pushq   %rbp
    movq    %rsp, %rbp
Source line: 2
    vaesenc %xmm1, %xmm0, %xmm0
    popq    %rbp
    retq
    nopl    (%rax,%rax)

@sunoru
Copy link
Member Author

sunoru commented Jul 23, 2016

Thanks, that's what I need.

@sunoru sunoru closed this as completed Feb 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants