aboutsummaryrefslogtreecommitdiff
path: root/tests/suites/test_suite_bignum_mod.function
blob: 4914e1d89a0e7dca0405edf1abc51196416bbf5a (plain)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/* BEGIN_HEADER */
#include "mbedtls/bignum.h"
#include "mbedtls/entropy.h"
#include "bignum_mod.h"
#include "bignum_mod_raw.h"
#include "constant_time_internal.h"
#include "test/constant_flow.h"

#define TEST_COMPARE_MPI_RESIDUES( a, b ) \
            ASSERT_COMPARE( (a).p, (a).limbs * sizeof(mbedtls_mpi_uint), \
                            (b).p, (b).limbs * sizeof(mbedtls_mpi_uint) )

static int test_read_modulus( mbedtls_mpi_mod_modulus *m,
                              mbedtls_mpi_mod_rep_selector int_rep,
                              char *input )
{
    mbedtls_mpi_uint *p = NULL;
    size_t limbs;

    int ret = mbedtls_test_read_mpi_core( &p, &limbs, input );
    if( ret != 0 )
        return( ret );

    return( mbedtls_mpi_mod_modulus_setup( m, p, limbs, int_rep ) );
}

static int test_read_residue( mbedtls_mpi_mod_residue *r,
                              const mbedtls_mpi_mod_modulus *m,
                              char *input,
                              int skip_limbs_and_value_checks )
{
    mbedtls_mpi_uint *p = NULL;
    size_t limbs;

    int ret = mbedtls_test_read_mpi_core( &p, &limbs, input );
    if( ret != 0 )
        return( ret );

    if( skip_limbs_and_value_checks )
    {
        r->p = p;
        r->limbs = limbs;
        return( 0 );
    }

    /* mbedtls_mpi_mod_residue_setup() checks limbs, and that value < m */
    return( mbedtls_mpi_mod_residue_setup( r, m, p, limbs ) );
}
/* END_HEADER */

/* BEGIN_DEPENDENCIES
 * depends_on:MBEDTLS_BIGNUM_C
 * END_DEPENDENCIES
 */

/* BEGIN_CASE */
void mpi_mod_setup( int int_rep, int iret )
{
    #define MLIMBS 8
    mbedtls_mpi_uint mp[MLIMBS];
    mbedtls_mpi_mod_modulus m;
    int ret;

    memset( mp, 0xFF, sizeof(mp) );

    mbedtls_mpi_mod_modulus_init( &m );
    ret = mbedtls_mpi_mod_modulus_setup( &m, mp, MLIMBS, int_rep );
    TEST_EQUAL( ret, iret );

    /* Only test if the constants have been set-up  */
    if ( ret == 0 && int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
    {
        /* Test that the consts have been calculated */
        TEST_ASSERT( m.rep.mont.rr != NULL );
        TEST_ASSERT( m.rep.mont.mm != 0 );

    }

    /* Address sanitiser should catch if we try to free mp */
    mbedtls_mpi_mod_modulus_free( &m );

    /* Make sure that the modulus doesn't have reference to mp anymore */
    TEST_ASSERT( m.p != mp );

    /* Only test if the constants have been set-up  */
    if ( ret == 0 && int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
    {
        /* Verify the data and pointers allocated have been properly wiped */
        TEST_ASSERT( m.rep.mont.rr == NULL );
        TEST_ASSERT( m.rep.mont.mm == 0 );
    }
exit:
    /* It should be safe to call an mbedtls free several times */
    mbedtls_mpi_mod_modulus_free( &m );

    #undef MLIMBS
}
/* END_CASE */

/* BEGIN MERGE SLOT 1 */

/* END MERGE SLOT 1 */

/* BEGIN MERGE SLOT 2 */

/* BEGIN_CASE */
void mpi_mod_mul( char * input_A,
                  char * input_B,
                  char * input_N,
                  char * result )
{
    mbedtls_mpi_uint *X = NULL;

    mbedtls_mpi_mod_modulus m;
    mbedtls_mpi_mod_modulus_init( &m );

    TEST_EQUAL( test_read_modulus( &m, MBEDTLS_MPI_MOD_REP_MONTGOMERY, input_N ),
                0 );

    mbedtls_mpi_mod_residue rA;
    TEST_EQUAL( test_read_residue( &rA, &m, input_A, 0 ), 0 );

    mbedtls_mpi_mod_residue rB;
    TEST_EQUAL( test_read_residue( &rB, &m, input_B, 0 ), 0 );

    mbedtls_mpi_mod_residue rR;
    TEST_EQUAL( test_read_residue( &rR, &m, result, 0 ), 0 );

    const size_t limbs = m.limbs;
    const size_t bytes = limbs * sizeof( mbedtls_mpi_uint );

    TEST_EQUAL( rA.limbs, limbs );
    TEST_EQUAL( rB.limbs, limbs );
    TEST_EQUAL( rR.limbs, limbs );

    ASSERT_ALLOC( X, limbs );

    mbedtls_mpi_mod_residue rX;
    TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rX, &m, X, limbs ), 0 );

    TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rB, &m ), 0 );
    ASSERT_COMPARE( rX.p, bytes, rR.p, bytes );

    /* alias X to A */
    memcpy( rX.p, rA.p, bytes );
    TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rX, &rB, &m ), 0 );
    ASSERT_COMPARE( rX.p, bytes, rR.p, bytes );

    /* alias X to B */
    memcpy( rX.p, rB.p, bytes );
    TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rX, &m ), 0);
    ASSERT_COMPARE( rX.p, bytes, rR.p, bytes );

    /* A == B: alias A and B */
    if( memcmp( rA.p, rB.p, bytes ) == 0 )
    {
        TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rA, &m ), 0 );
        ASSERT_COMPARE( rX.p, bytes, rR.p, bytes );

        /* X, A, B all aliased together */
        memcpy( rX.p, rA.p, bytes );
        TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rX, &rX, &m ), 0 );
        ASSERT_COMPARE( rX.p, bytes, rR.p, bytes );
    }

    /* A != B: test B * A */
    else
    {
        TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rB, &rA, &m ), 0 );
        ASSERT_COMPARE( rX.p, bytes, rR.p, bytes );

        /* B * A: alias X to A */
        memcpy( rX.p, rA.p, bytes );
        TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rB, &rX, &m ), 0 );
        ASSERT_COMPARE( rX.p, bytes, rR.p, bytes );

        /* B + A: alias X to B */
        memcpy( rX.p, rB.p, bytes );
        TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rX, &rA, &m ), 0 );
        ASSERT_COMPARE( rX.p, bytes, rR.p, bytes );
    }

exit:
    mbedtls_free( rA.p );
    mbedtls_free( rB.p );
    mbedtls_free( rR.p );
    mbedtls_free( X );
    mbedtls_free( (mbedtls_mpi_uint *) m.p );

    mbedtls_mpi_mod_modulus_free( &m );
}
/* END_CASE */

/* BEGIN_CASE */
void mpi_mod_mul_neg( char * input_A,
                      char * input_B,
                      char * input_N,
                      char * result,
                      int exp_ret )
{
    mbedtls_mpi_uint *X = NULL;

    mbedtls_mpi_mod_modulus m;
    mbedtls_mpi_mod_modulus_init( &m );

    TEST_EQUAL( test_read_modulus( &m, MBEDTLS_MPI_MOD_REP_MONTGOMERY, input_N ),
                0 );

    mbedtls_mpi_mod_residue rA;
    TEST_EQUAL( test_read_residue( &rA, &m, input_A, 1 ), 0 );

    mbedtls_mpi_mod_residue rB;
    TEST_EQUAL( test_read_residue( &rB, &m, input_B, 1 ), 0 );

    mbedtls_mpi_mod_residue rR;
    TEST_EQUAL( test_read_residue( &rR, &m, result, 1 ), 0 );

    const size_t limbs = m.limbs;

    ASSERT_ALLOC( X, limbs );

    mbedtls_mpi_mod_residue rX;
    TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rX, &m, X, limbs ), 0 );
    rX.limbs = rR.limbs;

    TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rB, &m ), exp_ret );

    mbedtls_mpi_mod_modulus fake_m;
    mbedtls_mpi_mod_modulus_init( &fake_m );

    /* Check when m is not initialized */
    TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rB, &fake_m ),
                MBEDTLS_ERR_MPI_BAD_INPUT_DATA );

exit:
    mbedtls_free( rA.p );
    mbedtls_free( rB.p );
    mbedtls_free( rR.p );
    mbedtls_free( X );
    mbedtls_free( (mbedtls_mpi_uint *) m.p );

    mbedtls_mpi_mod_modulus_free( &m );
    mbedtls_mpi_mod_modulus_free( &fake_m );
}
/* END_CASE */

/* END MERGE SLOT 2 */

/* BEGIN MERGE SLOT 3 */
/* BEGIN_CASE */
void mpi_mod_sub( char * input_N,
                  char * input_A, char * input_B,
                  char * input_D, int expected_ret )
{
    mbedtls_mpi_mod_residue a = { NULL, 0 };
    mbedtls_mpi_mod_residue b = { NULL, 0 };
    mbedtls_mpi_mod_residue d = { NULL, 0 };
    mbedtls_mpi_mod_residue x = { NULL, 0 };
    mbedtls_mpi_uint *X_raw = NULL;

    mbedtls_mpi_mod_modulus m;
    mbedtls_mpi_mod_modulus_init( &m );

    TEST_EQUAL( 0,
        test_read_modulus( &m, MBEDTLS_MPI_MOD_REP_MONTGOMERY, input_N ) );

    /* test_read_residue() normally checks that inputs have the same number of
     * limbs as the modulus. For negative testing we can ask it to skip this
     * with a non-zero final parameter. */
    TEST_EQUAL( 0, test_read_residue( &a, &m, input_A, expected_ret != 0 ) );
    TEST_EQUAL( 0, test_read_residue( &b, &m, input_B, expected_ret != 0 ) );
    TEST_EQUAL( 0, test_read_residue( &d, &m, input_D, expected_ret != 0 ) );

    size_t limbs = m.limbs;
    size_t bytes = limbs * sizeof( *X_raw );

    if( expected_ret == 0 )
    {
        /* Negative test with too many limbs in output */
        ASSERT_ALLOC( X_raw, limbs + 1 );

        x.p = X_raw;
        x.limbs = limbs + 1;
        TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
                    mbedtls_mpi_mod_sub( &x, &a, &b, &m ) );

        mbedtls_free( X_raw );
        X_raw = NULL;

        /* Negative test with too few limbs in output */
        if( limbs > 1 )
        {
            ASSERT_ALLOC( X_raw, limbs - 1 );

            x.p = X_raw;
            x.limbs = limbs - 1;
            TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
                        mbedtls_mpi_mod_sub( &x, &a, &b, &m ) );

            mbedtls_free( X_raw );
            X_raw = NULL;
        }

        /* Negative testing with too many/too few limbs in a and b is covered by
         * manually-written test cases with expected_ret != 0. */
    }

    ASSERT_ALLOC( X_raw, limbs );

    TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &x, &m, X_raw, limbs ) );

    /* a - b => Correct result, or expected error */
    TEST_EQUAL( expected_ret, mbedtls_mpi_mod_sub( &x, &a, &b, &m ) );
    if( expected_ret != 0 )
        goto exit;

    TEST_COMPARE_MPI_RESIDUES( x, d );

    /* a - b: alias x to a => Correct result */
    memcpy( x.p, a.p, bytes );
    TEST_EQUAL( 0, mbedtls_mpi_mod_sub( &x, &x, &b, &m ) );
    TEST_COMPARE_MPI_RESIDUES( x, d );

    /* a - b: alias x to b => Correct result */
    memcpy( x.p, b.p, bytes );
    TEST_EQUAL( 0, mbedtls_mpi_mod_sub( &x, &a, &x, &m ) );
    TEST_COMPARE_MPI_RESIDUES( x, d );

    if ( memcmp( a.p, b.p, bytes ) == 0 )
    {
        /* a == b: alias a and b */

        /* a - a => Correct result */
        TEST_EQUAL( 0, mbedtls_mpi_mod_sub( &x, &a, &a, &m ) );
        TEST_COMPARE_MPI_RESIDUES( x, d );

        /* a - a: x, a, b all aliased together => Correct result */
        memcpy( x.p, a.p, bytes );
        TEST_EQUAL( 0, mbedtls_mpi_mod_sub( &x, &x, &x, &m ) );
        TEST_COMPARE_MPI_RESIDUES( x, d );
    }

exit:
    mbedtls_free( (void *)m.p ); /* mbedtls_mpi_mod_modulus_free() sets m.p = NULL */
    mbedtls_mpi_mod_modulus_free( &m );

    mbedtls_free( a.p );
    mbedtls_free( b.p );
    mbedtls_free( d.p );
    mbedtls_free( X_raw );
}
/* END_CASE */

/* BEGIN_CASE */
void mpi_mod_inv_mont( char * input_N,
                       char * input_A, char * input_I,
                       int expected_ret )
{
    mbedtls_mpi_mod_residue a = { NULL, 0 };    /* argument */
    mbedtls_mpi_mod_residue i = { NULL, 0 };    /* expected inverse wrt N */
    mbedtls_mpi_mod_residue x = { NULL, 0 };    /* output */
    mbedtls_mpi_uint *X_raw = NULL;

    mbedtls_mpi_mod_modulus N;
    mbedtls_mpi_mod_modulus_init( &N );

    TEST_EQUAL( 0,
        test_read_modulus( &N, MBEDTLS_MPI_MOD_REP_MONTGOMERY, input_N ) );

    /* test_read_residue() normally checks that inputs have the same number of
     * limbs as the modulus. For negative testing we can ask it to skip this
     * with a non-zero final parameter. */
    TEST_EQUAL( 0, test_read_residue( &a, &N, input_A, expected_ret != 0 ) );
    TEST_EQUAL( 0, test_read_residue( &i, &N, input_I, expected_ret != 0 ) );

    size_t limbs = N.limbs;
    size_t bytes = limbs * sizeof( *X_raw );

    ASSERT_ALLOC( X_raw, limbs );

    TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &x, &N, X_raw, limbs ) );

    TEST_EQUAL( expected_ret, mbedtls_mpi_mod_inv( &x, &a, &N ) );
    if( expected_ret == 0 )
    {
        TEST_COMPARE_MPI_RESIDUES( x, i );

        /* a^-1: alias x to a => Correct result */
        memcpy( x.p, a.p, bytes );
        TEST_EQUAL( 0, mbedtls_mpi_mod_inv( &x, &x, &N ) );
        TEST_COMPARE_MPI_RESIDUES( x, i );
    }

exit:
    mbedtls_free( (void *)N.p ); /* mbedtls_mpi_mod_modulus_free() sets N.p = NULL */
    mbedtls_mpi_mod_modulus_free( &N );

    mbedtls_free( a.p );
    mbedtls_free( i.p );
    mbedtls_free( X_raw );
}
/* END_CASE */

/* BEGIN_CASE */
void mpi_mod_inv_non_mont( char * input_N,
                           char * input_A, char * input_I,
                           int expected_ret )
{
    mbedtls_mpi_mod_residue a = { NULL, 0 };    /* argument */
    mbedtls_mpi_mod_residue i = { NULL, 0 };    /* expected inverse wrt N */
    mbedtls_mpi_mod_residue x = { NULL, 0 };    /* output */
    mbedtls_mpi_uint *X_raw = NULL;

    mbedtls_mpi_mod_modulus N;
    mbedtls_mpi_mod_modulus_init( &N );

    TEST_EQUAL( 0,
        test_read_modulus( &N, MBEDTLS_MPI_MOD_REP_OPT_RED, input_N ) );

    /* test_read_residue() normally checks that inputs have the same number of
     * limbs as the modulus. For negative testing we can ask it to skip this
     * with a non-zero final parameter. */
    TEST_EQUAL( 0, test_read_residue( &a, &N, input_A, expected_ret != 0 ) );
    TEST_EQUAL( 0, test_read_residue( &i, &N, input_I, expected_ret != 0 ) );

    size_t limbs = N.limbs;
    size_t bytes = limbs * sizeof( *X_raw );

    ASSERT_ALLOC( X_raw, limbs );

    TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &x, &N, X_raw, limbs ) );

    TEST_EQUAL( expected_ret, mbedtls_mpi_mod_inv( &x, &a, &N ) );
    if( expected_ret == 0 )
    {
        TEST_COMPARE_MPI_RESIDUES( x, i );

        /* a^-1: alias x to a => Correct result */
        memcpy( x.p, a.p, bytes );
        TEST_EQUAL( 0, mbedtls_mpi_mod_inv( &x, &x, &N ) );
        TEST_COMPARE_MPI_RESIDUES( x, i );
    }

exit:
    mbedtls_free( (void *)N.p ); /* mbedtls_mpi_mod_modulus_free() sets N.p = NULL */
    mbedtls_mpi_mod_modulus_free( &N );

    mbedtls_free( a.p );
    mbedtls_free( i.p );
    mbedtls_free( X_raw );
}
/* END_CASE */
/* END MERGE SLOT 3 */

/* BEGIN MERGE SLOT 4 */

/* END MERGE SLOT 4 */

/* BEGIN MERGE SLOT 5 */
/* BEGIN_CASE */
void mpi_mod_add( char * input_N,
                  char * input_A, char * input_B,
                  char * input_S, int expected_ret )
{
    mbedtls_mpi_mod_residue a = { NULL, 0 };
    mbedtls_mpi_mod_residue b = { NULL, 0 };
    mbedtls_mpi_mod_residue s = { NULL, 0 };
    mbedtls_mpi_mod_residue x = { NULL, 0 };
    mbedtls_mpi_uint *X_raw = NULL;

    mbedtls_mpi_mod_modulus m;
    mbedtls_mpi_mod_modulus_init( &m );

    TEST_EQUAL( 0,
        test_read_modulus( &m, MBEDTLS_MPI_MOD_REP_MONTGOMERY, input_N ) );

    /* test_read_residue() normally checks that inputs have the same number of
     * limbs as the modulus. For negative testing we can ask it to skip this
     * with a non-zero final parameter. */
    TEST_EQUAL( 0, test_read_residue( &a, &m, input_A, expected_ret != 0 ) );
    TEST_EQUAL( 0, test_read_residue( &b, &m, input_B, expected_ret != 0 ) );
    TEST_EQUAL( 0, test_read_residue( &s, &m, input_S, expected_ret != 0 ) );

    size_t limbs = m.limbs;
    size_t bytes = limbs * sizeof( *X_raw );

    if( expected_ret == 0 )
    {
        /* Negative test with too many limbs in output */
        ASSERT_ALLOC( X_raw, limbs + 1 );

        x.p = X_raw;
        x.limbs = limbs + 1;
        TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
                    mbedtls_mpi_mod_add( &x, &a, &b, &m ) );

        mbedtls_free( X_raw );
        X_raw = NULL;

        /* Negative test with too few limbs in output */
        if( limbs > 1 )
        {
            ASSERT_ALLOC( X_raw, limbs - 1 );

            x.p = X_raw;
            x.limbs = limbs - 1;
            TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
                        mbedtls_mpi_mod_add( &x, &a, &b, &m ) );

            mbedtls_free( X_raw );
            X_raw = NULL;
        }

        /* Negative testing with too many/too few limbs in a and b is covered by
         * manually-written test cases with oret != 0. */
    }

    /* Allocate correct number of limbs for X_raw */
    ASSERT_ALLOC( X_raw, limbs );

    TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &x, &m, X_raw, limbs ) );

    /* A + B => Correct result or expected error */
    TEST_EQUAL( expected_ret, mbedtls_mpi_mod_add( &x, &a, &b, &m ) );
    if( expected_ret != 0 )
        goto exit;

    TEST_COMPARE_MPI_RESIDUES( x, s );

    /* a + b: alias x to a => Correct result */
    memcpy( x.p, a.p, bytes );
    TEST_EQUAL( 0, mbedtls_mpi_mod_add( &x, &x, &b, &m ) );
    TEST_COMPARE_MPI_RESIDUES( x, s );

    /* a + b: alias x to b => Correct result */
    memcpy( x.p, b.p, bytes );
    TEST_EQUAL( 0, mbedtls_mpi_mod_add( &x, &a, &x, &m ) );
    TEST_COMPARE_MPI_RESIDUES( x, s );

    if ( memcmp( a.p, b.p, bytes ) == 0 )
    {
        /* a == b: alias a and b */

        /* a + a => Correct result */
        TEST_EQUAL( 0, mbedtls_mpi_mod_add( &x, &a, &a, &m ) );
        TEST_COMPARE_MPI_RESIDUES( x, s );

        /* a + a: x, a, b all aliased together => Correct result */
        memcpy( x.p, a.p, bytes );
        TEST_EQUAL( 0, mbedtls_mpi_mod_add( &x, &x, &x, &m ) );
        TEST_COMPARE_MPI_RESIDUES( x, s );
    }

exit:
    mbedtls_free( (void *)m.p ); /* mbedtls_mpi_mod_modulus_free() sets m.p = NULL */
    mbedtls_mpi_mod_modulus_free( &m );

    mbedtls_free( a.p );
    mbedtls_free( b.p );
    mbedtls_free( s.p );
    mbedtls_free( X_raw );
}
/* END_CASE */
/* END MERGE SLOT 5 */

/* BEGIN MERGE SLOT 6 */

/* END MERGE SLOT 6 */

/* BEGIN MERGE SLOT 7 */
/* BEGIN_CASE */
void mpi_residue_setup( char * input_N, char * input_R, int ret )
{
    mbedtls_mpi_uint *N = NULL;
    mbedtls_mpi_uint *R = NULL;
    size_t n_limbs, r_limbs;
    mbedtls_mpi_mod_modulus m;
    mbedtls_mpi_mod_residue r;

    mbedtls_mpi_mod_modulus_init( &m );

    /* Allocate the memory for intermediate data structures */
    TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, input_N ) );
    TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &R, &r_limbs, input_R ) );

    TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs,
                MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );

    TEST_EQUAL( ret, mbedtls_mpi_mod_residue_setup( &r, &m, R , r_limbs ) );

    if ( ret == 0 )
    {
        TEST_EQUAL( r.limbs, r_limbs );
        TEST_ASSERT( r.p == R );
    }

exit:
    mbedtls_mpi_mod_modulus_free( &m );
    mbedtls_free( N );
    mbedtls_free( R );
}
/* END_CASE */

/* BEGIN_CASE */
void mpi_mod_io_neg( char * input_N, data_t * buf, int ret )
{
    mbedtls_mpi_uint *N = NULL;
    mbedtls_mpi_uint *R = NULL;

    mbedtls_mpi_mod_modulus m;
    mbedtls_mpi_mod_residue r = { NULL, 0 };
    mbedtls_mpi_mod_ext_rep endian = MBEDTLS_MPI_MOD_EXT_REP_LE;

    mbedtls_mpi_mod_modulus_init( &m );

    size_t n_limbs;
    TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, input_N ) );
    size_t r_limbs = n_limbs;
    ASSERT_ALLOC( R, r_limbs );

    /* modulus->p == NULL || residue->p == NULL ( m has not been set-up ) */
    TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
                mbedtls_mpi_mod_read( &r, &m, buf->x, buf->len, endian ) );

    TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
                mbedtls_mpi_mod_write( &r, &m, buf->x, buf->len, endian ) );

    /* Set up modulus and test with residue->p == NULL */
    TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs,
                MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );

    TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
                mbedtls_mpi_mod_read( &r, &m, buf->x, buf->len, endian ) );
    TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
                mbedtls_mpi_mod_write( &r, &m, buf->x, buf->len, endian ) );

    /* Do the rest of the tests with a residue set up with the input data */
    TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &r, &m, R , r_limbs ) );

    /* Fail for r_limbs < m->limbs */
    r.limbs--;
    TEST_ASSERT( r.limbs < m.limbs );
    TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
                mbedtls_mpi_mod_read( &r, &m, buf->x, buf->len, endian ) );
    TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
                mbedtls_mpi_mod_write( &r, &m, buf->x, buf->len, endian ) );
    r.limbs++;

    /* Fail for r_limbs > m->limbs */
    m.limbs--;
    TEST_ASSERT( r.limbs > m.limbs );
    TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
                mbedtls_mpi_mod_read( &r, &m, buf->x, buf->len, endian ) );
    TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
                mbedtls_mpi_mod_write( &r, &m, buf->x, buf->len, endian ) );
    m.limbs++;

    /* Test the read */
    TEST_EQUAL( ret, mbedtls_mpi_mod_read( &r, &m, buf->x, buf->len, endian ) );

    /* Test write overflow only when the representation is large and read is successful  */
    if ( r.limbs > 1 && ret == 0 )
        TEST_EQUAL( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL,
                        mbedtls_mpi_mod_write( &r, &m, buf->x, 1, endian ) );

exit:
    mbedtls_mpi_mod_residue_release( &r );
    mbedtls_mpi_mod_modulus_free( &m );
    mbedtls_free( N );
    mbedtls_free( R );
}
/* END_CASE */

/* BEGIN_CASE */
void mpi_mod_io( char * input_N, data_t * input_A, int endian )
{
    mbedtls_mpi_uint *N = NULL;
    mbedtls_mpi_uint *R = NULL;
    mbedtls_mpi_uint *R_COPY = NULL;
    unsigned char *obuf = NULL;
    unsigned char *ref_buf = NULL;
    mbedtls_mpi_mod_modulus m;
    mbedtls_mpi_mod_residue r;
    mbedtls_mpi_mod_residue r_copy;
    size_t n_limbs, n_bytes, a_bytes;

    mbedtls_mpi_mod_modulus_init( &m );

    /* Read inputs */
    TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, input_N ) );
    n_bytes = n_limbs * sizeof( mbedtls_mpi_uint );
    a_bytes = input_A->len;

    /* Allocate the memory for intermediate data structures */
    ASSERT_ALLOC( R, n_bytes );
    ASSERT_ALLOC( R_COPY, n_bytes );

    /* Test that input's size is not greater to modulo's */
    TEST_LE_U( a_bytes, n_bytes );

    /* Init Structures */
    TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs,
                MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );

    /* Enforcing p_limbs >= m->limbs */
    TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &r, &m, R, n_limbs ) );

    TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r, &m, input_A->x, input_A->len,
                                         endian ) );

    /* Read a copy for checking that writing didn't change the value of r */
    TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &r_copy, &m,
                                                  R_COPY, n_limbs ) );
    TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r_copy, &m, input_A->x, input_A->len,
                                         endian ) );

    /* Get number of bytes without leading zeroes */
    size_t a_bytes_trimmed = a_bytes;
    while( a_bytes_trimmed > 0 )
    {
        unsigned char* r_byte_array = (unsigned char*) r.p;
        if( r_byte_array[--a_bytes_trimmed] != 0 )
            break;
    }
    a_bytes_trimmed++;

    /* Test write with three output buffer sizes: tight, same as input and
     * longer than the input */
    size_t obuf_sizes[3];
    const size_t obuf_sizes_len = sizeof( obuf_sizes ) / sizeof( obuf_sizes[0] );
    obuf_sizes[0] = a_bytes_trimmed;
    obuf_sizes[1] = a_bytes;
    obuf_sizes[2] = a_bytes + 8;

    for( size_t i = 0; i < obuf_sizes_len; i++ )
    {
        ASSERT_ALLOC( obuf, obuf_sizes[i] );
        TEST_EQUAL( 0, mbedtls_mpi_mod_write( &r, &m, obuf, obuf_sizes[i], endian ) );

        /* Make sure that writing didn't corrupt the value of r */
        ASSERT_COMPARE( r.p, r.limbs, r_copy.p, r_copy.limbs );

        /* Set up reference output for checking the result */
        ASSERT_ALLOC( ref_buf, obuf_sizes[i] );
        switch( endian )
        {
            case MBEDTLS_MPI_MOD_EXT_REP_LE:
                memcpy( ref_buf, input_A->x, a_bytes_trimmed );
                break;
            case MBEDTLS_MPI_MOD_EXT_REP_BE:
                {
                    size_t a_offset = input_A->len - a_bytes_trimmed;
                    size_t ref_offset = obuf_sizes[i] - a_bytes_trimmed;
                    memcpy( ref_buf + ref_offset, input_A->x + a_offset,
                            a_bytes_trimmed );
                }
                break;
            default:
                TEST_ASSERT( 0 );
        }

        /* Check the result */
        ASSERT_COMPARE( obuf, obuf_sizes[i], ref_buf, obuf_sizes[i] );

        mbedtls_free( ref_buf );
        ref_buf = NULL;
        mbedtls_free( obuf );
        obuf = NULL;
    }

exit:
    mbedtls_mpi_mod_modulus_free( &m );
    mbedtls_free( N );
    mbedtls_free( R );
    mbedtls_free( R_COPY );
    mbedtls_free( obuf );
}
/* END_CASE */
/* END MERGE SLOT 7 */

/* BEGIN MERGE SLOT 8 */

/* END MERGE SLOT 8 */

/* BEGIN MERGE SLOT 9 */

/* END MERGE SLOT 9 */

/* BEGIN MERGE SLOT 10 */

/* END MERGE SLOT 10 */