【发布时间】:2015-09-25 10:14:12
【问题描述】:
我正在使用带有 Xcode 7 beta 2 的 OS X 10.10。我想使用来自 OpenSSL 的 AES/GCM。我想从一个例子开始,所以我从OpenSSL wiki 中拿了一个。代码如下。
代码无法编译。编译器似乎找不到以下内容:
EVP_aes_256_gcmEVP_CTRL_GCM_SET_IVLENEVP_CTRL_GCM_GET_TAG
我认为缺少涉及 GCM 模式的项目。我该怎么办?有没有办法更新我的库或我没有导入的东西?
这是我的代码:
#include <openssl/evp.h>
#include <openssl/crypto.h>
#include <openssl/aes.h>
void handleErrors()
{
}
int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *aad,
int aad_len, unsigned char *key, unsigned char *iv,
unsigned char *ciphertext, unsigned char *tag)
{
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
/* Initialise the encryption operation. */
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
handleErrors();
/* Set IV length if default 12 bytes (96 bits) is not appropriate */
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL))
handleErrors();
/* Initialise key and IV */
if(1 != EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv)) handleErrors();
/* Provide any AAD data. This can be called zero or more times as
* required
*/
if(1 != EVP_EncryptUpdate(ctx, NULL, &len, aad, aad_len))
handleErrors();
/* Provide the message to be encrypted, and obtain the encrypted output.
* EVP_EncryptUpdate can be called multiple times if necessary
*/
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
handleErrors();
ciphertext_len = len;
/* Finalise the encryption. Normally ciphertext bytes may be written at
* this stage, but this does not occur in GCM mode
*/
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
ciphertext_len += len;
/* Get the tag */
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag))
handleErrors();
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
int main()
{
}
【问题讨论】:
-
AES GCM 在 1.1+ openssl 分支中。 OS X (10.10.4) 附带的 libcrypto (a) 自 10.7 起已弃用,并且 (b) 仅涵盖 0.9.8zf 版本(根据
openssl version)。看起来 Common Crypto 可能值得研究。 -
嗯...我需要我的代码在 Linux 上具有合理的可移植性... Common Crypto 是仅 OS X 的库吗?没有办法更新我的图书馆吗?
-
更新:从 brew 我管理了看起来像最新版本的 openssl。从命令行,如果我问“openssl 版本”,我现在会得到“OpenSSL 1.0.2c 2015 年 6 月 12 日”。但 SSLeay_version 仍然输出“OpenSSL 0.9.8zf 2015 年 3 月 19 日”。现在实际上找到了我需要的函数和定义,但是链接器抱怨缺少符号。我错过了什么吗?也许包含和库路径在某处有误?
-
CC 仅适用于 OSX(坦率地说,如果它提供 GCM,我会感到惊讶,因为我并没有真正寻找它)。更新到更高版本的 openssl 肯定是合理的,特别是如果其他人已经为您完成了这项工作(看起来像 brew 一样)。您可能必须链接到不同的 dylib,或者只使用静态库,无论哪种情况,都需要使用 brew 安装的版本;不是安装了 OS X 的那个。在那场狩猎中祝你好运,因为我不使用 brew,因此无法说出它被丢弃的位置。
-
好的,所以...当我说“-lcrypto”时,我指的是可能位于我系统中任何位置的 libcrypto.dylib 文件?没有办法追查吗?因为我知道在哪里可以找到新的,由 brew 安装...
标签: c++ xcode macos openssl aes-gcm