Skip to content

Instantly share code, notes, and snippets.

@00xc

00xc/solve.c Secret

Created October 24, 2021 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 00xc/76b397157a9eed3fd49f43a6988aa63d to your computer and use it in GitHub Desktop.
Save 00xc/76b397157a9eed3fd49f43a6988aa63d to your computer and use it in GitHub Desktop.
ASIS CTF Quals 2021 - Beans Talk solver
/*
* Compile with: cc solve.c -Wall -Wextra -Wpedantic -O3 -o solve -lz3
*/
#include <assert.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <z3.h>
typedef unsigned char byte;
typedef unsigned int uint;
typedef unsigned short ushort;
#define NUM_BLOCKS 6
#define BLOCK_SIZE 8
#define NUM_MAPS 10
#define MAP_SIZE 256
#define ARR_SIZE(x) sizeof(x)/sizeof(x[0])
typedef struct {
ushort enc[4];
int rand[32];
unsigned int rpos;
} state_t;
byte encrypted[] = { 0x46, 0x9d, 0xfa, 0x32, 0x51, 0xe2, 0x65, 0xf4, 0x80, 0xc6, 0xbe, 0xb3, 0xc6, 0x6e, 0x7e, 0x3c, 0x65, 0xc1, 0x35, 0xe0, 0x11, 0x19, 0x0d, 0x86, 0x2e, 0x93, 0xfe, 0xea, 0xd6, 0x67, 0xd7, 0xb1, 0xcd, 0xec, 0x52, 0xe4, 0x53, 0x3e, 0x3b, 0xe1, 0x0a, 0xfd, 0x50, 0x7e, 0xb4, 0xf8, 0xd0, 0x43 };
const byte map[][256] = {
{ 223, 10, 160, 2, 241, 23, 104, 96, 183, 18, 195, 122, 250, 233, 83, 61, 132, 150, 186, 107, 99, 242, 25, 154, 174, 124, 245, 229, 22, 247, 162, 106, 215, 163, 131, 9, 72, 248, 244, 246, 33, 179, 120, 21, 177, 153, 249, 175, 45, 231, 138, 77, 76, 206, 46, 202, 149, 82, 30, 217, 56, 78, 40, 68, 213, 53, 167, 192, 6, 51, 105, 101, 0, 69, 86, 148, 152, 109, 118, 155, 252, 151, 194, 178, 254, 176, 32, 219, 235, 225, 228, 214, 71, 221, 29, 74, 182, 57, 15, 123, 147, 193, 27, 129, 180, 238, 234, 26, 145, 208, 184, 47, 185, 85, 133, 218, 65, 63, 224, 191, 88, 90, 95, 128, 11, 102, 144, 216, 136, 112, 44, 97, 13, 159, 135, 43, 130, 80, 100, 84, 125, 38, 64, 3, 75, 52, 115, 28, 196, 209, 59, 253, 251, 204, 171, 127, 62, 230, 165, 91, 237, 66, 110, 158, 60, 73, 67, 205, 210, 39, 212, 7, 199, 222, 24, 103, 203, 137, 31, 48, 198, 141, 170, 143, 116, 200, 201, 220, 92, 93, 164, 49, 119, 8, 190, 17, 79, 146, 197, 36, 54, 50, 207, 157, 166, 243, 172, 187, 108, 94, 19, 169, 37, 87, 227, 181, 168, 189, 1, 58, 89, 5, 70, 42, 4, 173, 156, 35, 81, 20, 240, 34, 121, 41, 126, 113, 140, 255, 226, 14, 239, 12, 114, 188, 111, 117, 161, 55, 211, 236, 98, 142, 134, 139, 232, 16 },
{ 122, 195, 18, 183, 61, 83, 233, 250, 2, 160, 10, 223, 96, 104, 23, 241, 229, 245, 124, 174, 106, 162, 247, 22, 107, 186, 150, 132, 154, 25, 242, 99, 21, 120, 179, 33, 175, 249, 153, 177, 9, 131, 163, 215, 246, 244, 248, 72, 217, 30, 82, 149, 68, 40, 78, 56, 77, 138, 231, 45, 202, 46, 206, 76, 148, 86, 69, 0, 155, 118, 109, 152, 192, 167, 53, 213, 101, 105, 51, 6, 214, 228, 225, 235, 74, 29, 221, 71, 178, 194, 151, 252, 219, 32, 176, 254, 26, 234, 238, 180, 47, 184, 208, 145, 123, 15, 57, 182, 129, 27, 193, 147, 128, 95, 90, 88, 216, 144, 102, 11, 218, 133, 85, 185, 191, 224, 63, 65, 84, 100, 80, 130, 3, 64, 38, 125, 97, 44, 112, 136, 43, 135, 159, 13, 127, 171, 204, 251, 91, 165, 230, 62, 28, 115, 52, 75, 253, 59, 209, 196, 7, 212, 39, 210, 103, 24, 222, 199, 158, 110, 66, 237, 205, 67, 73, 60, 220, 201, 200, 116, 49, 164, 93, 92, 48, 31, 137, 203, 143, 170, 141, 198, 157, 207, 50, 54, 187, 172, 243, 166, 17, 190, 8, 119, 36, 197, 146, 79, 58, 1, 189, 168, 42, 70, 5, 89, 169, 19, 94, 108, 181, 227, 87, 37, 113, 126, 41, 121, 14, 226, 255, 140, 35, 156, 173, 4, 34, 240, 20, 81, 142, 98, 236, 211, 16, 232, 139, 134, 188, 114, 12, 239, 55, 161, 117, 111 },
{ 242, 99, 154, 25, 150, 132, 107, 186, 247, 22, 106, 162, 124, 174, 229, 245, 23, 241, 96, 104, 10, 223, 2, 160, 233, 250, 61, 83, 18, 183, 122, 195, 206, 76, 202, 46, 231, 45, 77, 138, 78, 56, 68, 40, 82, 149, 217, 30, 248, 72, 246, 244, 163, 215, 9, 131, 153, 177, 175, 249, 179, 33, 21, 120, 176, 254, 219, 32, 151, 252, 178, 194, 221, 71, 74, 29, 225, 235, 214, 228, 51, 6, 101, 105, 53, 213, 192, 167, 109, 152, 155, 118, 69, 0, 148, 86, 63, 65, 191, 224, 85, 185, 218, 133, 102, 11, 216, 144, 90, 88, 128, 95, 193, 147, 129, 27, 57, 182, 123, 15, 208, 145, 47, 184, 238, 180, 26, 234, 209, 196, 253, 59, 52, 75, 28, 115, 230, 62, 91, 165, 204, 251, 127, 171, 159, 13, 43, 135, 112, 136, 97, 44, 38, 125, 3, 64, 80, 130, 84, 100, 141, 198, 143, 170, 137, 203, 48, 31, 93, 92, 49, 164, 200, 116, 220, 201, 73, 60, 205, 67, 66, 237, 158, 110, 222, 199, 103, 24, 39, 210, 7, 212, 87, 37, 181, 227, 94, 108, 169, 19, 5, 89, 42, 70, 189, 168, 58, 1, 146, 79, 36, 197, 8, 119, 17, 190, 243, 166, 187, 172, 50, 54, 157, 207, 117, 111, 55, 161, 12, 239, 188, 114, 139, 134, 16, 232, 236, 211, 142, 98, 20, 81, 34, 240, 173, 4, 35, 156, 255, 140, 14, 226, 41, 121, 113, 126 },
{ 238, 180, 26, 234, 208, 145, 47, 184, 57, 182, 123, 15, 193, 147, 129, 27, 90, 88, 128, 95, 102, 11, 216, 144, 85, 185, 218, 133, 63, 65, 191, 224, 69, 0, 148, 86, 109, 152, 155, 118, 53, 213, 192, 167, 51, 6, 101, 105, 225, 235, 214, 228, 221, 71, 74, 29, 151, 252, 178, 194, 176, 254, 219, 32, 179, 33, 21, 120, 153, 177, 175, 249, 163, 215, 9, 131, 248, 72, 246, 244, 82, 149, 217, 30, 78, 56, 68, 40, 231, 45, 77, 138, 206, 76, 202, 46, 18, 183, 122, 195, 233, 250, 61, 83, 10, 223, 2, 160, 23, 241, 96, 104, 124, 174, 229, 245, 247, 22, 106, 162, 150, 132, 107, 186, 242, 99, 154, 25, 41, 121, 113, 126, 255, 140, 14, 226, 173, 4, 35, 156, 20, 81, 34, 240, 236, 211, 142, 98, 139, 134, 16, 232, 12, 239, 188, 114, 117, 111, 55, 161, 50, 54, 157, 207, 243, 166, 187, 172, 8, 119, 17, 190, 146, 79, 36, 197, 189, 168, 58, 1, 5, 89, 42, 70, 94, 108, 169, 19, 87, 37, 181, 227, 39, 210, 7, 212, 222, 199, 103, 24, 66, 237, 158, 110, 73, 60, 205, 67, 200, 116, 220, 201, 93, 92, 49, 164, 137, 203, 48, 31, 141, 198, 143, 170, 80, 130, 84, 100, 38, 125, 3, 64, 112, 136, 97, 44, 159, 13, 43, 135, 204, 251, 127, 171, 230, 62, 91, 165, 52, 75, 28, 115, 209, 196, 253, 59 },
{ 41, 121, 113, 126, 255, 140, 14, 226, 173, 4, 35, 156, 20, 81, 34, 240, 236, 211, 142, 98, 139, 134, 16, 232, 12, 239, 188, 114, 117, 111, 55, 161, 50, 54, 157, 207, 243, 166, 187, 172, 8, 119, 17, 190, 146, 79, 36, 197, 189, 168, 58, 1, 5, 89, 42, 70, 94, 108, 169, 19, 87, 37, 181, 227, 39, 210, 7, 212, 222, 199, 103, 24, 66, 237, 158, 110, 73, 60, 205, 67, 200, 116, 220, 201, 93, 92, 49, 164, 137, 203, 48, 31, 141, 198, 143, 170, 80, 130, 84, 100, 38, 125, 3, 64, 112, 136, 97, 44, 159, 13, 43, 135, 204, 251, 127, 171, 230, 62, 91, 165, 52, 75, 28, 115, 209, 196, 253, 59, 238, 180, 26, 234, 208, 145, 47, 184, 57, 182, 123, 15, 193, 147, 129, 27, 90, 88, 128, 95, 102, 11, 216, 144, 85, 185, 218, 133, 63, 65, 191, 224, 69, 0, 148, 86, 109, 152, 155, 118, 53, 213, 192, 167, 51, 6, 101, 105, 225, 235, 214, 228, 221, 71, 74, 29, 151, 252, 178, 194, 176, 254, 219, 32, 179, 33, 21, 120, 153, 177, 175, 249, 163, 215, 9, 131, 248, 72, 246, 244, 82, 149, 217, 30, 78, 56, 68, 40, 231, 45, 77, 138, 206, 76, 202, 46, 18, 183, 122, 195, 233, 250, 61, 83, 10, 223, 2, 160, 23, 241, 96, 104, 124, 174, 229, 245, 247, 22, 106, 162, 150, 132, 107, 186, 242, 99, 154, 25 },
{ 56, 78, 40, 68, 149, 82, 30, 217, 76, 206, 46, 202, 45, 231, 138, 77, 177, 153, 249, 175, 33, 179, 120, 21, 72, 248, 244, 246, 215, 163, 131, 9, 22, 247, 162, 106, 174, 124, 245, 229, 99, 242, 25, 154, 132, 150, 186, 107, 250, 233, 83, 61, 183, 18, 195, 122, 241, 23, 104, 96, 223, 10, 160, 2, 11, 102, 144, 216, 88, 90, 95, 128, 65, 63, 224, 191, 185, 85, 133, 218, 145, 208, 184, 47, 180, 238, 234, 26, 147, 193, 27, 129, 182, 57, 15, 123, 71, 221, 29, 74, 235, 225, 228, 214, 254, 176, 32, 219, 252, 151, 194, 178, 152, 109, 118, 155, 0, 69, 86, 148, 6, 51, 105, 101, 213, 53, 167, 192, 92, 93, 164, 49, 116, 200, 201, 220, 198, 141, 170, 143, 203, 137, 31, 48, 199, 222, 24, 103, 210, 39, 212, 7, 60, 73, 67, 205, 237, 66, 110, 158, 62, 230, 165, 91, 251, 204, 171, 127, 196, 209, 59, 253, 75, 52, 115, 28, 125, 38, 64, 3, 130, 80, 100, 84, 13, 159, 135, 43, 136, 112, 44, 97, 134, 139, 232, 16, 211, 236, 98, 142, 111, 117, 161, 55, 239, 12, 114, 188, 140, 255, 226, 14, 121, 41, 126, 113, 81, 20, 240, 34, 4, 173, 156, 35, 89, 5, 70, 42, 168, 189, 1, 58, 37, 87, 227, 181, 108, 94, 19, 169, 166, 243, 172, 187, 54, 50, 207, 157, 79, 146, 197, 36, 119, 8, 190, 17 },
{ 251, 204, 171, 127, 62, 230, 165, 91, 75, 52, 115, 28, 196, 209, 59, 253, 130, 80, 100, 84, 125, 38, 64, 3, 136, 112, 44, 97, 13, 159, 135, 43, 116, 200, 201, 220, 92, 93, 164, 49, 203, 137, 31, 48, 198, 141, 170, 143, 210, 39, 212, 7, 199, 222, 24, 103, 237, 66, 110, 158, 60, 73, 67, 205, 168, 189, 1, 58, 89, 5, 70, 42, 108, 94, 19, 169, 37, 87, 227, 181, 54, 50, 207, 157, 166, 243, 172, 187, 119, 8, 190, 17, 79, 146, 197, 36, 211, 236, 98, 142, 134, 139, 232, 16, 239, 12, 114, 188, 111, 117, 161, 55, 121, 41, 126, 113, 140, 255, 226, 14, 4, 173, 156, 35, 81, 20, 240, 34, 174, 124, 245, 229, 22, 247, 162, 106, 132, 150, 186, 107, 99, 242, 25, 154, 183, 18, 195, 122, 250, 233, 83, 61, 223, 10, 160, 2, 241, 23, 104, 96, 149, 82, 30, 217, 56, 78, 40, 68, 45, 231, 138, 77, 76, 206, 46, 202, 33, 179, 120, 21, 177, 153, 249, 175, 215, 163, 131, 9, 72, 248, 244, 246, 235, 225, 228, 214, 71, 221, 29, 74, 252, 151, 194, 178, 254, 176, 32, 219, 0, 69, 86, 148, 152, 109, 118, 155, 213, 53, 167, 192, 6, 51, 105, 101, 88, 90, 95, 128, 11, 102, 144, 216, 185, 85, 133, 218, 65, 63, 224, 191, 180, 238, 234, 26, 145, 208, 184, 47, 182, 57, 15, 123, 147, 193, 27, 129 },
{ 219, 32, 176, 254, 178, 194, 151, 252, 74, 29, 221, 71, 214, 228, 225, 235, 101, 105, 51, 6, 192, 167, 53, 213, 155, 118, 109, 152, 148, 86, 69, 0, 191, 224, 63, 65, 218, 133, 85, 185, 216, 144, 102, 11, 128, 95, 90, 88, 129, 27, 193, 147, 123, 15, 57, 182, 47, 184, 208, 145, 26, 234, 238, 180, 154, 25, 242, 99, 107, 186, 150, 132, 106, 162, 247, 22, 229, 245, 124, 174, 96, 104, 23, 241, 2, 160, 10, 223, 61, 83, 233, 250, 122, 195, 18, 183, 202, 46, 206, 76, 77, 138, 231, 45, 68, 40, 78, 56, 217, 30, 82, 149, 246, 244, 248, 72, 9, 131, 163, 215, 175, 249, 153, 177, 21, 120, 179, 33, 181, 227, 87, 37, 169, 19, 94, 108, 42, 70, 5, 89, 58, 1, 189, 168, 36, 197, 146, 79, 17, 190, 8, 119, 187, 172, 243, 166, 157, 207, 50, 54, 55, 161, 117, 111, 188, 114, 12, 239, 16, 232, 139, 134, 142, 98, 236, 211, 34, 240, 20, 81, 35, 156, 173, 4, 14, 226, 255, 140, 113, 126, 41, 121, 253, 59, 209, 196, 28, 115, 52, 75, 91, 165, 230, 62, 127, 171, 204, 251, 43, 135, 159, 13, 97, 44, 112, 136, 3, 64, 38, 125, 84, 100, 80, 130, 143, 170, 141, 198, 48, 31, 137, 203, 49, 164, 93, 92, 220, 201, 200, 116, 205, 67, 73, 60, 158, 110, 66, 237, 103, 24, 222, 199, 7, 212, 39, 210 },
{ 36, 197, 146, 79, 17, 190, 8, 119, 187, 172, 243, 166, 157, 207, 50, 54, 181, 227, 87, 37, 169, 19, 94, 108, 42, 70, 5, 89, 58, 1, 189, 168, 34, 240, 20, 81, 35, 156, 173, 4, 14, 226, 255, 140, 113, 126, 41, 121, 55, 161, 117, 111, 188, 114, 12, 239, 16, 232, 139, 134, 142, 98, 236, 211, 43, 135, 159, 13, 97, 44, 112, 136, 3, 64, 38, 125, 84, 100, 80, 130, 253, 59, 209, 196, 28, 115, 52, 75, 91, 165, 230, 62, 127, 171, 204, 251, 205, 67, 73, 60, 158, 110, 66, 237, 103, 24, 222, 199, 7, 212, 39, 210, 143, 170, 141, 198, 48, 31, 137, 203, 49, 164, 93, 92, 220, 201, 200, 116, 101, 105, 51, 6, 192, 167, 53, 213, 155, 118, 109, 152, 148, 86, 69, 0, 219, 32, 176, 254, 178, 194, 151, 252, 74, 29, 221, 71, 214, 228, 225, 235, 129, 27, 193, 147, 123, 15, 57, 182, 47, 184, 208, 145, 26, 234, 238, 180, 191, 224, 63, 65, 218, 133, 85, 185, 216, 144, 102, 11, 128, 95, 90, 88, 96, 104, 23, 241, 2, 160, 10, 223, 61, 83, 233, 250, 122, 195, 18, 183, 154, 25, 242, 99, 107, 186, 150, 132, 106, 162, 247, 22, 229, 245, 124, 174, 246, 244, 248, 72, 9, 131, 163, 215, 175, 249, 153, 177, 21, 120, 179, 33, 202, 46, 206, 76, 77, 138, 231, 45, 68, 40, 78, 56, 217, 30, 82, 149 },
{ 23, 241, 96, 104, 10, 223, 2, 160, 233, 250, 61, 83, 18, 183, 122, 195, 242, 99, 154, 25, 150, 132, 107, 186, 247, 22, 106, 162, 124, 174, 229, 245, 248, 72, 246, 244, 163, 215, 9, 131, 153, 177, 175, 249, 179, 33, 21, 120, 206, 76, 202, 46, 231, 45, 77, 138, 78, 56, 68, 40, 82, 149, 217, 30, 51, 6, 101, 105, 53, 213, 192, 167, 109, 152, 155, 118, 69, 0, 148, 86, 176, 254, 219, 32, 151, 252, 178, 194, 221, 71, 74, 29, 225, 235, 214, 228, 193, 147, 129, 27, 57, 182, 123, 15, 208, 145, 47, 184, 238, 180, 26, 234, 63, 65, 191, 224, 85, 185, 218, 133, 102, 11, 216, 144, 90, 88, 128, 95, 159, 13, 43, 135, 112, 136, 97, 44, 38, 125, 3, 64, 80, 130, 84, 100, 209, 196, 253, 59, 52, 75, 28, 115, 230, 62, 91, 165, 204, 251, 127, 171, 73, 60, 205, 67, 66, 237, 158, 110, 222, 199, 103, 24, 39, 210, 7, 212, 141, 198, 143, 170, 137, 203, 48, 31, 93, 92, 49, 164, 200, 116, 220, 201, 146, 79, 36, 197, 8, 119, 17, 190, 243, 166, 187, 172, 50, 54, 157, 207, 87, 37, 181, 227, 94, 108, 169, 19, 5, 89, 42, 70, 189, 168, 58, 1, 20, 81, 34, 240, 173, 4, 35, 156, 255, 140, 14, 226, 41, 121, 113, 126, 117, 111, 55, 161, 12, 239, 188, 114, 139, 134, 16, 232, 236, 211, 142, 98 },
};
void g(state_t* st, int n1, int n2, int n3, int n4, int n5) {
unsigned int res;
unsigned int i;
Z3_ast tmp0, tmp1, tmp2, tmp3;
Z3_model m;
printf("g\n");
Z3_config cfg = Z3_mk_config();
Z3_context ctx = Z3_mk_context(cfg);
Z3_solver s = Z3_mk_solver(ctx);
Z3_sort bv8 = Z3_mk_bv_sort(ctx, 8);
Z3_sort bv16 = Z3_mk_bv_sort(ctx, 16);
Z3_sort arr = Z3_mk_array_sort(ctx, bv8, bv8);
/* Symbolic variables */
Z3_ast unk = Z3_mk_const(ctx, Z3_mk_string_symbol(ctx, "unk"), bv16);
Z3_ast m2 = Z3_mk_const(ctx, Z3_mk_string_symbol(ctx, "m2"), arr);
Z3_ast m3 = Z3_mk_const(ctx, Z3_mk_string_symbol(ctx, "m3"), arr);
Z3_ast m4 = Z3_mk_const(ctx, Z3_mk_string_symbol(ctx, "m4"), arr);
Z3_ast m5 = Z3_mk_const(ctx, Z3_mk_string_symbol(ctx, "m5"), arr);
/* Populate arrays */
for (i = 0; i < MAP_SIZE; ++i) {
m2 = Z3_mk_store(ctx, m2, Z3_mk_int(ctx, i, bv8), Z3_mk_int(ctx, map[n2][i], bv8));
m3 = Z3_mk_store(ctx, m3, Z3_mk_int(ctx, i, bv8), Z3_mk_int(ctx, map[n3][i], bv8));
m4 = Z3_mk_store(ctx, m4, Z3_mk_int(ctx, i, bv8), Z3_mk_int(ctx, map[n4][i], bv8));
m5 = Z3_mk_store(ctx, m5, Z3_mk_int(ctx, i, bv8), Z3_mk_int(ctx, map[n5][i], bv8));
}
tmp0 = Z3_mk_bvxor(
ctx,
unk,
Z3_mk_bvshl(
ctx,
Z3_mk_zero_ext(
ctx,
16 - 8,
Z3_mk_select(ctx, m5, Z3_mk_extract(ctx, 7, 0, unk))
),
Z3_mk_unsigned_int(ctx, 8, bv16)
)
);
tmp1 = Z3_mk_bvxor(
ctx,
tmp0,
Z3_mk_zero_ext(
ctx,
16 - 8,
Z3_mk_select(
ctx,
m4,
Z3_mk_extract(
ctx,
7,
0,
Z3_mk_bvashr(
ctx,
tmp0,
Z3_mk_unsigned_int(ctx, 8, bv16)
)
)
)
)
);
tmp2 = Z3_mk_bvxor(
ctx,
tmp1,
Z3_mk_bvshl(
ctx,
Z3_mk_zero_ext(
ctx,
16 - 8,
Z3_mk_select(ctx, m3, Z3_mk_extract(ctx, 7, 0, tmp1))
),
Z3_mk_unsigned_int(ctx, 8, bv16)
)
);
tmp3 = Z3_mk_bvxor(
ctx,
tmp2,
Z3_mk_zero_ext(
ctx,
16 - 8,
Z3_mk_select(
ctx,
m2,
Z3_mk_extract(
ctx,
7,
0,
Z3_mk_bvashr(
ctx,
tmp2,
Z3_mk_unsigned_int(ctx, 8, bv16)
)
)
)
)
);
/*
* The final observed value must be equal to the original value after applying
* the transformations.
*/
Z3_ast c1 = Z3_mk_eq(ctx, tmp3, Z3_mk_unsigned_int(ctx, st->enc[n1], bv16));
Z3_solver_assert(ctx, s, c1);
if (Z3_solver_check(ctx, s) == Z3_L_TRUE) {
Z3_ast v;
m = Z3_solver_get_model(ctx, s);
Z3_model_eval(ctx, m, unk, 1, &v);
if (!Z3_get_numeral_uint(ctx, v, &res))
warnx("[!] Z3_get_numeral_uint");
st->enc[n1] = (ushort)res;
Z3_del_context(ctx);
Z3_del_config(cfg);
} else {
printf(":(\n");
exit(EXIT_FAILURE);
}
}
void z(state_t* st, int n1, int n2) {
int r;
ushort x;
printf("z\n");
x = st->enc[n2];
r = st->rand[(st->rpos)--];
st->enc[n1] ^= x ^ (ushort)r;
}
void decrypt(state_t* st, byte* inp) {
unsigned int i;
unsigned int seed;
unsigned char out[8];
for (i = 0; i < 4; ++i) {
st->enc[i] = inp[i * 2] << 8 | inp[(i * 2) + 1];
}
/* Obtain seed and generate rand() values */
seed = map[9][31] << (3 * 8) | map[8][10] << (2 * 8) | map[5][14] << 8 | map[1][14];
assert(seed == 4126378519);
srand(seed);
for (i = 0; i < ARR_SIZE(st->rand); ++i) {
st->rand[i] = rand();
}
st->rpos = ARR_SIZE(st->rand) - 1;
/* Apply transformations in reverse order */
g(st, 0, 4, 5, 6, 7);
z(st,1,0);
g(st, 1, 0, 1, 2, 3);
z(st,2,1);
g(st, 2, 6, 7, 8, 9);
z(st,3,2);
g(st, 3, 2, 3, 4, 5);
z(st,0,3);
g(st, 0, 8, 9, 0, 1);
z(st,1,0);
g(st, 1, 4, 5, 6, 7);
z(st,2,1);
g(st, 2, 0, 1, 2, 3);
z(st,3,2);
g(st, 3, 6, 7, 8, 9);
z(st,0,3);
z(st,3,0);
g(st, 0, 2, 3, 4, 5);
z(st,0,1);
g(st, 1, 8, 9, 0, 1);
z(st,1,2);
g(st, 2, 4, 5, 6, 7);
z(st,2,3);
g(st, 3, 0, 1, 2, 3);
z(st,3,0);
g(st, 0, 6, 7, 8, 9);
z(st,0,1);
g(st, 1, 2, 3, 4, 5);
z(st,1,2);
g(st, 2, 8, 9, 0, 1);
z(st,2,3);
g(st, 3, 4, 5, 6, 7);
g(st, 0, 0, 1, 2, 3);
z(st,1,0);
g(st, 1, 6, 7, 8, 9);
z(st,2,1);
g(st, 2, 2, 3, 4, 5);
z(st,3,2);
g(st, 3, 8, 9, 0, 1);
z(st,0,3);
g(st, 0, 4, 5, 6, 7);
z(st,1,0);
g(st, 1, 0, 1, 2, 3);
z(st,2,1);
g(st, 2, 6, 7, 8, 9);
z(st,3,2);
g(st, 3, 2, 3, 4, 5);
z(st,0,3);
z(st,3,0);
g(st, 0, 8, 9, 0, 1);
z(st,0,1);
g(st, 1, 4, 5, 6, 7);
z(st,1,2);
g(st, 2, 0, 1, 2, 3);
z(st,2,3);
g(st, 3, 6, 7, 8, 9);
z(st,3,0);
g(st, 0, 2, 3, 4, 5);
z(st,0,1);
g(st, 1, 8, 9, 0, 1);
z(st,1,2);
g(st, 2, 4, 5, 6, 7);
z(st,2,3);
g(st, 3, 0, 1, 2, 3);
/* Transform back to byte array and print */
for (i = 0; i < 4; ++i) {
out[i * 2] = (byte)((uint)(ushort)st->enc[i] >> 8);
out[(long)(i * 2) + 1] = (byte)st->enc[i];
}
for (i = 0; i < 8; ++i) {
printf("%c", out[i]);
}
printf("\n");
}
int main() {
unsigned int i;
state_t st;
for (i = 0; i < NUM_BLOCKS; ++i) {
decrypt(&st, &encrypted[i * BLOCK_SIZE]);
}
/* ASIS{DATA_1n_TEXT_1s_4n_34sy_0bfusc4t10n} */
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment