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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [java/] [security/] [sig/] [rsa/] [RSAPKCS1V1_5SignatureRawCodec.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
/* RSAPKCS1V1_5SignatureRawCodec.java -- Raw RSA PKCS1 v1.5 signature codeec
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.java.security.sig.rsa;
40
 
41
import java.io.ByteArrayOutputStream;
42
 
43
import gnu.java.security.Registry;
44
import gnu.java.security.sig.ISignatureCodec;
45
 
46
/**
47
 * An object that implements the {@link ISignatureCodec} operations for the
48
 * <i>Raw</i> format to use with RSA-PKCS#1 v1.5 signatures.
49
 */
50
public class RSAPKCS1V1_5SignatureRawCodec
51
    implements ISignatureCodec
52
{
53
  public int getFormatID()
54
  {
55
    return RAW_FORMAT;
56
  }
57
 
58
  /**
59
   * Returns the encoded form of the designated RSA-PKCS#1 (v1.5) signature
60
   * object according to the <i>Raw</i> format supported by this library.
61
   * <p>
62
   * The <i>Raw</i> format for such a signature, in this implementation, is a
63
   * byte sequence consisting of the following:
64
   * <p>
65
   * <ol>
66
   * <li>4-byte magic consisting of the value of the literal
67
   * {@link Registry#MAGIC_RAW_RSA_PKCS1V1_5_SIGNATURE},
68
   * <li>
69
   * <li>1-byte version consisting of the constant: 0x01,</li>
70
   * <li>4-byte count of following bytes representing the RSA-PKCS#1 (v1.5)
71
   * signature bytes in internet order,</li>
72
   * <li>the RSA-PKCS#1 (v1.5) signature bytes in internet order.</li>
73
   * </ol>
74
   *
75
   * @param signature the signature to encode, consisting of the output of the
76
   *          <code>sign()</code> method of a {@link RSAPKCS1V1_5Signature}
77
   *          instance --a byte array.
78
   * @return the <i>Raw</i> format encoding of the designated signature.
79
   * @exception IllegalArgumentException if the designated signature is not an
80
   *              RSA-PKCS#1 (v1.5) one.
81
   */
82
  public byte[] encodeSignature(Object signature)
83
  {
84
    byte[] buffer;
85
    try
86
      {
87
        buffer = (byte[]) signature;
88
      }
89
    catch (Exception x)
90
      {
91
        throw new IllegalArgumentException("Signature/codec mismatch");
92
      }
93
 
94
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
95
 
96
    // magic
97
    baos.write(Registry.MAGIC_RAW_RSA_PKCS1V1_5_SIGNATURE[0]);
98
    baos.write(Registry.MAGIC_RAW_RSA_PKCS1V1_5_SIGNATURE[1]);
99
    baos.write(Registry.MAGIC_RAW_RSA_PKCS1V1_5_SIGNATURE[2]);
100
    baos.write(Registry.MAGIC_RAW_RSA_PKCS1V1_5_SIGNATURE[3]);
101
 
102
    // version
103
    baos.write(0x01);
104
 
105
    // signature bytes
106
    int length = buffer.length;
107
    baos.write( length >>> 24);
108
    baos.write((length >>> 16) & 0xFF);
109
    baos.write((length >>>  8) & 0xFF);
110
    baos.write( length         & 0xFF);
111
    baos.write(buffer, 0, length);
112
 
113
    return baos.toByteArray();
114
  }
115
 
116
  /**
117
   * Returns the decoded object from a designated input assumed to have been
118
   * generated by the {@link #encodeSignature(Object)} method.
119
   *
120
   * @param input the input bytes of a previously Raw-encoded RSA PKCS1 (v1.5)
121
   *          signature.
122
   * @return the signature object.
123
   * @throws IllegalArgumentException if the designated input does not start
124
   *           with the right <i>magic</i> characters, or if the <i>version</i>
125
   *           is not supported.
126
   */
127
  public Object decodeSignature(byte[] input)
128
  {
129
    // magic
130
    if (input[0] != Registry.MAGIC_RAW_RSA_PKCS1V1_5_SIGNATURE[0]
131
        || input[1] != Registry.MAGIC_RAW_RSA_PKCS1V1_5_SIGNATURE[1]
132
        || input[2] != Registry.MAGIC_RAW_RSA_PKCS1V1_5_SIGNATURE[2]
133
        || input[3] != Registry.MAGIC_RAW_RSA_PKCS1V1_5_SIGNATURE[3])
134
      throw new IllegalArgumentException("Signature/codec mismatch");
135
 
136
    // version
137
    if (input[4] != 0x01)
138
      throw new IllegalArgumentException("Wrong or unsupported format version");
139
 
140
    int i = 5;
141
    int l;
142
 
143
    // signature bytes
144
    l =    input[i++]         << 24
145
        | (input[i++] & 0xFF) << 16
146
        | (input[i++] & 0xFF) <<  8
147
        | (input[i++] & 0xFF);
148
    byte[] result = new byte[l];
149
    System.arraycopy(input, i, result, 0, l);
150
 
151
    return result;
152
  }
153
}

powered by: WebSVN 2.1.0

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