@@ -2,90 +2,93 @@ pragma circom 2.1.8;
2
2
3
3
include " ./hmac/circuits/hmac.circom" ;
4
4
5
- // ss : secret length
6
- // is : info length
5
+ // s : salt length
6
+ // i : info length
7
7
// k : key length
8
8
// m : number of keys to extract
9
9
// n : key length
10
10
template HKDFSha256 (s,i,k,m,n ){
11
- signal input secret [s];
11
+ signal input salt [s];
12
12
signal input info[i];
13
13
signal input key[k];
14
14
15
- component hmac = HmacSha256(s, k);
16
15
signal output out[m][n];
17
16
18
- hmac.message <== secret;
19
- hmac.key <== key;
17
+ component extract = Extract(s, k);
18
+ extract.salt <== salt;
19
+ extract.key <== key;
20
20
21
- component extract = Extract (i, 32 , m, n);
22
- extract .info <== info;
23
- extract .key <== hmac.hmac ;
21
+ component expand = Expand (i, 32 , m, n);
22
+ expand .info <== info;
23
+ expand .key <== extract.out ;
24
24
25
- out <== extract .out;
25
+ out <== expand .out;
26
26
}
27
27
28
- // n : message length
28
+ // s : salt length
29
29
// k : key length
30
30
// out : 32 bytes from sha256 hmac
31
- template Expand ( n ,k ){
32
- signal input secret[n ];
31
+ template Extract ( s ,k ){
32
+ signal input salt[s ];
33
33
signal input key[k];
34
34
35
- component hmac = HmacSha256(n, k );
35
+ component hmac = HmacSha256(k,s );
36
36
signal output out[32 ];
37
37
38
- hmac.message <== secret ;
39
- hmac.key <== key ;
38
+ hmac.message <== key ;
39
+ hmac.key <== salt ;
40
40
41
41
out <== hmac.hmac;
42
42
}
43
43
44
- // n : message length
44
+ // i : info length
45
45
// k : key length
46
46
// m : number of keys to extract
47
- // s : key length
48
- template Extract ( n ,k,m,s ){
49
- signal input info[n ];
47
+ // n : key length
48
+ template Expand ( i ,k,m,n ){
49
+ signal input info[i ];
50
50
signal input key[k];
51
51
52
- var size = 32 + n + 1 ; // 32 bytes for hmac, n bytes for info, 1 byte for counter
52
+ var size = 32 + i + 1 ; // 32 bytes for hmac, i bytes for info, 1 byte for counter
53
53
54
54
// hash size is 32 bytes
55
- var rounds = (m* s )\(32 );
56
- rounds = (rounds * 32 ) < (m* s ) ? rounds + 1 : rounds;
55
+ var rounds = (m* n )\(32 );
56
+ rounds = (rounds * 32 ) < (m* n ) ? rounds + 1 : rounds;
57
57
58
58
59
59
component hmac[rounds];
60
60
61
61
signal expandedKeys [rounds][32 ];
62
- signal output out[m][s ];
62
+ signal output out[m][n ];
63
63
64
- hmac[0 ] = HmacSha256(1 , k);
65
- hmac[0 ].message[0 ] <== 1 ; // here counter is byte(1)
66
- hmac[0 ].key <== key;
64
+ hmac[0 ] = HmacSha256(i+ 1 ,k);
65
+ hmac[0 ].key <== key;
66
+ for (var j = 0 ; j < i; j++ ){
67
+ hmac[0 ].message[j] <== info[j];
68
+ }
69
+ hmac[0 ].message[i] <== 1 ; // here counter is byte(1)
67
70
expandedKeys[0 ] <== hmac[0 ].hmac;
68
71
69
72
var counter = 2 ; // counter is byte(2)
70
73
71
- for (var i = 1 ; i < rounds; i ++ ){
72
- hmac[i ] = HmacSha256(size, k);
73
- for (var j = 0 ; j < 32 ; j ++ ){
74
- hmac[i ].message[j ] <== expandedKeys[i - 1 ][j ];
74
+ for (var j = 1 ; j < rounds; j ++ ){
75
+ hmac[j ] = HmacSha256(size, k);
76
+ for (var o = 0 ; o < 32 ; o ++ ){
77
+ hmac[j ].message[o ] <== expandedKeys[j - 1 ][o ];
75
78
}
76
- for (var j = 0 ; j < n; j ++ ){
77
- hmac[i ].message[32 + j ] <== info[j ];
79
+ for (var o = 0 ; o < i; o ++ ){
80
+ hmac[j ].message[32 + o ] <== info[o ];
78
81
}
79
- hmac[i ].message[32 + n ] <== counter;
80
- hmac[i ].key <== key;
81
- expandedKeys[i ] <== hmac[i ].hmac;
82
+ hmac[j ].message[32 + i ] <== counter;
83
+ hmac[j ].key <== key;
84
+ expandedKeys[j ] <== hmac[j ].hmac;
82
85
counter = counter + 1 ;
83
86
}
84
87
85
88
var byteIndex = 0 ;
86
- for (var i = 0 ; i < m; i ++ ) {
87
- for (var j = 0 ; j < s; j ++ ) {
88
- out[i][j ] <== expandedKeys[byteIndex \ 32 ][byteIndex % 32 ];
89
+ for (var j = 0 ; j < m; j ++ ) {
90
+ for (var o = 0 ; o < n; o ++ ) {
91
+ out[j][o ] <== expandedKeys[byteIndex \ 32 ][byteIndex % 32 ];
89
92
byteIndex++ ;
90
93
}
91
94
}
0 commit comments