기존에 사용하는 aes를 aes-ni로 바꾸고싶은데 잘 모르겠네요

wonjnlee의 이미지

안녕하세요.

aes-ni를 공부하고, 이를 기존에 동작하던 aes 시스템에 적용하려고 하는데.. 잘 안되네요.
우선 기존 aes는 openssl에서 사용되는 라이브러리를 이용했습니다.
문제는 이 openssl의 aes를 intel에서 지원하는 aes-ni로 어떻게 '대치'하느냐가 좀 어렵네요 (사실 이해가 안가서..)

aes-ni 소스코드는 intel 홈페이지에서 얻었는데요.
(인텔 홈페이지 가면 무료로 다운받을 수 있습니다: https://software.intel.com/en-us/articles/download-the-intel-aesni-sample-library)
예시로 128비트 cbc로 동작하는 소스는 다음과 같습니다.

int test128_CBC(unsigned long numBlocks)
{
unsigned int buffer_size = numBlocks * BLOCK_SIZE;
unsigned int i;
UCHAR *testVector = (UCHAR*)_alloca(buffer_size);
UCHAR *testResult = (UCHAR*)_alloca(buffer_size);
UCHAR local_test_iv[BLOCK_SIZE];
unsigned int half_size;

// Init the test vector and the test result
for (i=0;i {
testVector[i] = test_plain_text[i];
testResult[i] = 0xee;
}

// Init the Initialization Vector
memcpy(local_test_iv,test_init_vector,BLOCK_SIZE);

intel_AES_enc128_CBC(testVector, testResult, test_key_128, numBlocks, local_test_iv);

// check the encrypted buffer against the known cipher text
for (i=0;i {
if (testResult[i] != test_cipher_128_cbc[i])
{
printf("%d",i);
return TEST_FAIL_DEC;
}
}

//test chaining as well
memcpy(local_test_iv,test_init_vector,BLOCK_SIZE);
half_size = numBlocks/2;
intel_AES_dec128_CBC(testResult,testVector,test_key_128,half_size,local_test_iv);
intel_AES_dec128_CBC(testResult+BLOCK_SIZE*(half_size),testVector+BLOCK_SIZE*(half_size),test_key_128,numBlocks - half_size,local_test_iv);

for (i=0;i {
if (testVector[i] != test_plain_text[i])
{
printf("%d",i);
return TEST_FAIL_DEC;
}
}

return TEST_PASS;
}

그리고 기존 openssl에서 쓰던 aes_cbc 128비트는 다음과 같거든요.

AES_cbc_encrypt(inbuf, outbuf, inlength, key, iv, AES_ENCRYPT);

문제는 위에 있는 소스코드에서 보이는

intel_AES_enc128_CBC(testVector, testResult, test_key_128, numBlocks, local_test_iv);

여기에 testvector, numblocks 등등 이런게 기존에 있는거랑 뭐가 호환되는건지를 모르겠네여..
벡터가 왜 또 나오는지도 모르겠고ㅠㅠ 도움 부탁드립니다.

익명 사용자의 이미지

wonjnlee의 이미지

코드를 수정할필요없이 cpu가 자동으로 aes-ni로 암복호화를 하나요?

ymir의 이미지

intel_AES_enc128_CBC() 의 prototype 을 보세요.
먼저 parameter 를 확인하고, 그 담에 argument 가 어떻게 전달되는지를 봐야죠.

void intel_AES_enc128_CBC(UCHAR *plainText, UCHAR *cipherText, UCHAR *key, size_t numBlocks, UCHAR *iv);
void intel_AES_dec128_CBC(UCHAR *cipherText, UCHAR *plainText, UCHAR *key, size_t numBlocks, UCHAR *iv);

그리고 AES_cbc_encrypt() 는 다음과 같이 생겼으니..
잘 보면 한 번 더 래핑했을 뿐, parameter 가 같다는 걸 알 수 있을 겁니다.

void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
        size_t len, const AES_KEY *key, unsigned char *ivec, const int enc)
{
    if (enc)
        CRYPTO_cbc128_encrypt(in, out, len, key, ivec, (block128_f) AES_encrypt);
    else
        CRYPTO_cbc128_decrypt(in, out, len, key, ivec, (block128_f) AES_decrypt);
}

그리고 OpenSSL 의 crypto module 에 있는 aes 만 떼와서 다른 암호 모듈에 붙인거라면..
aesni 는 crypto/aes/asm/aesni-*.pl 에 perl 로 구현되어 있습니다.
그걸 실행해서 어셈 코드를 생성한 다음에, 그걸 빌드해서 링크합니다.

$ fcn aesni-\*.pl
./crypto/aes/asm/aesni-x86_64.pl
./crypto/aes/asm/aesni-sha1-x86_64.pl
./crypto/aes/asm/aesni-sha256-x86_64.pl
./crypto/aes/asm/aesni-x86.pl
./crypto/aes/asm/aesni-mb-x86_64.pl
./crypto/modes/asm/aesni-gcm-x86_64.pl
$ fcn aesni-\*.s
./crypto/aes/aesni-x86_64.s
./crypto/aes/aesni-mb-x86_64.s
./crypto/aes/aesni-sha256-x86_64.s
./crypto/aes/aesni-sha1-x86_64.s
./crypto/modes/aesni-gcm-x86_64.s

관련 함수들에 대한 prototype 은 crypto/evp/e_aes.c 에서 확인해 보세요.

/*
 * AES-NI section
 */
#  define AESNI_CAPABLE   (OPENSSL_ia32cap_P[1]&(1<<(57-32)))
 
int aesni_set_encrypt_key(const unsigned char *userKey, int bits, AES_KEY *key);
int aesni_set_decrypt_key(const unsigned char *userKey, int bits, AES_KEY *key);
...

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.