urbit/gen164/5/aesc.c

121 lines
2.4 KiB
C
Raw Normal View History

/* j/5/aes.c
**
** This file is in the public domain.
*/
#include "all.h"
#include "../pit.h"
2014-04-20 13:03:26 +04:00
#if defined(U2_OS_osx)
#include <CommonCrypto/CommonCryptor.h>
#else
#include <openssl/aes.h>
2014-04-20 13:03:26 +04:00
#endif
/* functions
*/
2014-09-04 01:33:18 +04:00
u2_noun
u2_cqea_en(u2_atom a, u2_atom b)
{
2014-04-20 13:03:26 +04:00
c3_y a_y[32];
c3_y b_y[16];
#if defined(U2_OS_osx)
2014-04-30 04:19:12 +04:00
size_t siz_i = 0;
2014-04-20 13:03:26 +04:00
#else
AES_KEY key_u;
2014-04-20 13:03:26 +04:00
#endif
c3_assert(u2_cr_met(3, a) <= 32);
c3_assert(u2_cr_met(3, b) <= 16);
2014-04-20 13:03:26 +04:00
u2_cr_bytes(0, 32, a_y, a);
u2_cr_bytes(0, 16, b_y, b);
#if defined(U2_OS_osx)
if ( kCCSuccess != CCCrypt(kCCEncrypt, kCCAlgorithmAES128,
kCCOptionECBMode, a_y, kCCKeySizeAES256, 0, b_y,
2014-04-30 04:19:12 +04:00
16, b_y, 16, &siz_i) )
2014-04-20 13:03:26 +04:00
{
2014-08-20 10:22:41 +04:00
return u2_cm_bail(c3__exit);
2014-04-20 13:03:26 +04:00
}
2014-04-30 04:19:12 +04:00
else c3_assert(16 == siz_i);
2014-04-20 13:03:26 +04:00
#else
if ( 0 != AES_set_encrypt_key(a_y, 256, &key_u) ) {
2014-08-20 10:22:41 +04:00
return u2_cm_bail(c3__exit);
}
else {
2014-04-20 13:03:26 +04:00
AES_encrypt(b_y, b_y, &key_u);
}
2014-04-20 13:03:26 +04:00
#endif
2014-04-20 13:03:26 +04:00
return u2_ci_bytes(16, b_y);
}
2014-09-04 01:33:18 +04:00
u2_noun
u2_cwea_en(u2_noun cor)
{
u2_noun a, b;
2014-08-20 10:22:41 +04:00
if ( u2_no == u2_cr_mean(cor, u2_cv_sam_2, &a, u2_cv_sam_3, &b, 0) ||
u2_no == u2ud(a) ||
u2_no == u2ud(b) )
{
2014-08-20 10:22:41 +04:00
return u2_cm_bail(c3__exit);
}
else {
2014-09-04 01:33:18 +04:00
return u2_cqea_en(a, b);
}
}
2014-09-04 01:33:18 +04:00
u2_noun
u2_cqea_de(u2_atom a, u2_atom b)
{
2014-04-20 13:03:26 +04:00
c3_y a_y[32];
c3_y b_y[16];
#if defined(U2_OS_osx)
2014-04-30 04:19:12 +04:00
size_t siz_i = 0;
2014-04-20 13:03:26 +04:00
#else
AES_KEY key_u;
2014-04-20 13:03:26 +04:00
#endif
c3_assert(u2_cr_met(3, a) <= 32);
c3_assert(u2_cr_met(3, b) <= 16);
2014-04-20 13:03:26 +04:00
u2_cr_bytes(0, 32, a_y, a);
u2_cr_bytes(0, 16, b_y, b);
#if defined(U2_OS_osx)
if ( kCCSuccess != CCCrypt(kCCDecrypt, kCCAlgorithmAES128,
kCCOptionECBMode, a_y, kCCKeySizeAES256, 0, b_y,
2014-04-30 04:19:12 +04:00
16, b_y, 16, &siz_i) )
2014-04-20 13:03:26 +04:00
{
2014-08-20 10:22:41 +04:00
return u2_cm_bail(c3__exit);
2014-04-20 13:03:26 +04:00
}
2014-04-30 04:19:12 +04:00
else c3_assert(16 == siz_i);
2014-04-20 13:03:26 +04:00
#else
if ( 0 != AES_set_decrypt_key(a_y, 256, &key_u) ) {
2014-08-20 10:22:41 +04:00
return u2_cm_bail(c3__exit);
}
else {
2014-04-20 13:03:26 +04:00
AES_decrypt(b_y, b_y, &key_u);
}
2014-04-20 13:03:26 +04:00
#endif
2014-04-20 13:03:26 +04:00
return u2_ci_bytes(16, b_y);
}
2014-09-04 01:33:18 +04:00
u2_noun
u2_cwea_de(u2_noun cor)
{
u2_noun a, b;
2014-08-20 10:22:41 +04:00
if ( u2_no == u2_cr_mean(cor, u2_cv_sam_2, &a, u2_cv_sam_3, &b, 0) ||
u2_no == u2ud(a) ||
u2_no == u2ud(b) )
{
2014-08-20 10:22:41 +04:00
return u2_cm_bail(c3__exit);
}
else {
2014-09-04 01:33:18 +04:00
return u2_cqea_de(a, b);
}
}