shrub/jets/e/aesc.c

120 lines
2.3 KiB
C
Raw Normal View History

/* j/5/aes.c
**
*/
#include "all.h"
2014-09-04 07:10:43 +04:00
#if defined(U3_OS_osx)
2014-04-20 13:03:26 +04:00
#include <CommonCrypto/CommonCryptor.h>
#else
#include <openssl/aes.h>
2014-04-20 13:03:26 +04:00
#endif
/* functions
*/
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3qea_en(u3_atom a, u3_atom b)
{
2014-04-20 13:03:26 +04:00
c3_y a_y[32];
c3_y b_y[16];
#if defined(U3_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
2014-11-06 03:20:01 +03:00
c3_assert(u3r_met(3, a) <= 32);
c3_assert(u3r_met(3, b) <= 16);
2014-11-06 03:20:01 +03:00
u3r_bytes(0, 32, a_y, a);
u3r_bytes(0, 16, b_y, b);
2014-04-20 13:03:26 +04:00
#if defined(U3_OS_osx)
2014-04-20 13:03:26 +04:00
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-11-06 03:20:01 +03:00
return u3m_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-11-06 03:20:01 +03:00
return u3m_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-11-06 03:20:01 +03:00
return u3i_bytes(16, b_y);
}
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3wea_en(u3_noun cor)
{
2014-09-06 00:13:24 +04:00
u3_noun a, b;
2014-11-18 00:56:51 +03:00
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0) ||
2014-11-05 04:18:47 +03:00
c3n == u3ud(a) ||
c3n == u3ud(b) )
{
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__exit);
}
else {
2014-11-06 22:13:57 +03:00
return u3qea_en(a, b);
}
}
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3qea_de(u3_atom a, u3_atom b)
{
2014-04-20 13:03:26 +04:00
c3_y a_y[32];
c3_y b_y[16];
#if defined(U3_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
2014-11-06 03:20:01 +03:00
c3_assert(u3r_met(3, a) <= 32);
c3_assert(u3r_met(3, b) <= 16);
2014-11-06 03:20:01 +03:00
u3r_bytes(0, 32, a_y, a);
u3r_bytes(0, 16, b_y, b);
2014-04-20 13:03:26 +04:00
#if defined(U3_OS_osx)
2014-04-20 13:03:26 +04:00
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-11-06 03:20:01 +03:00
return u3m_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-11-06 03:20:01 +03:00
return u3m_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-11-06 03:20:01 +03:00
return u3i_bytes(16, b_y);
}
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3wea_de(u3_noun cor)
{
2014-09-06 00:13:24 +04:00
u3_noun a, b;
2014-11-18 00:56:51 +03:00
if ( c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0) ||
2014-11-05 04:18:47 +03:00
c3n == u3ud(a) ||
c3n == u3ud(b) )
{
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__exit);
}
else {
2014-11-06 22:13:57 +03:00
return u3qea_de(a, b);
}
}