dhe.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * ZGrab Copyright 2015 Regents of the University of Michigan
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy
  6. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  11. * implied. See the License for the specific language governing
  12. * permissions and limitations under the License.
  13. */
  14. package json
  15. import (
  16. "encoding/json"
  17. "math/big"
  18. )
  19. // DHParams can be used to store finite-field Diffie-Hellman parameters. At any
  20. // point in time, it is unlikely that both OurPrivate and TheirPrivate will be
  21. // non-nil.
  22. type DHParams struct {
  23. Prime *big.Int
  24. Generator *big.Int
  25. ServerPublic *big.Int
  26. ServerPrivate *big.Int
  27. ClientPublic *big.Int
  28. ClientPrivate *big.Int
  29. SessionKey *big.Int
  30. }
  31. type auxDHParams struct {
  32. Prime *cryptoParameter `json:"prime"`
  33. Generator *cryptoParameter `json:"generator"`
  34. ServerPublic *cryptoParameter `json:"server_public,omitempty"`
  35. ServerPrivate *cryptoParameter `json:"server_private,omitempty"`
  36. ClientPublic *cryptoParameter `json:"client_public,omitempty"`
  37. ClientPrivate *cryptoParameter `json:"client_private,omitempty"`
  38. SessionKey *cryptoParameter `json:"session_key,omitempty"`
  39. }
  40. // MarshalJSON implements the json.Marshal interface
  41. func (p *DHParams) MarshalJSON() ([]byte, error) {
  42. aux := auxDHParams{
  43. Prime: &cryptoParameter{Int: p.Prime},
  44. Generator: &cryptoParameter{Int: p.Generator},
  45. }
  46. if p.ServerPublic != nil {
  47. aux.ServerPublic = &cryptoParameter{Int: p.ServerPublic}
  48. }
  49. if p.ServerPrivate != nil {
  50. aux.ServerPrivate = &cryptoParameter{Int: p.ServerPrivate}
  51. }
  52. if p.ClientPublic != nil {
  53. aux.ClientPublic = &cryptoParameter{Int: p.ClientPublic}
  54. }
  55. if p.ClientPrivate != nil {
  56. aux.ClientPrivate = &cryptoParameter{Int: p.ClientPrivate}
  57. }
  58. if p.SessionKey != nil {
  59. aux.SessionKey = &cryptoParameter{Int: p.SessionKey}
  60. }
  61. return json.Marshal(aux)
  62. }
  63. // UnmarshalJSON implement the json.Unmarshaler interface
  64. func (p *DHParams) UnmarshalJSON(b []byte) error {
  65. var aux auxDHParams
  66. if err := json.Unmarshal(b, &aux); err != nil {
  67. return err
  68. }
  69. if aux.Prime != nil {
  70. p.Prime = aux.Prime.Int
  71. }
  72. if aux.Generator != nil {
  73. p.Generator = aux.Generator.Int
  74. }
  75. if aux.ServerPublic != nil {
  76. p.ServerPublic = aux.ServerPublic.Int
  77. }
  78. if aux.ServerPrivate != nil {
  79. p.ServerPrivate = aux.ServerPrivate.Int
  80. }
  81. if aux.ClientPublic != nil {
  82. p.ClientPublic = aux.ClientPublic.Int
  83. }
  84. if aux.ClientPrivate != nil {
  85. p.ClientPrivate = aux.ClientPrivate.Int
  86. }
  87. if aux.SessionKey != nil {
  88. p.SessionKey = aux.SessionKey.Int
  89. }
  90. return nil
  91. }
  92. // CryptoParameter represents a big.Int used a parameter in some cryptography.
  93. // It serializes to json as a tupe of a base64-encoded number and a length in
  94. // bits.
  95. type cryptoParameter struct {
  96. *big.Int
  97. }
  98. type auxCryptoParameter struct {
  99. Raw []byte `json:"value"`
  100. Length int `json:"length"`
  101. }
  102. // MarshalJSON implements the json.Marshaler interface
  103. func (p *cryptoParameter) MarshalJSON() ([]byte, error) {
  104. var aux auxCryptoParameter
  105. if p.Int != nil {
  106. aux.Raw = p.Bytes()
  107. aux.Length = 8 * len(aux.Raw)
  108. }
  109. return json.Marshal(&aux)
  110. }
  111. // UnmarshalJSON implements the json.Unmarshal interface
  112. func (p *cryptoParameter) UnmarshalJSON(b []byte) error {
  113. var aux auxCryptoParameter
  114. if err := json.Unmarshal(b, &aux); err != nil {
  115. return err
  116. }
  117. p.Int = new(big.Int)
  118. p.SetBytes(aux.Raw)
  119. return nil
  120. }