Added CCM and CCM_8 cipher suites.
[BoarSSL] / Crypto / RSAPublicKey.cs
1 /*
2 * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 using System;
26
27 namespace Crypto {
28
29 /*
30 * This class contains a RSA public key, defined as a modulus and an
31 * exponent. Both use big-endian representation. This class normalizes
32 * the parameters provided to the constructor so that modulus and
33 * exponent use their minimal unsigned big-endian representation.
34 *
35 * Modulus must have length at least 512 bits. Modulus and exponent
36 * must be odd integers.
37 */
38
39 public class RSAPublicKey : IPublicKey {
40
41 public byte[] Modulus {
42 get {
43 return mod;
44 }
45 }
46
47 public byte[] Exponent {
48 get {
49 return e;
50 }
51 }
52
53 public int KeySizeBits {
54 get {
55 return ((mod.Length - 1) << 3)
56 + BigInt.BitLength(mod[0]);
57 }
58 }
59
60 public string AlgorithmName {
61 get {
62 return "RSA";
63 }
64 }
65
66 byte[] mod;
67 byte[] e;
68 int hashCode;
69
70 /*
71 * Create a new instance with the provided element (unsigned,
72 * big-endian). This constructor checks the following rules:
73 *
74 * the modulus size must be at least 512 bits
75 * the modulus must be odd
76 * the exponent must be odd and greater than 1
77 */
78 public RSAPublicKey(byte[] modulus, byte[] exponent)
79 {
80 mod = BigInt.NormalizeBE(modulus, false);
81 e = BigInt.NormalizeBE(exponent, false);
82 if (mod.Length < 64 || (mod.Length == 64 && mod[0] < 0x80)) {
83 throw new CryptoException(
84 "Invalid RSA public key (less than 512 bits)");
85 }
86 if ((mod[mod.Length - 1] & 0x01) == 0) {
87 throw new CryptoException(
88 "Invalid RSA public key (even modulus)");
89 }
90 if (BigInt.IsZero(e)) {
91 throw new CryptoException(
92 "Invalid RSA public key (exponent is zero)");
93 }
94 if (BigInt.IsOne(e)) {
95 throw new CryptoException(
96 "Invalid RSA public key (exponent is one)");
97 }
98 if ((e[e.Length - 1] & 0x01) == 0) {
99 throw new CryptoException(
100 "Invalid RSA public key (even exponent)");
101 }
102
103 /*
104 * A simple hash code that will work well because RSA
105 * keys are in practice quite randomish.
106 */
107 hashCode = (int)(BigInt.HashInt(modulus)
108 ^ BigInt.HashInt(exponent));
109 }
110
111 /*
112 * For a RSA public key, we cannot, in all generality, check
113 * any more things than we already did in the constructor.
114 * Notably, we cannot check whether the public exponent (e)
115 * is indeed relatively prime to phi(n) (the order of the
116 * invertible group modulo n).
117 */
118 public void CheckValid()
119 {
120 /*
121 * We cannot check more than what we already checked in
122 * the constructor.
123 */
124 }
125
126 public override bool Equals(object obj)
127 {
128 RSAPublicKey p = obj as RSAPublicKey;
129 if (p == null) {
130 return false;
131 }
132 return BigInt.Compare(mod, p.mod) == 0
133 && BigInt.Compare(e, p.e) == 0;
134 }
135
136 public override int GetHashCode()
137 {
138 return hashCode;
139 }
140 }
141
142 }