Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.crypto.macs;
  
implementation of GOST 28147-89 MAC
 
 public class GOST28147Mac
     implements Mac
 {
     private int                 blockSize = 8;
     private int                 macSize = 4;
     private int                 bufOff;
     private byte[]              buf;
     private byte[]              mac;
     private boolean             firstStep = true;
     private int[]               workingKey = null;
 
     //
     // This is default S-box - E_A.
     private byte S[] = {
             0x9,0x6,0x3,0x2,0x8,0xB,0x1,0x7,0xA,0x4,0xE,0xF,0xC,0x0,0xD,0x5,
             0x3,0x7,0xE,0x9,0x8,0xA,0xF,0x0,0x5,0x2,0x6,0xC,0xB,0x4,0xD,0x1,
             0xE,0x4,0x6,0x2,0xB,0x3,0xD,0x8,0xC,0xF,0x5,0xA,0x0,0x7,0x1,0x9,
             0xE,0x7,0xA,0xC,0xD,0x1,0x3,0x9,0x0,0x2,0xB,0x4,0xF,0x8,0x5,0x6,
             0xB,0x5,0x1,0x9,0x8,0xD,0xF,0x0,0xE,0x4,0x2,0x3,0xC,0x7,0xA,0x6,
             0x3,0xA,0xD,0xC,0x1,0x2,0x0,0xB,0x7,0x5,0x9,0x4,0x8,0xF,0xE,0x6,
             0x1,0xD,0x2,0x9,0x7,0xA,0x6,0x0,0x8,0xC,0x4,0x5,0xF,0x3,0xB,0xE,
             0xB,0xA,0xF,0x5,0x0,0xC,0xE,0x8,0x6,0x2,0x3,0x9,0x1,0x7,0xD,0x4
     };
     
     public GOST28147Mac()
     {
          = new byte[];
 
          = new byte[];
          = 0;
     }
 
     private int[] generateWorkingKey(
         byte[]  userKey)
     {
         if (userKey.length != 32)
         {
             throw new IllegalArgumentException("Key length invalid. Key needs to be 32 byte - 256 bit!!!");
         }
 
         int key[] = new int[8];
         for(int i=0; i!=8; i++)
         {
             key[i] = bytesToint(userKey,i*4);
         }
 
         return key;
     }
     
     public void init(
         CipherParameters params)
         throws IllegalArgumentException
     {
         reset();
          = new byte[];
         if (params instanceof ParametersWithSBox)
         {
             ParametersWithSBox   param = (ParametersWithSBox)params;
 
             //
             // Set the S-Box
             //
             System.arraycopy(param.getSBox(), 0, this., 0, param.getSBox().length);
 
             //
             // set key if there is one
             //
             if (param.getParameters() != null)
             {
                  = generateWorkingKey(((KeyParameter)param.getParameters()).getKey());
             }
         }
         else if (params instanceof KeyParameter)
         {
              = generateWorkingKey(((KeyParameter)params).getKey());
         }
         else
         {
            throw new IllegalArgumentException("invalid parameter passed to GOST28147 init - " + params.getClass().getName());
         }
     }
 
     public String getAlgorithmName()
     {
         return "GOST28147Mac";
     }
 
     public int getMacSize()
    {
        return ;
    }
    private int gost28147_mainStep(int n1int key)
    {
        int cm = (key + n1); // CM1
        
        // S-box replacing
        
        int om = [  0 + ((cm >> (0 * 4)) & 0xF)] << (0 * 4);
        om += [ 16 + ((cm >> (1 * 4)) & 0xF)] << (1 * 4);
        om += [ 32 + ((cm >> (2 * 4)) & 0xF)] << (2 * 4);
        om += [ 48 + ((cm >> (3 * 4)) & 0xF)] << (3 * 4);
        om += [ 64 + ((cm >> (4 * 4)) & 0xF)] << (4 * 4);
        om += [ 80 + ((cm >> (5 * 4)) & 0xF)] << (5 * 4);
        om += [ 96 + ((cm >> (6 * 4)) & 0xF)] << (6 * 4);
        om += [112 + ((cm >> (7 * 4)) & 0xF)] << (7 * 4);
        
        return om << 11 | om >>> (32-11); // 11-leftshift
    }
    
    private void gost28147MacFunc(
            int[]   workingKey,
            byte[]  in,
            int     inOff,
            byte[]  out,
            int     outOff)
    {
        int N1N2tmp;  //tmp -> for saving N1
        N1 = bytesToint(ininOff);
        N2 = bytesToint(ininOff + 4);
        
        for(int k = 0; k < 2; k++)  // 1-16 steps
        {
            for(int j = 0; j < 8; j++)
            {
                tmp = N1;
                N1 = N2 ^ gost28147_mainStep(N1workingKey[j]); // CM2
                N2 = tmp;
            }
        }
        
        intTobytes(N1outoutOff);
        intTobytes(N2outoutOff + 4);
    }
    
    //array of bytes to type int
    private int bytesToint(
            byte[]  in,
            int     inOff)
    {
        return  ((in[inOff + 3] << 24) & 0xff000000) + ((in[inOff + 2] << 16) & 0xff0000) +
        ((in[inOff + 1] << 8) & 0xff00) + (in[inOff] & 0xff);
    }
    
    //int to array of bytes
    private void intTobytes(
            int     num,
            byte[]  out,
            int     outOff)
    {
        out[outOff + 3] = (byte)(num >>> 24);
        out[outOff + 2] = (byte)(num >>> 16);
        out[outOff + 1] = (byte)(num >>> 8);
        out[outOff] =     (byte)num;
    }
        
    private byte[] CM5func(byte[] bufint bufOffbyte[] mac)
    {
        byte[] sum = new byte[buf.length - bufOff];
        System.arraycopy(bufbufOffsum, 0, mac.length);
        for (int i = 0; i != mac.lengthi++)
        {
            sum[i] = (byte)(sum[i] ^ mac[i]);
        }
        return sum;
    }
    public void update(byte in)
            throws IllegalStateException
    {
        if ( == .)
        {
            byte[] sumbuf = new byte[.];
            System.arraycopy(, 0, sumbuf, 0, .);
            if ()
            {
                 = false;
            }
            else
            {
                sumbuf = CM5func(, 0, );
            }
            gost28147MacFunc(sumbuf, 0, , 0);
             = 0;
        }
        [++] = in;
    }
    public void update(byte[] inint inOffint len)
    {
            if (len < 0)
            {
                throw new IllegalArgumentException("Can't have a negative input length!");
            }
            int gapLen =  - ;
            if (len > gapLen)
            {
                System.arraycopy(ininOffgapLen);
                byte[] sumbuf = new byte[.];
                System.arraycopy(, 0, sumbuf, 0, .);
                if ()
                {
                     = false;
                }
                else
                {
                    sumbuf = CM5func(, 0, );
                }
                gost28147MacFunc(sumbuf, 0, , 0);
                 = 0;
                len -= gapLen;
                inOff += gapLen;
                while (len > )
                {
                    sumbuf = CM5func(ininOff);
                    gost28147MacFunc(sumbuf, 0, , 0);
                    len -= ;
                    inOff += ;
                }
            }
            System.arraycopy(ininOfflen);
             += len;    
    }     
    public int doFinal(byte[] outint outOff)
    {
        //padding with zero
        while ( < )
        {
            [] = 0;
            ++;
        }
        byte[] sumbuf = new byte[.];
        System.arraycopy(, 0, sumbuf, 0, .);
        if ()
        {
             = false;
        }
        else
        {
            sumbuf = CM5func(, 0, );
        }
        gost28147MacFunc(sumbuf, 0, , 0);
        System.arraycopy(, (./2)-outoutOff);
        reset();
        return ;
    }
    public void reset()
    {
        /*
         * clean the buffer.
         */
        for (int i = 0; i < .i++)
        {
            [i] = 0;
        }
         = 0;
         = true;
    }
New to GrepCode? Check out our FAQ X