2 * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
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.
35 * Modulus must have length at least 512 bits. Modulus and exponent
36 * must be odd integers.
39 public class RSAPublicKey : IPublicKey {
41 public byte[] Modulus {
47 public byte[] Exponent {
53 public int KeySizeBits {
55 return ((mod.Length - 1) << 3)
56 + BigInt.BitLength(mod[0]);
60 public string AlgorithmName {
71 * Create a new instance with the provided element (unsigned,
72 * big-endian). This constructor checks the following rules:
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
78 public RSAPublicKey(byte[] modulus, byte[] exponent)
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)");
86 if ((mod[mod.Length - 1] & 0x01) == 0) {
87 throw new CryptoException(
88 "Invalid RSA public key (even modulus)");
90 if (BigInt.IsZero(e)) {
91 throw new CryptoException(
92 "Invalid RSA public key (exponent is zero)");
94 if (BigInt.IsOne(e)) {
95 throw new CryptoException(
96 "Invalid RSA public key (exponent is one)");
98 if ((e[e.Length - 1] & 0x01) == 0) {
99 throw new CryptoException(
100 "Invalid RSA public key (even exponent)");
104 * A simple hash code that will work well because RSA
105 * keys are in practice quite randomish.
107 hashCode = (int)(BigInt.HashInt(modulus)
108 ^ BigInt.HashInt(exponent));
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).
118 public void CheckValid()
121 * We cannot check more than what we already checked in
126 public override bool Equals(object obj)
128 RSAPublicKey p = obj as RSAPublicKey;
132 return BigInt.Compare(mod, p.mod) == 0
133 && BigInt.Compare(e, p.e) == 0;
136 public override int GetHashCode()