RFC3394WrapEngine.cs :  » PDF » iTextSharp » Org » BouncyCastle » Crypto » Engines » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » iTextSharp 
iTextSharp » Org » BouncyCastle » Crypto » Engines » RFC3394WrapEngine.cs
using System;

using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Crypto.Engines{
  /// <remarks>
  /// An implementation of the AES Key Wrapper from the NIST Key Wrap
  /// Specification as described in RFC 3394.
  /// <p/>
  /// For further details see: <a href="http://www.ietf.org/rfc/rfc3394.txt">http://www.ietf.org/rfc/rfc3394.txt</a>
  /// and  <a href="http://csrc.nist.gov/encryption/kms/key-wrap.pdf">http://csrc.nist.gov/encryption/kms/key-wrap.pdf</a>.
  /// </remarks>
  public class Rfc3394WrapEngine
    : IWrapper
  {
    private readonly IBlockCipher engine;

    private KeyParameter  param;
    private bool      forWrapping;

    private byte[] iv =
    {
      0xa6, 0xa6, 0xa6, 0xa6,
      0xa6, 0xa6, 0xa6, 0xa6
    };

    public Rfc3394WrapEngine(
      IBlockCipher engine)
    {
      this.engine = engine;
    }

    public void Init(
      bool        forWrapping,
      ICipherParameters  parameters)
    {
      this.forWrapping = forWrapping;

      if (parameters is ParametersWithRandom)
      {
        parameters = ((ParametersWithRandom) parameters).Parameters;
      }

      if (parameters is KeyParameter)
      {
        this.param = (KeyParameter) parameters;
      }
      else if (parameters is ParametersWithIV)
      {
        ParametersWithIV pIV = (ParametersWithIV) parameters;
        byte[] iv = pIV.GetIV();

        if (iv.Length != 8)
          throw new ArgumentException("IV length not equal to 8", "parameters");

        this.iv = iv;
        this.param = (KeyParameter) pIV.Parameters;
      }
      else
      {
        // TODO Throw an exception for bad parameters?
      }
    }

    public string AlgorithmName
    {
      get { return engine.AlgorithmName; }
    }

    public byte[] Wrap(
      byte[]  input,
      int    inOff,
      int    inLen)
    {
      if (!forWrapping)
      {
        throw new InvalidOperationException("not set for wrapping");
      }

      int n = inLen / 8;

      if ((n * 8) != inLen)
      {
        throw new DataLengthException("wrap data must be a multiple of 8 bytes");
      }

      byte[] block = new byte[inLen + iv.Length];
      byte[] buf = new byte[8 + iv.Length];

      Array.Copy(iv, 0, block, 0, iv.Length);
      Array.Copy(input, 0, block, iv.Length, inLen);

      engine.Init(true, param);

      for (int j = 0; j != 6; j++)
      {
        for (int i = 1; i <= n; i++)
        {
          Array.Copy(block, 0, buf, 0, iv.Length);
          Array.Copy(block, 8 * i, buf, iv.Length, 8);
          engine.ProcessBlock(buf, 0, buf, 0);

          int t = n * j + i;
          for (int k = 1; t != 0; k++)
          {
            byte v = (byte)t;

            buf[iv.Length - k] ^= v;
            t = (int) ((uint)t >> 8);
          }

          Array.Copy(buf, 0, block, 0, 8);
          Array.Copy(buf, 8, block, 8 * i, 8);
        }
      }

      return block;
    }

    public byte[] Unwrap(
      byte[]  input,
      int     inOff,
      int     inLen)
    {
      if (forWrapping)
      {
        throw new InvalidOperationException("not set for unwrapping");
      }

      int n = inLen / 8;

      if ((n * 8) != inLen)
      {
        throw new InvalidCipherTextException("unwrap data must be a multiple of 8 bytes");
      }

      byte[]  block = new byte[inLen - iv.Length];
      byte[]  a = new byte[iv.Length];
      byte[]  buf = new byte[8 + iv.Length];

      Array.Copy(input, 0, a, 0, iv.Length);
      Array.Copy(input, iv.Length, block, 0, inLen - iv.Length);

      engine.Init(false, param);

      n = n - 1;

      for (int j = 5; j >= 0; j--)
      {
        for (int i = n; i >= 1; i--)
        {
          Array.Copy(a, 0, buf, 0, iv.Length);
          Array.Copy(block, 8 * (i - 1), buf, iv.Length, 8);

          int t = n * j + i;
          for (int k = 1; t != 0; k++)
          {
            byte v = (byte)t;

            buf[iv.Length - k] ^= v;
            t = (int) ((uint)t >> 8);
          }

          engine.ProcessBlock(buf, 0, buf, 0);
          Array.Copy(buf, 0, a, 0, 8);
          Array.Copy(buf, 8, block, 8 * (i - 1), 8);
        }
      }

      if (!Arrays.ConstantTimeAreEqual(a, iv))
        throw new InvalidCipherTextException("checksum failed");

      return block;
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.