Beispiel #1
0
        void EmitCore(CipherGenContext context)
        {
            Expression         a = context.GetDataExpression(DataIndexes[0]);
            Expression         b = context.GetDataExpression(DataIndexes[1]);
            VariableExpression tmp;

            if (Mask == 0xffffffff)
            {
                /*  t = a * k;
                 *  a = b;
                 *  b = t * k^-1;
                 */
                using (context.AcquireTempVar(out tmp))
                {
                    context.Emit(new AssignmentStatement
                    {
                        Value  = a * (LiteralExpression)Key,
                        Target = tmp
                    }).Emit(new AssignmentStatement
                    {
                        Value  = b,
                        Target = a
                    }).Emit(new AssignmentStatement
                    {
                        Value  = tmp * (LiteralExpression)MathsUtils.modInv(Key),
                        Target = b
                    });
                }
            }
            else
            {
                var mask    = (LiteralExpression)Mask;
                var notMask = (LiteralExpression) ~Mask;

                /*  t = (a & mask) * k;
                 *  a = a & (~mask) | (b & mask);
                 *  b = b & (~mask) | (t * k^-1);
                 */
                using (context.AcquireTempVar(out tmp))
                {
                    context.Emit(new AssignmentStatement
                    {
                        Value  = (a & mask) * (LiteralExpression)Key,
                        Target = tmp
                    }).Emit(new AssignmentStatement
                    {
                        Value  = (a & notMask) | (b & mask),
                        Target = a
                    }).Emit(new AssignmentStatement
                    {
                        Value  = (b & notMask) | (tmp * (LiteralExpression)MathsUtils.modInv(Key)),
                        Target = b
                    });
                }
            }
        }
Beispiel #2
0
        public override void EmitInverse(CipherGenContext context)
        {
            Expression         val = context.GetDataExpression(DataIndexes[0]);
            VariableExpression tmp;

            using (context.AcquireTempVar(out tmp)) {
                if (IsAlternate)
                {
                    context.Emit(new AssignmentStatement {
                        Value  = (val << (32 - Bits)),
                        Target = tmp
                    }).Emit(new AssignmentStatement {
                        Value  = (val >> Bits) | tmp,
                        Target = val
                    });
                }
                else
                {
                    context.Emit(new AssignmentStatement {
                        Value  = (val >> (32 - Bits)),
                        Target = tmp
                    }).Emit(new AssignmentStatement {
                        Value  = (val << Bits) | tmp,
                        Target = val
                    });
                }
            }
        }
Beispiel #3
0
        void EmitCore(CipherGenContext context, uint[,] k)
        {
            Expression a = context.GetDataExpression(DataIndexes[0]);
            Expression b = context.GetDataExpression(DataIndexes[1]);
            Expression c = context.GetDataExpression(DataIndexes[2]);
            Expression d = context.GetDataExpression(DataIndexes[3]);

            VariableExpression ta, tb, tc, td;

            Func <uint, LiteralExpression> l = v => (LiteralExpression)v;

            using (context.AcquireTempVar(out ta))
                using (context.AcquireTempVar(out tb))
                    using (context.AcquireTempVar(out tc))
                        using (context.AcquireTempVar(out td))
                        {
                            context.Emit(new AssignmentStatement
                            {
                                Value  = a * l(k[0, 0]) + b * l(k[0, 1]) + c * l(k[0, 2]) + d * l(k[0, 3]),
                                Target = ta
                            }).Emit(new AssignmentStatement
                            {
                                Value  = a * l(k[1, 0]) + b * l(k[1, 1]) + c * l(k[1, 2]) + d * l(k[1, 3]),
                                Target = tb
                            }).Emit(new AssignmentStatement
                            {
                                Value  = a * l(k[2, 0]) + b * l(k[2, 1]) + c * l(k[2, 2]) + d * l(k[2, 3]),
                                Target = tc
                            }).Emit(new AssignmentStatement
                            {
                                Value  = a * l(k[3, 0]) + b * l(k[3, 1]) + c * l(k[3, 2]) + d * l(k[3, 3]),
                                Target = td
                            })
                            .Emit(new AssignmentStatement {
                                Value = ta, Target = a
                            })
                            .Emit(new AssignmentStatement {
                                Value = tb, Target = b
                            })
                            .Emit(new AssignmentStatement {
                                Value = tc, Target = c
                            })
                            .Emit(new AssignmentStatement {
                                Value = td, Target = d
                            });
                        }
        }