1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//! An implementation of the SM2 signature standard.
//!
//! ## Usage
//! ```
//! extern crate yogcrypt;
//! use yogcrypt::sm2::{get_sec_key, get_pub_key, sm2_gen_sign, sm2_ver_sign};
//!
//! let sk = get_sec_key();
//! let msg = b"Hello World!";
//!
//! let pk = get_pub_key(sk);
//!
//! let mut tag = sm2_gen_sign(msg, sk, pk);
//!
//! let t = sm2_ver_sign(msg, pk, &tag);
//!
//! // Signature is accepted
//! assert!(t);
//! ```
//!
//! ## Reference
//! Most variable's name in the source code are in accordance with the document.
//!
//! [OSCCA: SM2 document](http://www.oscca.gov.cn/sca/xxgk/2010-12/17/1002386/files/b791a9f908bb4803875ab6aeeb7b4e03.pdf)
use basic::field::field_n::*;
use basic::group::ecc_group::*;
use basic::util::bytes_to_u32_blocks;
use sm3::*;

pub use basic::group::ecc_group::Coordinate;

pub use basic::cell::u64x4::U64x4;

/// A public key is a point on the elliptic curve group.
///
/// ## Usage
/// ```no-run
///     extern crate yogcrypt;
///     use yogcrypt::sm2::*;
///
///     let x = Coordinate::from_u64([0x7b706375_42fb998e, 0x00f78ba4_e992817f, 0x6fcbd376_2040ded0, 0xa06c2b7e_e7b810bd]);
///     let y = Coordinate::from_u64([0xd8cb4986_918e9375, 0x3055dfcc_d2870256, 0x973ccca3_1d33bd55, 0xf6fed50c_fd14ede7]);
///     let pk = PubKey::new(x,y);
/// ```
pub type PubKey = Point;

/// A secret key is an element from the field with order `MODULO_N`.
///
/// ## Usage
/// ```no_run
///     extern crate yogcrypt;
///     use yogcrypt::sm2::*;
///
///     let x: SecKey = get_sec_key();
/// ```
pub type SecKey = U64x4;

/// A signature (tag) consists of two fields: `r` and `s`, each is a 256 bit number
pub struct Signature {
    pub r: U64x4,
    pub s: U64x4,
}

/// Randomly sample secret key uniformly from [0,..n), where n is the order of the base point
pub fn get_sec_key() -> SecKey {
    let mut k = U64x4::random();
    while k >= MODULO_N {
        k = U64x4::random()
    }
    k
}

/// Compute public key from secret key
///
/// By definition public key is computed by pk = [sk] G, where G is the base point.
pub fn get_pub_key(d: SecKey) -> PubKey {
    let rst_jacobi = times_base_point(d);
    jacobi_to_affine(rst_jacobi)
}

/// Compute context `Z` as specified in standard document
fn get_z(q: PubKey) -> [u32; 8] {
    let _len: usize = 2 + 14 + 6 * 32;
    let mut s: [u32; 52] = [0; 52];

    s[0] = 0x00D00102;
    s[1] = 0x03040506;
    s[2] = 0x0708090A;
    s[3] = 0x0B0C0D0E;

    s[4] = (ECC_A.value(3) >> 32) as u32;
    s[5] = ECC_A.value(3) as u32;
    s[6] = (ECC_A.value(2) >> 32) as u32;
    s[7] = ECC_A.value(2) as u32;
    s[8] = (ECC_A.value(1) >> 32) as u32;
    s[9] = ECC_A.value(1) as u32;
    s[10] = (ECC_A.value(0) >> 32) as u32;
    s[11] = ECC_A.value(0) as u32;

    s[12] = (ECC_B.value(3) >> 32) as u32;
    s[13] = ECC_B.value(3) as u32;
    s[14] = (ECC_B.value(2) >> 32) as u32;
    s[15] = ECC_B.value(2) as u32;
    s[16] = (ECC_B.value(1) >> 32) as u32;
    s[17] = ECC_B.value(1) as u32;
    s[18] = (ECC_B.value(0) >> 32) as u32;
    s[19] = ECC_B.value(0) as u32;

    s[20] = (ECC_G.x.value(3) >> 32) as u32;
    s[21] = ECC_G.x.value(3) as u32;
    s[22] = (ECC_G.x.value(2) >> 32) as u32;
    s[23] = ECC_G.x.value(2) as u32;
    s[24] = (ECC_G.x.value(1) >> 32) as u32;
    s[25] = ECC_G.x.value(1) as u32;
    s[26] = (ECC_G.x.value(0) >> 32) as u32;
    s[27] = ECC_G.x.value(0) as u32;

    s[28] = (ECC_G.y.value(3) >> 32) as u32;
    s[29] = ECC_G.y.value(3) as u32;
    s[30] = (ECC_G.y.value(2) >> 32) as u32;
    s[31] = ECC_G.y.value(2) as u32;
    s[32] = (ECC_G.y.value(1) >> 32) as u32;
    s[33] = ECC_G.y.value(1) as u32;
    s[34] = (ECC_G.y.value(0) >> 32) as u32;
    s[35] = ECC_G.y.value(0) as u32;

    s[36] = (q.x.value(3) >> 32) as u32;
    s[37] = q.x.value(3) as u32;
    s[38] = (q.x.value(2) >> 32) as u32;
    s[39] = q.x.value(2) as u32;
    s[40] = (q.x.value(1) >> 32) as u32;
    s[41] = q.x.value(1) as u32;
    s[42] = (q.x.value(0) >> 32) as u32;
    s[43] = q.x.value(0) as u32;

    s[44] = (q.y.value(3) >> 32) as u32;
    s[45] = q.y.value(3) as u32;
    s[46] = (q.y.value(2) >> 32) as u32;
    s[47] = q.y.value(2) as u32;
    s[48] = (q.y.value(1) >> 32) as u32;
    s[49] = q.y.value(1) as u32;
    s[50] = (q.y.value(0) >> 32) as u32;
    s[51] = q.y.value(0) as u32;

    //Z = sm3Enc(&s[0..52], 52 * 32)
    sm3_enc_inner(&s[0..52], 52 * 32)
}

/// Generate a valid signature for a message using a pair of keys.
///
/// **Note**: The underlying hash function is `sm3`.
pub fn sm2_gen_sign(msg: &[u8], d: SecKey, q: PubKey) -> Signature {
    let (msg, bit_len) = bytes_to_u32_blocks(msg);
    sm2_gen_sign_inner(&msg[..], d, q, bit_len)
}

/// Core function for generation with specified input length
pub(crate) fn sm2_gen_sign_inner(msg: &[u32], d: SecKey, q: PubKey, len: usize) -> Signature {
    // verify that Q is indeed on the curve
    // to prevent false curve attack
    assert!(is_on_curve(q), "Public key not on curve!");

    let z = get_z(q);

    let m = [msg, &z].concat();

    // compute the hash value
    let e = sm3_enc_inner(&m, (len + 8) * 32);
    let mut e = U64x4::new(
        u64::from(e[7]) | (u64::from(e[6]) << 32),
        u64::from(e[5]) | (u64::from(e[4]) << 32),
        u64::from(e[3]) | (u64::from(e[2]) << 32),
        u64::from(e[1]) | (u64::from(e[0]) << 32),
    );

    e = to_mod_n(e);

    let mut s = U64x4::zero();
    let mut r = U64x4::zero();
    while s.equal_to_zero() {
        // ephemeral key
        let k = get_sec_key();

        let p_jacobi = times_base_point(k);
        let p = jacobi_to_affine(p_jacobi);

        r = add_mod_n(e, p.x.num);
        if r.equal_to_zero() || add_mod_n(r, k).equal_to_zero() {
            continue;
        }

        // Calculate s = (1+d)^-1 * (k-r*d);
        s = mul_mod_n(
            get_mul_inv_mod_n(add_mod_n(d, U64x4::new(1, 0, 0, 0))), //(1+d)^-1
            sub_mod_n(k, mul_mod_n(r, d)),                           //k-r*d
        );
    }

    Signature { r, s }
}

/// Verify a signature on a given message using public key
///
/// **Note**: The underlying hash function is `sm3`.
pub fn sm2_ver_sign(msg: &[u8], q: PubKey, sig: &Signature) -> bool {
    let (msg, bit_len) = bytes_to_u32_blocks(msg);
    sm2_ver_sign_inner(&msg[..], q, bit_len, sig)
}

/// Core function for verification with specified input length
pub(crate) fn sm2_ver_sign_inner(msg: &[u32], q: PubKey, len: usize, sig: &Signature) -> bool {
    // verify that Q is indeed on the curve
    // to prevent false curve attack
    assert!(is_on_curve(q), "public key not on curve!");
    let r = sig.r;
    let s = sig.s;

    if r >= MODULO_N || r.equal_to_zero() {
        return false;
    }
    if s >= MODULO_N || s.equal_to_zero() {
        return false;
    }
    let z_a = get_z(q);
    let m = [msg, &z_a].concat();

    let e = sm3_enc_inner(&m, (len + 8) * 32);
    let e = U64x4::new(
        u64::from(e[7]) | (u64::from(e[6]) << 32),
        u64::from(e[5]) | (u64::from(e[4]) << 32),
        u64::from(e[3]) | (u64::from(e[2]) << 32),
        u64::from(e[1]) | (u64::from(e[0]) << 32),
    );

    let t = add_mod_n(r, s);
    if t.equal_to_zero() {
        return false;
    }

    let p_jacobi = add_jacobi_point(times_base_point(s), times_point(q, t));
    let p = jacobi_to_affine(p_jacobi);

    let e1 = to_mod_n(e);
    let x1 = to_mod_n(p.x.num);
    let r2 = add_mod_n(e1, x1);

    // accept if r2 = r
    r2 == r
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test() {
        for _ in 0..10000 {
            let d_a = U64x4::random();

            let msg = [
                0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54,
                0x32, 0x10,
            ];

            let q = get_pub_key(d_a);

            let mut m = sm2_gen_sign(&msg, d_a, q);

            let t = sm2_ver_sign(&msg, q, &m);
            assert!(t);
        }
    }
}