df54b47c0bd65154420d37f4fb6658c2e8dfb1dd
[openssl.git] / crypto / ec / ec.c
1 /*
2  *
3  *      ec.c
4  *
5  *      Elliptic Curve Arithmetic Functions
6  *
7  *      Copyright (C) Lenka Fibikova 2000
8  *
9  *
10  */
11
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <assert.h>
16
17 #include "ec.h"
18
19
20
21 EC *EC_new()
22 {
23         EC *ret;
24
25         ret=(EC *)malloc(sizeof(EC));
26         if (ret == NULL) return NULL;
27         ret->A = BN_new();
28         ret->B = BN_new();
29         ret->p = BN_new();
30         ret->h = BN_new();
31         ret->is_in_mont = 0;
32
33         if (ret->A == NULL || ret->B == NULL || ret->p == NULL || ret->h == NULL)
34         {
35                 if (ret->A != NULL) BN_free(ret->A);
36                 if (ret->B != NULL) BN_free(ret->B);
37                 if (ret->p != NULL) BN_free(ret->p);
38                 if (ret->h != NULL) BN_free(ret->h);
39                 free(ret);
40                 return(NULL);
41         }
42         return(ret);
43 }
44
45
46 void EC_clear_free(EC *E)
47 {
48         if (E == NULL) return;
49
50         if (E->A != NULL) BN_clear_free(E->A);
51         if (E->B != NULL) BN_clear_free(E->B);
52         if (E->p != NULL) BN_clear_free(E->p);
53         if (E->h != NULL) BN_clear_free(E->h);
54         E->is_in_mont = 0;
55         free(E);
56 }
57
58
59 #ifdef MONTGOMERY
60 int EC_to_montgomery(EC *E, BN_MONTGOMERY *mont, BN_CTX *ctx)
61 {
62         assert(E != NULL);
63         assert(E->A != NULL && E->B != NULL && E->p != NULL && E->h != NULL);
64
65         assert(mont != NULL);
66         assert(mont->p != NULL);
67
68         assert(ctx != NULL);
69
70         if (E->is_in_mont) return 1;
71
72         if (!BN_lshift(E->A, E->A, mont->R_num_bits)) return 0;
73         if (!BN_mod(E->A, E->A, mont->p, ctx)) return 0;
74
75         if (!BN_lshift(E->B, E->B, mont->R_num_bits)) return 0;
76         if (!BN_mod(E->B, E->B, mont->p, ctx)) return 0;
77
78         if (!BN_lshift(E->h, E->h, mont->R_num_bits)) return 0;
79         if (!BN_mod(E->h, E->h, mont->p, ctx)) return 0;
80
81         E->is_in_mont = 1;
82         return 1;
83
84 }
85
86
87 int EC_from_montgomery(EC *E, BN_MONTGOMERY *mont, BN_CTX *ctx)
88 {
89         assert(E != NULL);
90         assert(E->A != NULL && E->B != NULL && E->p != NULL && E->h != NULL);
91
92         assert(mont != NULL);
93         assert(mont->p != NULL);
94
95         assert(ctx != NULL);
96
97         if (!E->is_in_mont) return 1;
98
99         if (!BN_mont_red(E->A, mont)) return 0;
100         if (!BN_mont_red(E->B, mont)) return 0;
101         if (!BN_mont_red(E->h, mont)) return 0;
102
103         E->is_in_mont = 0;
104         return 1;
105 }
106 #endif /* MONTGOMERY */
107
108 int EC_set_half(EC *E)
109 /* h <- 1/2 mod p = (p + 1)/2 */
110 {
111         assert(E != NULL);
112         assert(E->p != NULL);
113         assert(E->h != NULL);
114         assert(!E->is_in_mont);
115
116         if (BN_copy(E->h, E->p) == NULL) return 0; 
117         if (!BN_add_word(E->h, 1)) return 0;
118         if (!BN_rshift1(E->h, E->h)) return 0; 
119         return 1;
120 }