static int rj_decrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res) { struct int_ctx *cx = (struct int_ctx *) c->ptr; if (!cx->is_init) if (rj_real_init(cx, 0)) return PXE_CIPHER_INIT; if (dlen == 0) return 0; if (dlen & 15) return PXE_NOTBLOCKSIZE; memcpy(res, data, dlen); if (cx->mode == MODE_CBC) { aes_cbc_decrypt(&cx->ctx.rj, cx->iv, res, dlen); memcpy(cx->iv, data + dlen - 16, 16); } else aes_ecb_decrypt(&cx->ctx.rj, res, dlen); return 0; }
static void AES_ecb_encrypt(const uint8 *src, uint8 *dst, AES_KEY *ctx, int enc) { memcpy(dst, src, 16); if (enc) aes_ecb_encrypt(ctx, dst, 16); else aes_ecb_decrypt(ctx, dst, 16); }
int main(int argc, char *argv[]) { const char indata[] = "MyBloodyValentinMyBloodyValentin"; unsigned char data[32768]; char hex[256], base64[256]; int len; unsigned char iv[] = { 'Y', 'E', 'L', 'L', 'O', 'W', ' ', 'S', 'U', 'B', 'M', 'A', 'R', 'I', 'N', 'E' }; unsigned char key[] = { 'Y', 'E', 'L', 'L', 'O', 'W', ' ', 'S', 'U', 'B', 'M', 'A', 'R', 'I', 'N', 'E' }; len = strlen(indata); printf("// testing aes-128-ecb\n"); printf("Plaintext:\n"); hexdump(indata, len); memcpy(data, indata, len); aes_ecb_encrypt(data, len, key, sizeof(key)); printf("Ciphertext:\n"); hexdump(data, 16); base64[base64encode(data, len, base64)] = '\0'; printf("base64: %s\n", base64); aes_ecb_decrypt(data, len, key, sizeof(key)); data[len] = '\0'; printf("Decrypted plaintext: %s\n", data); hexdump(data, len); printf("// testing aes-128-cbc\n"); printf("Plaintext:\n"); hexdump(indata, len); memcpy(data, indata, len); aes_cbc_encrypt(data, len, key, sizeof(key), iv); printf("Ciphertext:\n"); hexdump(data, 16); base64[base64encode(data, len, base64)] = '\0'; printf("base64: %s\n", base64); aes_cbc_decrypt(data, len, key, sizeof(key), iv); data[len] = '\0'; printf("Decrypted plaintext: %s\n", data); hexdump(data, len); return 0; }
int main(int argc, char *argv[]) { unsigned char encrypted[padded_message_size]; unsigned char decrypted[padded_message_size]; memset(decrypted, 0, sizeof(decrypted)); memcpy(decrypted, message, sizeof(message)); aes_ecb_encrypt(decrypted, encrypted, padded_message_size, key_size * 8, key); memset(decrypted, 0, padded_message_size); aes_ecb_decrypt(encrypted, decrypted, padded_message_size, key_size * 8, key); printf("%s", decrypted); return 0; }
OSStatus AES_ECB_Update( AES_ECB_Context *inContext, const void *inSrc, size_t inLen, void *inDst ) { OSStatus err; const uint8_t * src; uint8_t * dst; size_t n; // inSrc and inDst may be the same, but otherwise, the buffers must not overlap. #if( DEBUG ) if( inSrc != inDst ) check_ptr_overlap( inSrc, inLen, inDst, inLen ); if( ( inLen % kAES_ECB_Size ) != 0 ) aes_log( "ECB doesn't support non-block-sized operations (%d bytes)", (int)inLen ); #endif src = (const uint8_t *) inSrc; dst = (uint8_t *) inDst; for( n = inLen / kAES_ECB_Size; n > 0; --n ) { #if( AES_UTILS_USE_COMMON_CRYPTO ) size_t len; err = CCCryptorUpdate( inContext->cryptor, src, kAES_ECB_Size, dst, kAES_ECB_Size, &len ); require_noerr( err, exit ); check( len == kAES_ECB_Size ); #elif( AES_UTILS_USE_GLADMAN_AES ) if( inContext->encrypt ) aes_ecb_encrypt( src, dst, kAES_ECB_Size, &inContext->ctx.encrypt ); else aes_ecb_decrypt( src, dst, kAES_ECB_Size, &inContext->ctx.decrypt ); #elif( AES_UTILS_USE_USSL ) aes_crypt_ecb( &inContext->ctx, inContext->mode, (unsigned char *) src, dst ); #else inContext->cryptFunc( src, dst, &inContext->key ); #endif src += kAES_ECB_Size; dst += kAES_ECB_Size; } err = kNoErr; #if( AES_UTILS_USE_COMMON_CRYPTO ) exit: #endif return( err ); }
static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst, struct scatterlist *src, unsigned int nbytes) { struct crypto_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); int err, first, rounds = 6 + ctx->key_length / 4; struct blkcipher_walk walk; unsigned int blocks; desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP; blkcipher_walk_init(&walk, dst, src, nbytes); err = blkcipher_walk_virt(desc, &walk); kernel_neon_begin(); for (first = 1; (blocks = (walk.nbytes / AES_BLOCK_SIZE)); first = 0) { aes_ecb_decrypt(walk.dst.virt.addr, walk.src.virt.addr, (u8 *)ctx->key_dec, rounds, blocks, first); err = blkcipher_walk_done(desc, &walk, walk.nbytes % AES_BLOCK_SIZE); } kernel_neon_end(); return err; }
mr_unsign32 aes_decrypt(aes *a,char *buff) { int j,bytes; char st[16]; mr_unsign32 fell_off; /* Supported modes of operation */ fell_off=0; switch (a->mode) { case MR_ECB: aes_ecb_decrypt(a,(MR_BYTE *)buff); return 0; case MR_CBC: for (j=0;j<4*NB;j++) { st[j]=a->f[j]; a->f[j]=buff[j]; } aes_ecb_decrypt(a,(MR_BYTE *)buff); for (j=0;j<4*NB;j++) { buff[j]^=st[j]; st[j]=0; } return 0; case MR_CFB1: case MR_CFB2: case MR_CFB4: bytes=a->mode-MR_CFB1+1; for (j=0;j<bytes;j++) fell_off=(fell_off<<8)|a->f[j]; for (j=0;j<4*NB;j++) st[j]=a->f[j]; for (j=bytes;j<4*NB;j++) a->f[j-bytes]=a->f[j]; aes_ecb_encrypt(a,(MR_BYTE *)st); for (j=0;j<bytes;j++) { a->f[16-bytes+j]=buff[j]; buff[j]^=st[j]; } return fell_off; case MR_OFB1: case MR_OFB2: case MR_OFB4: case MR_OFB8: case MR_OFB16: bytes=a->mode-MR_OFB1+1; aes_ecb_encrypt(a,(MR_BYTE *)(a->f)); for (j=0;j<bytes;j++) buff[j]^=a->f[j]; return 0; case MR_PCFB1: /* error propagating CFB */ case MR_PCFB2: case MR_PCFB4: bytes=a->mode-MR_PCFB1+1; for (j=0;j<bytes;j++) fell_off=(fell_off<<8)|a->f[j]; for (j=0;j<4*NB;j++) st[j]=a->f[j]; for (j=bytes;j<4*NB;j++) a->f[j-bytes]=a->f[j]; aes_ecb_encrypt(a,(MR_BYTE *)st); for (j=0;j<bytes;j++) { a->f[16-bytes+j]=buff[j]^st[16-bytes+j]; buff[j]^=st[j]; } return fell_off; default: return 0; } }
int main(){ unsigned char key128[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}; unsigned char key192[24] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17}; unsigned char key256[32] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}; unsigned char ct[16]; unsigned char vt[16]; unsigned char pt[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}; symmetric_key skey128; symmetric_key skey192; symmetric_key skey256; /* AES 128 */ if (aes_setup(key128, 16, 10, &skey128) != CRYPT_OK){ printf("ERROR: in %s, unable to setup AES 128 key\n", __func__); return 0; } if (aes_ecb_encrypt(pt, ct, &skey128) != CRYPT_OK){ printf("ERROR: in %s, unable to encrypt AES 128\n", __func__); } printf("Plaintext: "); fprintBuffer_raw(stdout, (char*)pt, 16); printf("\nKey 128: "); fprintBuffer_raw(stdout, (char*)key128, 16); printf("\nCiphertext 128: "); fprintBuffer_raw(stdout, (char*)ct, 16); if (aes_ecb_decrypt(ct, vt, &skey128) != CRYPT_OK){ printf("ERROR: in %s, unable to decrypt AES 128\n", __func__); } if (!memcmp(vt, pt, 16)){ printf("\nRecovery 128: OK\n"); } else{ printf("\nRecovery 128: FAIL\n"); } aes_done(&skey128); /* AES 192 */ if (aes_setup(key192, 24, 12, &skey192) != CRYPT_OK){ printf("ERROR: in %s, unable to setup AES 192 key\n", __func__); return 0; } if (aes_ecb_encrypt(pt, ct, &skey192) != CRYPT_OK){ printf("ERROR: in %s, unable to encrypt AES 192\n", __func__); } printf("Key 192: "); fprintBuffer_raw(stdout, (char*)key192, 24); printf("\nCiphertext 192: "); fprintBuffer_raw(stdout, (char*)ct, 16); if (aes_ecb_decrypt(ct, vt, &skey192) != CRYPT_OK){ printf("ERROR: in %s, unable to decrypt AES 192\n", __func__); } if (!memcmp(vt, pt, 16)){ printf("\nRecovery 192: OK\n"); } else{ printf("\nRecovery 192: FAIL\n"); } aes_done(&skey192); /* AES 256 */ if (aes_setup(key256, 32, 14, &skey256) != CRYPT_OK){ printf("ERROR: in %s, unable to setup AES 256 key\n", __func__); return 0; } if (aes_ecb_encrypt(pt, ct, &skey256) != CRYPT_OK){ printf("ERROR: in %s, unable to encrypt AES 256\n", __func__); } printf("Key 256: "); fprintBuffer_raw(stdout, (char*)key256, 32); printf("\nCiphertext 256: "); fprintBuffer_raw(stdout, (char*)ct, 16); if (aes_ecb_decrypt(ct, vt, &skey256) != CRYPT_OK){ printf("ERROR: in %s, unable to decrypt AES 256\n", __func__); } if (!memcmp(vt, pt, 16)){ printf("\nRecovery 256: OK\n"); } else{ printf("\nRecovery 256: FAIL\n"); } aes_done(&skey256); return 0; }
void decrypt() { aes_ecb_decrypt(buf, key); }