OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [javax/] [crypto/] [kwa/] [AESKeyWrap.java] - Blame information for rev 775

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* AESWrap.java -- An implementation of RFC-3394 AES Key Wrap Algorithm
2
   Copyright (C) 2006 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
 
39
package gnu.javax.crypto.kwa;
40
 
41
import gnu.java.security.Registry;
42
import gnu.javax.crypto.cipher.IBlockCipher;
43
import gnu.javax.crypto.cipher.Rijndael;
44
 
45
import java.security.InvalidKeyException;
46
import java.util.Arrays;
47
import java.util.HashMap;
48
import java.util.Map;
49
 
50
/**
51
 * The GNU implementation of the AES Key Wrap Algorithm as described in [1].
52
 * <p>
53
 * References:
54
 * <ol>
55
 * <li><a href="http://csrc.nist.gov/encryption/kms/key-wrap.pdf"></a>.</li>
56
 * <li><a href="http://www.rfc-archive.org/getrfc.php?rfc=3394">Advanced
57
 * Encryption Standard (AES) Key Wrap Algorithm</a>.</li>
58
 * <li><a href="http://www.w3.org/TR/xmlenc-core/">XML Encryption Syntax and
59
 * Processing</a>.</li>
60
 * </ol>
61
 */
62
public class AESKeyWrap
63
    extends BaseKeyWrappingAlgorithm
64
{
65
  private static final byte[] DEFAULT_IV = new byte[] {
66
      (byte) 0xA6, (byte) 0xA6, (byte) 0xA6, (byte) 0xA6,
67
      (byte) 0xA6, (byte) 0xA6, (byte) 0xA6, (byte) 0xA6 };
68
 
69
  private Rijndael aes;
70
  private byte[] iv;
71
 
72
  public AESKeyWrap()
73
  {
74
    super(Registry.AES_KWA);
75
 
76
    aes = new Rijndael();
77
  }
78
 
79
  protected void engineInit(Map attributes) throws InvalidKeyException
80
  {
81
    Map cipherAttributes = new HashMap();
82
    cipherAttributes.put(IBlockCipher.CIPHER_BLOCK_SIZE, Integer.valueOf(16));
83
    cipherAttributes.put(IBlockCipher.KEY_MATERIAL,
84
                         attributes.get(KEY_ENCRYPTION_KEY_MATERIAL));
85
    aes.reset();
86
    aes.init(cipherAttributes);
87
    byte[] initialValue = (byte[]) attributes.get(INITIAL_VALUE);
88
    iv = initialValue == null ? DEFAULT_IV : (byte[]) initialValue.clone();
89
  }
90
 
91
  protected byte[] engineWrap(byte[] in, int inOffset, int length)
92
  {
93
    // TODO: handle input length which is not a multiple of 8 as suggested by
94
    // section 2.2.3.2 of RFC-3394
95
    if (length % 8 != 0)
96
      throw new IllegalArgumentException("Input length MUST be a multiple of 8");
97
    int n = length / 8;
98
    // output is always one block larger than input
99
    byte[] result = new byte[length + 8];
100
 
101
    // 1. init variables: we'll use out buffer for our work buffer;
102
    //    A will be the first block in out, while R will be the rest
103
    System.arraycopy(iv, 0, result, 0, 8);
104
    System.arraycopy(in, inOffset, result, 8, length);
105
    byte[] B = new byte[2 * 8];
106
    // 2. compute intermediate values
107
    long t;
108
    for (int j = 0; j < 6; j++)
109
      for (int i = 1; i <= n; i++)
110
        {
111
          System.arraycopy(result, 0, B, 0, 8);
112
          System.arraycopy(result, i * 8, B, 8, 8);
113
          aes.encryptBlock(B, 0, B, 0);
114
          t = (n * j) + i;
115
          result[0] = (byte)(B[0] ^ (t >>> 56));
116
          result[1] = (byte)(B[1] ^ (t >>> 48));
117
          result[2] = (byte)(B[2] ^ (t >>> 40));
118
          result[3] = (byte)(B[3] ^ (t >>> 32));
119
          result[4] = (byte)(B[4] ^ (t >>> 24));
120
          result[5] = (byte)(B[5] ^ (t >>> 16));
121
          result[6] = (byte)(B[6] ^ (t >>>  8));
122
          result[7] = (byte)(B[7] ^  t        );
123
          System.arraycopy(B, 8, result, i * 8, 8);
124
        }
125
    return result;
126
  }
127
 
128
  protected byte[] engineUnwrap(byte[] in, int inOffset, int length)
129
      throws KeyUnwrappingException
130
  {
131
    // TODO: handle input length which is not a multiple of 8 as suggested by
132
    // section 2.2.3.2 of RFC-3394
133
    if (length % 8 != 0)
134
      throw new IllegalArgumentException("Input length MUST be a multiple of 8");
135
    // output is always one block shorter than input
136
    byte[] result = new byte[length - 8];
137
 
138
    // 1. init variables: we'll use out buffer for our R work buffer
139
    byte[] A = new byte[8];
140
    System.arraycopy(in, inOffset, A, 0, 8);
141
    System.arraycopy(in, inOffset + 8, result, 0, result.length);
142
    byte[] B = new byte[2 * 8];
143
    // 2. compute intermediate values
144
    int n = length / 8 - 1;
145
    long t;
146
    for (int j = 5; j >= 0; j--)
147
      for (int i = n; i >= 1; i--)
148
        {
149
          t = (n * j) + i;
150
          B[0] = (byte)(A[0] ^ (t >>> 56));
151
          B[1] = (byte)(A[1] ^ (t >>> 48));
152
          B[2] = (byte)(A[2] ^ (t >>> 40));
153
          B[3] = (byte)(A[3] ^ (t >>> 32));
154
          B[4] = (byte)(A[4] ^ (t >>> 24));
155
          B[5] = (byte)(A[5] ^ (t >>> 16));
156
          B[6] = (byte)(A[6] ^ (t >>>  8));
157
          B[7] = (byte)(A[7] ^  t        );
158
          System.arraycopy(result, (i - 1) * 8, B, 8, 8);
159
          aes.decryptBlock(B, 0, B, 0);
160
          System.arraycopy(B, 0, A, 0, 8);
161
          System.arraycopy(B, 8, result, (i - 1) * 8, 8);
162
        }
163
    if (! Arrays.equals(A, iv))
164
      throw new KeyUnwrappingException();
165
 
166
    return result;
167
  }
168
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.